Guest User

Untitled

a guest
Dec 18th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.42 KB | None | 0 0
  1. require 'net/http'
  2. require 'open-uri'
  3. require 'json'
  4. require 'CGI'
  5.  
  6. module CandleLength
  7.   MINUTE = "60"
  8.   HOUR = "3600"
  9.   DAY="86400"
  10.   WEEK="week"
  11.   MONTH = "month"
  12. end
  13.  
  14. class QuoteInfo < Struct.new(:number, :label, :code, :seconds)
  15.   def initialize(number, label, code, seconds = CandleLength::MINUTE)
  16.     super
  17.   end
  18.  
  19.   def to_s
  20.     %Q{["#{number}", "#{seconds}", "#{code}", "#{label}"]}
  21.   end
  22. end
  23.  
  24. CurrentQuoteIndex = Struct.new(:label, :value, :candle_min, :candle_max) do
  25.   def to_s
  26.     "#{label}: current: #{value}, min candle: #{candle_min}, max candle: #{candle_max}"
  27.   end
  28. end
  29.  
  30. class InvestingComParser
  31.   attr_accessor :url
  32.   attr_accessor :data
  33.   attr_accessor :quotes
  34.  
  35.   BASE_URL = "http://www.investing.com/common/refresher_new/refresher_v13.2.php"
  36.   REQUEST_HEADERS = ['X-Requested-With' => 'XMLHttpRequest', 'Referer' => 'http://www.investing.com']
  37.  
  38.   def initialize(quotes)
  39.     parts = ['pulse'         => 100,
  40.       'refresher_version'    => 'v1.7.0',
  41.       'quotes_bar_selected'  => 1,
  42.       'tsb_currentTimeframe' => 60,
  43.       'fpcharts[]'           => quotes.map(&:to_s)]
  44.     self.url = BASE_URL << "?" << URI.encode_www_form(*parts)
  45.     self.update_data
  46.   end
  47.  
  48.   def update_data
  49.     json = open(self.url, *REQUEST_HEADERS, &:read)
  50.     self.data = JSON.parse(json)["js_instrument_chart"]["js_instrument_chart"]
  51.   end
  52.  
  53.   def get_by_quote(quote)
  54.     js_data = self.data[quote.number.to_s][quote.seconds.to_s]["chart_data"]
  55.     candle = js_data["candles"]["last_candle"]
  56.     CurrentQuoteIndex.new(quote.label, js_data["last_value"], candle["min"], candle["max"])
  57.   end
  58. end
  59.  
  60. quotes = [
  61.   QuoteInfo.new(2186, "USD/RUB", 1001),
  62.   QuoteInfo.new(1691, "EUR/RUB", 1001),
  63.   QuoteInfo.new(940802, "RUB/UAH", 1003, CandleLength::HOUR),
  64.   QuoteInfo.new(8833, "Brent", 1004),
  65.   QuoteInfo.new(13665, "RTSI", 40),
  66.   QuoteInfo.new(8839, "S&P 500", 1004),
  67.   QuoteInfo.new(8830, "GOLD", 1004),
  68.   QuoteInfo.new(2208, "USD/UAH", 1001),
  69.   QuoteInfo.new(1709, "EUR/UAH", 1001)
  70. ]
  71. parser = InvestingComParser.new(quotes)
  72.  
  73. t = ARGV[0].to_i
  74. update_time = (t > 0) ? t : 15
  75.  
  76. # main loop
  77. puts "\e[H\e[2J"
  78. while (true)
  79.   puts "^ INFO: last_update=#{Time.now.strftime("%d/%m/%Y %H:%M:%S")}; update_time=#{update_time}sec; quotes_count=#{quotes.count}"
  80.   puts ""
  81.   parser.update_data
  82.   quotes.each do |quote|
  83.     puts parser.get_by_quote(quote).to_s
  84.   end
  85.   sleep(update_time)
  86.   puts "\e[H\e[2J"
  87. end
Advertisement
Add Comment
Please, Sign In to add comment