diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 488eebb..068a6f6 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -8,6 +8,10 @@ Requires the following python packages: Parameters: * currency.interval: Interval in minutes between updates, default is 1. + * currency.source: Source currency (defaults to "GBP") + * currency.destination: Comma-separated list of destination currencies (defaults to "USD,EUR") + +Note: source and destination names right now must correspond to the names used by the API of http://fixer.io """ import bumblebee.input @@ -21,40 +25,40 @@ try: except ImportError: pass +SYMBOL = { + "GBP": u"£", "EUR": u"€", "USD": u"$" +} + class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.price) ) - self._price = "-" - self._interval = int(self.parameter("interval", "1")) + self._data = {} + self._interval = int(self.parameter("interval", 1)) + self._base = self.parameter("source", "GBP") + self._symbols = self.parameter("destination", "USD,EUR") self._nextcheck = 0 - self._valid = False def price(self, widget): - if not self._valid: - return u"?" - return self._price + if self._data == {}: + return "?" + + rates = [] + for sym in self._data["rates"]: + rates.append(u"{}{}".format(self._data["rates"][sym], SYMBOL[sym] if sym in SYMBOL else sym)) + + return u"{}: {}".format(SYMBOL[self._base] if self._base in SYMBOL else self._base, u"/".join(rates)) def update(self, widgets): timestamp = int(time.time()) if self._nextcheck < int(time.time()): + self._data = {} + self._nextcheck = int(time.time()) + self._interval*60 + url = "http://api.fixer.io/latest?symbols={}&base={}".format(self._symbols, self._base) try: - self._nextcheck = int(time.time()) + self._interval*60 - price_url = "http://api.fixer.io/latest?symbols=USD,EUR&base=GBP" - try: - price_json = json.loads( requests.get(price_url).text ) - gbpeur = str(price_json['rates']['EUR']) - gbpusd = str(price_json['rates']['USD']) - except ValueError: - gbpeur = "-" - gbpusd = "-" - - self._price = u"£/€ " + gbpeur + u" | £/$ " + gbpusd - self._valid = True - except RequestException: - self._price = u"£/€ - | £/$ -" - self._valid = True + self._data = json.loads(requests.get(url).text) + except Exception: + pass # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 -