commit 8d75b68152124a04194fa280b4c9c20bdb61d783
parent 02d8d297dc86d0f24393058d75eae7b6e83509ae
Author: Amir Taaki <genjix@riseup.net>
Date: Sat, 30 Jun 2012 14:43:42 +0200
show the BTC value in other currency.
Diffstat:
2 files changed, 38 insertions(+), 7 deletions(-)
diff --git a/lib/exchange_rate.py b/lib/exchange_rate.py
@@ -1,12 +1,37 @@
+import decimal
+import httplib
+import json
+
class Exchanger:
- def __init__(self, quote_currencies):
- self.quote_currencies = quote_currencies
+ def __init__(self, quote_currencies, refresh_balance):
+ self.refresh_balance = refresh_balance
+ self.quote_currencies = {}
def exchange(self, btc_amount, quote_currency):
- assert quote_currency in self.quote_currencies
+ return btc_amount * self.quote_currencies[quote_currency]
+
+ def discovery(self):
+ connection = httplib.HTTPSConnection('intersango.com')
+ connection.request("GET", "/api/ticker.php")
+ response = connection.getresponse()
+ if response.status == 404:
+ return
+ response = json.loads(response.read())
+ # 1 = BTC:GBP
+ # 2 = BTC:EUR
+ # 3 = BTC:USD
+ # 4 = BTC:PLN
+ try:
+ self.quote_currencies["GBP"] = self.lookup_rate(response, 1)
+ self.quote_currencies["EUR"] = self.lookup_rate(response, 2)
+ self.quote_currencies["USD"] = self.lookup_rate(response, 3)
+ self.refresh_balance()
+ except KeyError:
+ pass
- return btc_amount * 6
+ def lookup_rate(self, response, quote_id):
+ return decimal.Decimal(response[str(quote_id)]["last"])
if __name__ == "__main__":
exch = Exchanger(("EUR", "USD", "GBP"))
diff --git a/lib/gui_lite.py b/lib/gui_lite.py
@@ -86,10 +86,12 @@ class MiniWindow(QDialog):
self.btc_balance = 0
self.quote_currencies = ("EUR", "USD", "GBP")
- self.exchanger = exchange_rate.Exchanger(self.quote_currencies)
+ self.exchanger = exchange_rate.Exchanger(self.quote_currencies,
+ self.refresh_balance)
self.balance_label = BalanceLabel(self.change_quote_currency)
self.balance_label.setObjectName("balance_label")
+ self.exchanger.discovery()
copy_button = QPushButton(_("&Copy Address"))
copy_button.setObjectName("copy_button")
@@ -163,13 +165,17 @@ class MiniWindow(QDialog):
def change_quote_currency(self):
self.quote_currencies = \
self.quote_currencies[1:] + self.quote_currencies[0:1]
+ self.refresh_balance()
+
+ def refresh_balance(self):
self.set_balances(self.btc_balance)
def set_balances(self, btc_balance):
self.btc_balance = btc_balance
- btc_balance /= bitcoin(1)
- quote_balance = "%.2f" % (btc_balance * 6)
quote_currency = self.quote_currencies[0]
+ quote_balance = self.exchanger.exchange(btc_balance, quote_currency)
+ quote_balance = "%.2f" % (quote_balance / bitcoin(1))
+ btc_balance = "%.2f" % (btc_balance / bitcoin(1))
self.balance_label.set_balances( \
btc_balance, quote_balance, quote_currency)
main_account_info = \