2017-09-07 13:54:29 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2017-09-07 16:19:37 +02:00
|
|
|
"""Displays currency exchange rates. Currently, displays currency between GBP and USD/EUR only.
|
|
|
|
|
|
|
|
Requires the following python packages:
|
|
|
|
* requests
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* currency.interval: Interval in minutes between updates, default is 1.
|
2017-09-10 09:34:02 +02:00
|
|
|
* currency.source: Source currency (defaults to "GBP")
|
|
|
|
* currency.destination: Comma-separated list of destination currencies (defaults to "USD,EUR")
|
2017-09-10 18:37:11 +02:00
|
|
|
* currency.sourceformat: String format for source formatting; Defaults to "{}: {}" and has two variables,
|
|
|
|
the base symbol and the rate list
|
|
|
|
* currency.destinationdelimiter: Delimiter used for separating individual rates (defaults to "|")
|
2017-09-10 09:34:02 +02:00
|
|
|
|
2017-10-17 16:38:35 +02:00
|
|
|
Note: source and destination names right now must correspond to the names used by the API of https://markets.ft.com
|
2017-09-07 16:22:30 +02:00
|
|
|
"""
|
2017-09-07 16:19:37 +02:00
|
|
|
|
2017-09-07 13:54:29 +02:00
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2017-09-10 09:34:02 +02:00
|
|
|
SYMBOL = {
|
2017-10-17 19:13:26 +02:00
|
|
|
"GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥"
|
2017-09-10 09:34:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-17 16:38:35 +02:00
|
|
|
API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}"
|
|
|
|
|
2017-09-07 13:54:29 +02:00
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
super(Module, self).__init__(engine, config,
|
|
|
|
bumblebee.output.Widget(full_text=self.price)
|
|
|
|
)
|
2017-10-17 16:44:05 +02:00
|
|
|
self._data = []
|
2018-01-07 20:27:11 +01:00
|
|
|
self.interval(1)
|
2017-09-10 09:34:02 +02:00
|
|
|
self._base = self.parameter("source", "GBP")
|
2017-10-17 16:38:35 +02:00
|
|
|
self._symbols = self.parameter("destination", "USD,EUR").split(",")
|
2017-09-07 13:54:29 +02:00
|
|
|
self._nextcheck = 0
|
|
|
|
|
|
|
|
def price(self, widget):
|
2017-10-17 16:44:05 +02:00
|
|
|
if len(self._data) == 0:
|
2017-09-10 09:34:02 +02:00
|
|
|
return "?"
|
|
|
|
|
|
|
|
rates = []
|
2017-10-17 16:44:05 +02:00
|
|
|
for sym, rate in self._data:
|
2017-10-17 16:38:35 +02:00
|
|
|
rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym))
|
2017-09-10 09:34:02 +02:00
|
|
|
|
2017-09-10 18:37:11 +02:00
|
|
|
basefmt = u"{}".format(self.parameter("sourceformat", "{}: {}"))
|
|
|
|
ratefmt = u"{}".format(self.parameter("destinationdelimiter", "|"))
|
|
|
|
|
|
|
|
return basefmt.format(SYMBOL[self._base] if self._base in SYMBOL else self._base, ratefmt.join(rates))
|
2017-09-07 13:54:29 +02:00
|
|
|
|
|
|
|
def update(self, widgets):
|
2018-01-07 20:27:11 +01:00
|
|
|
self._data = []
|
|
|
|
for symbol in self._symbols:
|
|
|
|
url = API_URL.format(self._base, symbol)
|
|
|
|
try:
|
|
|
|
response = requests.get(url).json()
|
|
|
|
self._data.append((symbol, response['data']['exchangeRate']))
|
|
|
|
except Exception:
|
|
|
|
pass
|
2017-09-07 13:54:29 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|