From 4b1ba931091448b4e5d980cb0695b4a8aa85b459 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 17 Oct 2017 16:38:35 +0200 Subject: [PATCH 1/3] [modules/currency] Use markets.ft.com instead of fixer.io --- bumblebee/modules/currency.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 81d533e..b0ace93 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -14,7 +14,7 @@ Parameters: the base symbol and the rate list * currency.destinationdelimiter: Delimiter used for separating individual rates (defaults to "|") -Note: source and destination names right now must correspond to the names used by the API of http://fixer.io +Note: source and destination names right now must correspond to the names used by the API of https://markets.ft.com """ import bumblebee.input @@ -32,6 +32,8 @@ SYMBOL = { "GBP": u"£", "EUR": u"€", "USD": u"$" } +API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}" + class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config, @@ -40,7 +42,7 @@ class Module(bumblebee.engine.Module): self._data = {} self._interval = int(self.parameter("interval", 1)) self._base = self.parameter("source", "GBP") - self._symbols = self.parameter("destination", "USD,EUR") + self._symbols = self.parameter("destination", "USD,EUR").split(",") self._nextcheck = 0 def price(self, widget): @@ -48,8 +50,8 @@ class Module(bumblebee.engine.Module): return "?" rates = [] - for sym in self._data["rates"]: - rates.append(u"{}{}".format(self._data["rates"][sym], SYMBOL[sym] if sym in SYMBOL else sym)) + for sym, rate in self._data.items(): + rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym)) basefmt = u"{}".format(self.parameter("sourceformat", "{}: {}")) ratefmt = u"{}".format(self.parameter("destinationdelimiter", "|")) @@ -61,10 +63,12 @@ class Module(bumblebee.engine.Module): 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._data = json.loads(requests.get(url).text) - except Exception: - pass + for symbol in self._symbols: + url = API_URL.format(self._base, symbol) + try: + response = requests.get(url).json() + self._data[symbol] = response['data']['exchangeRate'] + except Exception: + pass # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From 1eb49ab53eab523dbf851580c5a3bb66402c4baf Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 17 Oct 2017 16:44:05 +0200 Subject: [PATCH 2/3] [modules/currency] Use a list for the data to preserve the order --- bumblebee/modules/currency.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index b0ace93..cba7247 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -39,18 +39,18 @@ class Module(bumblebee.engine.Module): super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.price) ) - self._data = {} + self._data = [] self._interval = int(self.parameter("interval", 1)) self._base = self.parameter("source", "GBP") self._symbols = self.parameter("destination", "USD,EUR").split(",") self._nextcheck = 0 def price(self, widget): - if self._data == {}: + if len(self._data) == 0: return "?" rates = [] - for sym, rate in self._data.items(): + for sym, rate in self._data: rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym)) basefmt = u"{}".format(self.parameter("sourceformat", "{}: {}")) @@ -61,13 +61,13 @@ class Module(bumblebee.engine.Module): def update(self, widgets): timestamp = int(time.time()) if self._nextcheck < int(time.time()): - self._data = {} + self._data = [] self._nextcheck = int(time.time()) + self._interval*60 for symbol in self._symbols: url = API_URL.format(self._base, symbol) try: response = requests.get(url).json() - self._data[symbol] = response['data']['exchangeRate'] + self._data.append((symbol, response['data']['exchangeRate'])) except Exception: pass From d341c90c538e55445ad4c33210a3715af56d4337 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 17 Oct 2017 19:13:26 +0200 Subject: [PATCH 3/3] [modules/currency] Add symbol for JPY --- bumblebee/modules/currency.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index cba7247..d2d9033 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -29,7 +29,7 @@ except ImportError: pass SYMBOL = { - "GBP": u"£", "EUR": u"€", "USD": u"$" + "GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥" } API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}"