Merge pull request #192 from fredj/currency_use_ft

[modules/current] Use markets.ft.com instead of fixer.io
This commit is contained in:
tobi-wan-kenobi 2017-10-17 20:20:06 +02:00 committed by GitHub
commit 851be18d72

View file

@ -14,7 +14,7 @@ Parameters:
the base symbol and the rate list the base symbol and the rate list
* currency.destinationdelimiter: Delimiter used for separating individual rates (defaults to "|") * 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 import bumblebee.input
@ -28,27 +28,29 @@ except ImportError:
pass pass
SYMBOL = { 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={}"
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
super(Module, self).__init__(engine, config, super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.price) bumblebee.output.Widget(full_text=self.price)
) )
self._data = {} self._data = []
self._interval = int(self.parameter("interval", 1)) self._interval = int(self.parameter("interval", 1))
self._base = self.parameter("source", "GBP") self._base = self.parameter("source", "GBP")
self._symbols = self.parameter("destination", "USD,EUR") self._symbols = self.parameter("destination", "USD,EUR").split(",")
self._nextcheck = 0 self._nextcheck = 0
def price(self, widget): def price(self, widget):
if self._data == {}: if len(self._data) == 0:
return "?" return "?"
rates = [] rates = []
for sym in self._data["rates"]: for sym, rate in self._data:
rates.append(u"{}{}".format(self._data["rates"][sym], SYMBOL[sym] if sym in SYMBOL else sym)) rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym))
basefmt = u"{}".format(self.parameter("sourceformat", "{}: {}")) basefmt = u"{}".format(self.parameter("sourceformat", "{}: {}"))
ratefmt = u"{}".format(self.parameter("destinationdelimiter", "|")) ratefmt = u"{}".format(self.parameter("destinationdelimiter", "|"))
@ -58,12 +60,14 @@ class Module(bumblebee.engine.Module):
def update(self, widgets): def update(self, widgets):
timestamp = int(time.time()) timestamp = int(time.time())
if self._nextcheck < int(time.time()): if self._nextcheck < int(time.time()):
self._data = {} self._data = []
self._nextcheck = int(time.time()) + self._interval*60 self._nextcheck = int(time.time()) + self._interval*60
url = "http://api.fixer.io/latest?symbols={}&base={}".format(self._symbols, self._base) for symbol in self._symbols:
try: url = API_URL.format(self._base, symbol)
self._data = json.loads(requests.get(url).text) try:
except Exception: response = requests.get(url).json()
pass self._data.append((symbol, response['data']['exchangeRate']))
except Exception:
pass
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4