Commit 7711df65 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Improve poll script

parent a7a55962
...@@ -7,6 +7,7 @@ import grequests ...@@ -7,6 +7,7 @@ import grequests
import json import json
from argparse import ArgumentParser from argparse import ArgumentParser
# 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'),
...@@ -42,6 +43,8 @@ if __name__ == '__main__': ...@@ -42,6 +43,8 @@ if __name__ == '__main__':
'(default dev/shm/tothemoon)') '(default dev/shm/tothemoon)')
parser.add_argument('-i', '--interval', metavar='SECONDS=2', type=float, parser.add_argument('-i', '--interval', metavar='SECONDS=2', type=float,
default=2, help='exchange poll interval') default=2, help='exchange poll interval')
parser.add_argument('-t', '--timeout', metavar='SECONDS=7', type=float,
default=5, help='timeout for polling')
parser.add_argument('-l', '--list', action='store_true', default=False, parser.add_argument('-l', '--list', action='store_true', default=False,
help='list api info') help='list api info')
args = parser.parse_args() args = parser.parse_args()
...@@ -79,8 +82,7 @@ if __name__ == '__main__': ...@@ -79,8 +82,7 @@ if __name__ == '__main__':
print >>sys.stderr, e.strerror print >>sys.stderr, e.strerror
sys.exit(1) sys.exit(1)
# keep updating URLs from priority queue, adding them back to the queue # poll APIs in endless loop
# scheduled after their current interval
try: try:
last_update = 0 last_update = 0
...@@ -89,34 +91,38 @@ if __name__ == '__main__': ...@@ -89,34 +91,38 @@ 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.interval * 1.5) requests = (grequests.get(ex[0], timeout=args.timeout)
for ex in exchanges) for ex in exchanges)
responses = grequests.map(requests, exception_handler=handle_error) responses = grequests.map(requests, exception_handler=handle_error)
for (url, exid, indices, currency), res in zip(exchanges, responses): for (url, exid, indices, currency), res in zip(exchanges, responses):
status = {'last': None, 'currency': currency, 'updated': 0}
if res is None: if res is None:
print >>sys.stderr, exid, 'unknown error' print >>sys.stderr, 'Unknown error at %s' % exid
elif res.status_code != 200: elif res.status_code != 200:
print >>sys.stderr, 'server error %d at %s' % (res.status_code, exid) print >>sys.stderr, 'Server error %d at %s' % \
(res.status_code, exid)
else: else:
try: try:
last = res.json() last = res.json()
for i in indices: for i in indices:
last = last[i] last = last[i]
last = float(last)
status['updated'] = time.time() status = {
status['last'] = last 'last': float(last),
'currency': currency,
'updated': time.time()
}
with open(root + '/' + exid, 'w') as f:
json.dump(status, f)
except ValueError as e: except ValueError as e:
print >>sys.stderr, 'invalid response from %s:' % exid, e.message print >>sys.stderr, 'Invalid response from %s:' % \
exid, e.message
except KeyError as e: except KeyError as e:
print >>sys.stderr, 'unexpected response content from %s:' % exid, res.text print >>sys.stderr, 'Unexpected response ' \
'content from %s:' % exid, res.text
with open(root + '/' + exid, 'w') as f:
json.dump(status, f)
last_update = time.time() last_update = time.time()
except KeyboardInterrupt: 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