-WIP-electrum-btcp/lib/exchange_rate.py

40 lines
1.2 KiB
Python
Raw Normal View History

2012-06-30 05:43:42 -07:00
import decimal
import httplib
import json
2012-06-30 04:47:08 -07:00
class Exchanger:
2012-06-30 05:43:42 -07:00
def __init__(self, quote_currencies, refresh_balance):
self.refresh_balance = refresh_balance
self.quote_currencies = {}
2012-06-30 04:47:08 -07:00
def exchange(self, btc_amount, quote_currency):
2012-06-30 05:43:42 -07:00
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
2012-06-30 04:47:08 -07:00
2012-06-30 05:43:42 -07:00
def lookup_rate(self, response, quote_id):
return decimal.Decimal(response[str(quote_id)]["last"])
2012-06-30 04:47:08 -07:00
if __name__ == "__main__":
exch = Exchanger(("EUR", "USD", "GBP"))
print exch.exchange(1, "EUR")