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")
|
|
|
|
|
|
|
|
Note: source and destination names right now must correspond to the names used by the API of http://fixer.io
|
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
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
from requests.exceptions import RequestException
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2017-09-10 09:34:02 +02:00
|
|
|
SYMBOL = {
|
|
|
|
"GBP": u"£", "EUR": u"€", "USD": u"$"
|
|
|
|
}
|
|
|
|
|
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-09-10 09:34:02 +02:00
|
|
|
self._data = {}
|
|
|
|
self._interval = int(self.parameter("interval", 1))
|
|
|
|
self._base = self.parameter("source", "GBP")
|
|
|
|
self._symbols = self.parameter("destination", "USD,EUR")
|
2017-09-07 13:54:29 +02:00
|
|
|
self._nextcheck = 0
|
|
|
|
|
|
|
|
def price(self, widget):
|
2017-09-10 09:34:02 +02:00
|
|
|
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))
|
2017-09-07 13:54:29 +02:00
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
timestamp = int(time.time())
|
|
|
|
if self._nextcheck < int(time.time()):
|
2017-09-10 09:34:02 +02:00
|
|
|
self._data = {}
|
|
|
|
self._nextcheck = int(time.time()) + self._interval*60
|
|
|
|
url = "http://api.fixer.io/latest?symbols={}&base={}".format(self._symbols, self._base)
|
2017-09-07 13:54:29 +02:00
|
|
|
try:
|
2017-09-10 09:34:02 +02:00
|
|
|
self._data = json.loads(requests.get(url).text)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2017-09-07 13:54:29 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|