Commit 407be6e0 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Implement simple polling server that updates files in shared memory

parent 1b914140
import requests #!/usr/bin/env python
import sys
import os
import atexit
import time
import grequests
from Queue import PriorityQueue
class APIError(RuntimeError): interval = 3 # poll exchange tickers every 3 seconds
pass cap_interval = 60 # poll check market cap every minute
def get_check_status(errstr, url, *args): exchanges = {
r = requests.get(url, timeout=10.0, *args) 'https://btc-e.com/api/3/ticker/btc_usd':
if r.status_code != 200: ('btce', ('btc_usd', 'last'), 'USD', interval),
raise APIError(errstr) 'https://api.gdax.com/products/BTC-USD/ticker':
return r.json() ('coinbase', ('price',), 'USD', interval),
'https://www.bitstamp.net/api/v2/ticker/btcusd/':
('bitstamp', ('last',), 'USD', interval),
'https://api.bitfinex.com/v1/pubticker/btcusd':
('bitfinex', ('last_price',), 'USD', interval),
'https://data.btcchina.com/data/ticker?market=btccny':
('btcchina', ('ticker', 'last'), 'CNY', interval),
'http://api.huobi.com/staticmarket/ticker_btc_json.js':
('huobi', ('ticker', 'last'), 'CNY', interval),
'https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd':
('okcoin', ('ticker', 'last'), 'CNY', interval),
'https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR':
('kraken', ('result', 'XXBTZEUR', 'c', 0), 'EUR', interval),
'https://api.coinmarketcap.com/v1/ticker/bitcoin/':
('coinmarketcap', (0, 'market_cap_usd'), 'USD', cap_interval),
}
def poll_btce(): queue = PriorityQueue(len(exchanges))
url = 'https://btc-e.com/api/3/ticker/btc_usd'
return float(get_check_status('BTC-e', url)['btc_usd']['last']), 'USD'
def poll_coinbase(): def generate_requests():
url = 'https://api.gdax.com/products/BTC-USD/ticker' while True:
return float(get_check_status('Coinbase', url)['price']), 'USD' scheduled, url = queue.get()
wait = scheduled - time.time()
def poll_bitstamp(): if wait > 0:
url = 'https://www.bitstamp.net/api/v2/ticker/btcusd/' time.sleep(wait)
return float(get_check_status('Bitstamp', url)['last']), 'USD'
def poll_bitfinex(): yield grequests.get(url, timeout=5)
url = 'https://api.bitfinex.com/v1/pubticker/btcusd'
return float(get_check_status('Bitfinex', url)['last_price']), 'USD'
def poll_btcchina(): if __name__ == '__main__':
url = 'https://data.btcchina.com/data/ticker?market=btccny' for url in exchanges.iterkeys():
return float(get_check_status('BTCChina', url)['ticker']['last']), 'CNY' queue.put((0, url))
def poll_huobi(): root = sys.argv[1] if len(sys.argv) == 2 else '/dev/shm/tothemoon'
url = 'http://api.huobi.com/staticmarket/ticker_btc_json.js' create_root = not os.path.isdir(root)
return float(get_check_status('Huobi', url)['ticker']['last']), 'CNY'
def poll_okcoin(): def remove_files():
url = 'https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd' for ex in exchanges.itervalues():
return float(get_check_status('OKcoin', url)['ticker']['last']), 'CNY' path = root + '/' + ex[0]
if os.path.exists(path):
os.remove(path)
def poll_kraken(): if create_root:
url = 'https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR' os.rmdir(root)
return float(get_check_status('Kraken', url)['result']['XXBTZEUR']['c'][0]), 'EUR'
def poll_coinmarketcap(): atexit.register(remove_files)
url = 'https://api.coinmarketcap.com/v1/ticker/bitcoin'
return get_check_status('Coinmarketcap', url)[0]['market_cap_usd'], 'USD'
if __name__ == '__main__': if create_root:
print 'btc-e', poll_btce() try:
print 'coinbase', poll_coinbase() os.mkdir(root)
print 'bitstamp', poll_bitstamp() except OSError as e:
print 'bitfinex', poll_bitfinex() print >>sys.stderr, 'Could not create root directory %s:' % root,
print 'btcchina', poll_btcchina() print >>sys.stderr, e.strerror
print 'huobi', poll_huobi() sys.exit(1)
print 'okcoin', poll_okcoin()
print 'kraken', poll_kraken() try:
print 'coinmarketcap', poll_coinmarketcap() for res in grequests.imap(generate_requests()):
exid, indices, currency, interval = exchanges[res.request.url]
queue.put((time.time() + interval, res.request.url))
last = res.json()
for i in indices:
last = last[i]
last = float(last)
with open(root + '/' + exid, 'w') as f:
print >>f, last, currency
except KeyboardInterrupt:
pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment