From 975027820f259c82fc65866c0c03850cc236189a Mon Sep 17 00:00:00 2001 From: Antonis Karamitros Date: Thu, 7 Sep 2017 12:54:29 +0100 Subject: [PATCH] Add an fx module to track GBP/EUR and GBP/USD --- bumblebee/modules/fx.py | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 bumblebee/modules/fx.py diff --git a/bumblebee/modules/fx.py b/bumblebee/modules/fx.py new file mode 100644 index 0000000..30aace1 --- /dev/null +++ b/bumblebee/modules/fx.py @@ -0,0 +1,51 @@ +# -*- coding: UTF-8 -*- +# pylint: disable=C0111,R0903 + +import bumblebee.input +import bumblebee.output +import bumblebee.engine +import json +import time +try: + import requests + from requests.exceptions import RequestException +except ImportError: + pass + +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._nextcheck = 0 + self._valid = False + + def price(self, widget): + if not self._valid: + return u"?" + return self._price + + def update(self, widgets): + timestamp = int(time.time()) + if self._nextcheck < int(time.time()): + 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 = "£/€ " + gbpeur + " | £/$ " + gbpusd + self._valid = True + except RequestException: + self._price = "£/€ - | £/$ -" + self._valid = True + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 +