1 from PyQt4.QtCore import SIGNAL
7 class Exchanger(threading.Thread):
9 def __init__(self, parent):
10 threading.Thread.__init__(self)
13 self.quote_currencies = None
14 self.lock = threading.Lock()
18 def exchange(self, btc_amount, quote_currency):
20 if self.quote_currencies is None:
22 quote_currencies = self.quote_currencies.copy()
23 if quote_currency not in quote_currencies:
25 return btc_amount * quote_currencies[quote_currency]
32 connection = httplib.HTTPConnection('blockchain.info')
33 connection.request("GET", "/ticker")
36 response = connection.getresponse()
37 if response.reason == httplib.responses[httplib.NOT_FOUND]:
40 response = json.loads(response.read())
46 quote_currencies[r] = self._lookup_rate(response, r)
48 self.quote_currencies = quote_currencies
49 self.parent.emit(SIGNAL("refresh_balance()"))
53 def get_currencies(self):
54 return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
56 def _lookup_rate(self, response, quote_id):
57 return decimal.Decimal(str(response[str(quote_id)]["15m"]))
59 if __name__ == "__main__":
60 exch = Exchanger(("BRL", "CNY", "EUR", "GBP", "RUB", "USD"))
61 print exch.exchange(1, "EUR")