Commit 190cfd25 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Use grequests.imap in poll script

parent 78eab148
...@@ -8,26 +8,26 @@ import json ...@@ -8,26 +8,26 @@ import json
from argparse import ArgumentParser from argparse import ArgumentParser
# exchanges as (url, id, response_indices_last, currency) # exchanges as (url, id, response_indices_last, currency)
exchanges = [ exchanges = {
('https://btc-e.com/api/3/ticker/btc_usd', 'https://btc-e.com/api/3/ticker/btc_usd':
'btce', ('btc_usd', 'last'), 'USD'), ('btce', ('btc_usd', 'last'), 'USD'),
('https://api.gdax.com/products/BTC-USD/ticker', 'https://api.gdax.com/products/BTC-USD/ticker':
'coinbase', ('price',), 'USD'), ('coinbase', ('price',), 'USD'),
('https://www.bitstamp.net/api/v2/ticker/btcusd/', 'https://www.bitstamp.net/api/v2/ticker/btcusd/':
'bitstamp', ('last',), 'USD'), ('bitstamp', ('last',), 'USD'),
('https://api.bitfinex.com/v1/pubticker/btcusd', 'https://api.bitfinex.com/v1/pubticker/btcusd':
'bitfinex', ('last_price',), 'USD'), ('bitfinex', ('last_price',), 'USD'),
('https://data.btcchina.com/data/ticker?market=btccny', 'https://data.btcchina.com/data/ticker?market=btccny':
'btcchina', ('ticker', 'last'), 'CNY'), ('btcchina', ('ticker', 'last'), 'CNY'),
('http://api.huobi.com/staticmarket/ticker_btc_json.js', 'http://api.huobi.com/staticmarket/ticker_btc_json.js':
'huobi', ('ticker', 'last'), 'CNY'), ('huobi', ('ticker', 'last'), 'CNY'),
('https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd', 'https://www.okcoin.com/api/v1/ticker.do?symbol=btc_usd':
'okcoin', ('ticker', 'last'), 'CNY'), ('okcoin', ('ticker', 'last'), 'CNY'),
('https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR', 'https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR':
'kraken', ('result', 'XXBTZEUR', 'c', 0), 'EUR'), ('kraken', ('result', 'XXBTZEUR', 'c', 0), 'EUR'),
('https://api.coinmarketcap.com/v1/ticker/bitcoin/', 'https://api.coinmarketcap.com/v1/ticker/bitcoin/':
'coinmarketcap', (0, 'market_cap_usd'), 'USD'), ('coinmarketcap', (0, 'market_cap_usd'), 'USD'),
] }
def handle_error(req, e): def handle_error(req, e):
...@@ -52,7 +52,7 @@ if __name__ == '__main__': ...@@ -52,7 +52,7 @@ if __name__ == '__main__':
root = args.dir root = args.dir
if args.list: if args.list:
for url, exid, indices, currency in exchanges: for url, (exid, indices, currency) in exchanges.iteritems():
print exid print exid
print ' url: ', url print ' url: ', url
print ' currency:', currency print ' currency:', currency
...@@ -63,7 +63,7 @@ if __name__ == '__main__': ...@@ -63,7 +63,7 @@ if __name__ == '__main__':
# clean up created files on exit # clean up created files on exit
def remove_files(): def remove_files():
for url, exid, indices, currency in exchanges: for url, (exid, indices, currency) in exchanges.iteritems():
path = root + '/' + exid path = root + '/' + exid
if os.path.exists(path): if os.path.exists(path):
os.remove(path) os.remove(path)
...@@ -91,20 +91,27 @@ if __name__ == '__main__': ...@@ -91,20 +91,27 @@ if __name__ == '__main__':
if diff < args.interval: if diff < args.interval:
time.sleep(args.interval - diff) time.sleep(args.interval - diff)
requests = (grequests.get(ex[0], timeout=args.timeout) requests = (grequests.get(url, timeout=args.timeout)
for ex in exchanges) for url in exchanges.iterkeys())
responses = grequests.map(requests, exception_handler=handle_error)
for (url, exid, indices, currency), res in zip(exchanges, responses): for res in grequests.imap(requests, exception_handler=handle_error):
if res is None: if res is None:
print >>sys.stderr, 'Unknown error at %s' % exid continue
elif res.status_code != 200:
exid, indices, currency = exchanges[res.request.url]
try:
res_json = res.json()
if res.status_code != 200:
if 'error' in res_json:
print >>sys.stderr, 'Server error %d at %s: %s' % \
(res.status_code, exid, res_json['error'])
else:
print >>sys.stderr, 'Server error %d at %s' % \ print >>sys.stderr, 'Server error %d at %s' % \
(res.status_code, exid) (res.status_code, exid)
else: else:
try: last = res_json
last = res.json()
for i in indices: for i in indices:
last = last[i] last = last[i]
...@@ -115,6 +122,7 @@ if __name__ == '__main__': ...@@ -115,6 +122,7 @@ if __name__ == '__main__':
} }
with open(root + '/' + exid, 'w') as f: with open(root + '/' + exid, 'w') as f:
print >>sys.stderr, 'Updating', exid
json.dump(status, f) json.dump(status, f)
except ValueError as e: except ValueError as e:
......
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