Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/http'
- require 'open-uri'
- require 'json'
- require 'CGI'
- module CandleLength
- MINUTE = "60"
- HOUR = "3600"
- DAY="86400"
- WEEK="week"
- MONTH = "month"
- end
- class QuoteInfo < Struct.new(:number, :label, :code, :seconds)
- def initialize(number, label, code, seconds = CandleLength::MINUTE)
- super
- end
- def to_s
- %Q{["#{number}", "#{seconds}", "#{code}", "#{label}"]}
- end
- end
- CurrentQuoteIndex = Struct.new(:label, :value, :candle_min, :candle_max) do
- def to_s
- "#{label}: current: #{value}, min candle: #{candle_min}, max candle: #{candle_max}"
- end
- end
- class InvestingComParser
- attr_accessor :url
- attr_accessor :data
- attr_accessor :quotes
- BASE_URL = "http://www.investing.com/common/refresher_new/refresher_v13.2.php"
- REQUEST_HEADERS = ['X-Requested-With' => 'XMLHttpRequest', 'Referer' => 'http://www.investing.com']
- def initialize(quotes)
- parts = ['pulse' => 100,
- 'refresher_version' => 'v1.7.0',
- 'quotes_bar_selected' => 1,
- 'tsb_currentTimeframe' => 60,
- 'fpcharts[]' => quotes.map(&:to_s)]
- self.url = BASE_URL << "?" << URI.encode_www_form(*parts)
- self.update_data
- end
- def update_data
- json = open(self.url, *REQUEST_HEADERS, &:read)
- self.data = JSON.parse(json)["js_instrument_chart"]["js_instrument_chart"]
- end
- def get_by_quote(quote)
- js_data = self.data[quote.number.to_s][quote.seconds.to_s]["chart_data"]
- candle = js_data["candles"]["last_candle"]
- CurrentQuoteIndex.new(quote.label, js_data["last_value"], candle["min"], candle["max"])
- end
- end
- quotes = [
- QuoteInfo.new(2186, "USD/RUB", 1001),
- QuoteInfo.new(1691, "EUR/RUB", 1001),
- QuoteInfo.new(940802, "RUB/UAH", 1003, CandleLength::HOUR),
- QuoteInfo.new(8833, "Brent", 1004),
- QuoteInfo.new(13665, "RTSI", 40),
- QuoteInfo.new(8839, "S&P 500", 1004),
- QuoteInfo.new(8830, "GOLD", 1004),
- QuoteInfo.new(2208, "USD/UAH", 1001),
- QuoteInfo.new(1709, "EUR/UAH", 1001)
- ]
- parser = InvestingComParser.new(quotes)
- t = ARGV[0].to_i
- update_time = (t > 0) ? t : 15
- # main loop
- puts "\e[H\e[2J"
- while (true)
- puts "^ INFO: last_update=#{Time.now.strftime("%d/%m/%Y %H:%M:%S")}; update_time=#{update_time}sec; quotes_count=#{quotes.count}"
- puts ""
- parser.update_data
- quotes.each do |quote|
- puts parser.get_by_quote(quote).to_s
- end
- sleep(update_time)
- puts "\e[H\e[2J"
- end
Advertisement
Add Comment
Please, Sign In to add comment