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

Improve poll script

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