49 lines
1.6 KiB
Ruby
49 lines
1.6 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 = 3
|
|
|
|
start_index = '-1'
|
|
currencies = {1 => "BTC", 4 => "LSK", 5 => "LTC", 7 => "GAME"}
|
|
currency_ids = [5,4,7]
|
|
#currency = Currency.where(name: 'LSK').first
|
|
while true
|
|
for currency_id in currency_ids
|
|
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
|
|
else
|
|
start_index = -1
|
|
end
|
|
uri = "https://bitbay.net/API/Public/#{currencies[currency_id]}PLN/trades.json?since=#{start_index}"
|
|
puts "start at #{start_index} index for #{currencies[currency_id]}"
|
|
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
|
|
end
|