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.HTTPSConnection('blockchain.info')
33 connection.request("GET", "/ticker")
36 response = connection.getresponse()
37 if response.reason == httplib.responses[httplib.NOT_FOUND]:
39 response = json.loads(response.read())
43 quote_currencies[r] = self._lookup_rate(response, r)
45 self.quote_currencies = quote_currencies
46 self.parent.emit(SIGNAL("refresh_balance()"))
50 def get_currencies(self):
51 return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
53 def _lookup_rate(self, response, quote_id):
54 return decimal.Decimal(str(response[str(quote_id)]["15m"]))
56 if __name__ == "__main__":
57 exch = Exchanger(("BRL", "CNY", "EUR", "GBP", "RUB", "USD"))
58 print exch.exchange(1, "EUR")