Created
March 6, 2019 13:45
-
-
Save mnaichuk/574e65c207104c3714e93f1004ed97aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# USAGE | |
# ruby app.rb 'NEOUSD' 'neo-usd-data.json' "2019-03-05 13:15:00 +0200" | |
require 'faye/websocket' | |
require 'eventmachine' | |
require 'pry' | |
@data = [] | |
@market = ARGV[0] | |
@file = File.open(ARGV[1], "a") | |
def start_connection | |
EM.run { | |
ws = Faye::WebSocket::Client.new('wss://api-pub.bitfinex.com/ws/2') | |
ws.on :open do |event| | |
ws.send({ "event": "subscribe", "channel": "book", "prec": "R0", "symbol": @market }.to_json) | |
end | |
ws.on :message do |event| | |
next if JSON.parse(event.data).is_a?(Hash) | |
entry = JSON.parse(event.data) | |
next if entry[1] == 'hb' || entry[1][1] == 0 | |
if entry[1].count > 1 && entry[1][0].is_a?(Array) | |
entry[1].each do |point| | |
next if @data.include?(point) | |
@data << point | |
end | |
next | |
end | |
next if @data.include?(entry[1]) | |
@data << entry[1] | |
if Time.now.to_s == ARGV[2] | |
p Time.now.to_i | |
"Finished, processed #{@data.count} orders" | |
@file.write(@data) | |
return 'ok' | |
end | |
end | |
ws.on :close do |event| | |
puts [:close, event.code, event.reason] | |
puts "Processed #{@data.count} orders" | |
puts Time.now.to_s | |
puts "Retrieve connection" | |
start_connection | |
end | |
} | |
end | |
start_connection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment