Skip to content

Instantly share code, notes, and snippets.

@defHLT
Created November 19, 2013 15:10
Show Gist options
  • Save defHLT/7546766 to your computer and use it in GitHub Desktop.
Save defHLT/7546766 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# encoding: utf-8
# Created: 2013-04-15 00:52:16 +0300 by ice
unless $*[0]
puts "usage: ruby mticker.rb 60 2>/dev/nul"
exit
end
STDOUT.sync = true
require 'net/http'
require 'json'
def get_json uri_raw, use_ssl=true
uri = URI.parse uri_raw
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = use_ssl
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
JSON.parse response.read_body
end
TICKER_FAST = 'https://data.mtgox.com/api/1/BTCUSD/ticker_fast'
if __FILE__ == $0
begin
t1 = Thread.new do
loop do
result, ret = get_json(TICKER_FAST).values_at 'result', 'return'
if result == 'success'
$price = ret['last_local']['display']
else
$price = "-1.0"
end
end
end
loop do
puts "#{Time.now.to_s}: " << $price
sleep $*[0].to_i
end
rescue
sleep 2
retry
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment