cryptobot/app/scripts/get_bit_bay_trades.rb

42 lines
1.5 KiB
Ruby

#!/usr/bin/env ruby
# encoding: UTF-8
ENV["RAILS_ENV"] ||= "development"
require File.dirname(__FILE__) + "/../../config/environment"
require 'uri'
require 'net/http'
require 'json'
pause_interval = 5
start_index = '-1'
currency = Currency.where(name: 'GAME').first
while true
start_time = Time.now.to_i
bbt_index = BitBayTrade.where('currency_id = ?', currency.id).order('cast(tid as unsigned) DESC').first
unless bbt_index.blank?
start_index = bbt_index.tid
end
uri = "https://bitbay.net/API/Public/#{currency.name}PLN/trades.json?since=#{start_index}"
puts "start at #{start_index} index"
url = URI.parse(uri)
resp = Net::HTTP.get_response(url)
my_hash = JSON.parse(resp.body)
for mh in my_hash
bbt = BitBayTrade.where('currency_id = ? AND tid = ?', currency.id, mh['tid']).first
if bbt.blank?
BitBayTrade.create(currency_id: currency.id, tid: mh['tid'], date: Time.at(mh['date'].to_i), transaction_type: mh['type'], price: mh['price'], amount: mh['amount'])
#puts "#{mh['tid']} -#{mh['date']} - #{mh['price']} - #{mh['type']} - #{mh['amount']}"
end
end
end_time = Time.now.to_i
if end_time > start_time
diff_time = end_time - start_time
if diff_time < pause_interval
puts "Sleep #{pause_interval - diff_time + 1} dla #{diff_time}"
sleep(pause_interval - diff_time + 1)
end
else
sleep(pause_interval)
end
end