-
Notifications
You must be signed in to change notification settings - Fork 21
/
ticker.py
30 lines (28 loc) · 1003 Bytes
/
ticker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import urllib
import json
from PyQt4.Qt import QThread
class Ticker(QThread):
def __init__(self, source, currency, url, fields, interval, callback):
QThread.__init__(self)
self.source = source
self.url = url
self.fields = fields
self.interval = interval
self.callback = callback
def run(self):
self.sleep(5) # give GUI time to initialize
while True:
try:
f = urllib.urlopen(self.url)
data = f.read()
feed = json.loads(data)
rate = feed
for field in self.fields:
rate = rate[field]
rate = float(rate)
self.callback(rate, self.source)
except (ValueError, KeyError):
print "Warning: Unable to parse exchange rate ticker"
except IOError:
print "Warning: Unable to access exchange rate ticker"
self.sleep(self.interval)