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