Created
October 15, 2014 10:11
-
-
Save StasKoval/19292b0547a7f84f2c84 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
require "#{Rails.root}/app/helpers/application_helper" | |
namespace :bittrix do | |
include ApplicationHelper | |
require 'open-uri' | |
require "net/http" | |
require "net/https" | |
require 'json' | |
desc "TODO" | |
task load_currencies: :environment do | |
url = 'https://bittrex.com/api/v1.1/public/getmarketsummaries' | |
uri = URI(url) | |
req = Net::HTTP::Get.new(url) | |
res = Net::HTTP.start( | |
uri.host, uri.port, | |
:use_ssl => uri.scheme == 'https', | |
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https| | |
result = https.request(req) | |
data = JSON.parse(result.body) | |
data['result'].each do |r| | |
if r['MarketName'].scan(/BTC-/).first | |
@market = Currency.find_or_create_by(name: r['MarketName']) | |
@market.basevolume = r['BaseVolume'] | |
@market.volume = r['Volume'] | |
@market.created = r['Created'] | |
@market.save | |
end | |
end | |
end | |
end | |
desc "TODO" | |
task load_ticks: :environment do | |
Currency.find_each(batch_size: 10) do |currency| | |
url = "https://138.91.156.50/Market/Pub_GetMarketData?marketName=#{currency[:name]}&lastFill=1&lastBuyActivity=1&lastSellActivity=1&tickEpoch=#{unixtimestamp_to_winstamp(currency[:created].to_i)}&tickInterval=onemin&_=#{unixtimestamp_to_winstamp(DateTime.now.to_i)}" | |
puts url | |
uri = URI(url) | |
req = Net::HTTP::Get.new(url) | |
res = Net::HTTP.start( | |
uri.host, uri.port, | |
:use_ssl => uri.scheme == 'https', | |
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https| | |
result = https.request(req) | |
data = JSON.parse(result.body) | |
data['ticks'].each do |d| | |
if currency.coins.where(date: "#{Time.at(d["T"].gsub(/[^\d]/, '').to_i/1000)}").blank? | |
currency.coins.create!(name: currency[:name], date: "#{Time.at(d["T"].gsub(/[^\d]/, '').to_i/1000)}", | |
volume: d["V"], open: d["O"], high: d["H"], low: d["L"], close: d["C"]) | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment