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.
|
2019-11-24 14:18:44 +01:00
|
|
|
* currency.source: Source currency (ex. "GBP", "EUR"). Defaults to "auto", which infers the local one from IP address.
|
2017-09-10 09:34:02 +02:00
|
|
|
* 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
|
2019-11-24 15:06:58 +01:00
|
|
|
try:
|
|
|
|
from babel.numbers import format_currency
|
|
|
|
except ImportError:
|
|
|
|
format_currency = None
|
2019-11-24 14:33:52 +01:00
|
|
|
import json
|
|
|
|
import os
|
2017-09-07 13:54:29 +02:00
|
|
|
|
2017-09-10 09:34:02 +02:00
|
|
|
SYMBOL = {
|
2019-11-24 14:16:35 +01:00
|
|
|
"GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥", "KRW": u"₩"
|
2017-09-10 09:34:02 +02:00
|
|
|
}
|
2019-11-24 15:06:58 +01:00
|
|
|
DEFAULT_DEST = "USD,EUR,auto"
|
|
|
|
DEFAULT_SRC = "GBP"
|
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={}"
|
2019-11-24 14:41:17 +01:00
|
|
|
LOCATION_URL = "https://ipvigilante.com/"
|
2017-10-17 16:38:35 +02:00
|
|
|
|
2019-11-24 14:38:50 +01:00
|
|
|
|
|
|
|
def get_local_country():
|
2019-11-24 14:41:17 +01:00
|
|
|
r = requests.get(LOCATION_URL)
|
2019-11-24 14:38:50 +01:00
|
|
|
location = r.json()
|
|
|
|
return location['data']['country_name']
|
|
|
|
|
|
|
|
|
|
|
|
def load_country_to_currency():
|
|
|
|
fname = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'data', 'country-by-currency-code.json')
|
|
|
|
with open(fname, 'r') as f:
|
|
|
|
data = json.load(f)
|
|
|
|
country2curr = {}
|
|
|
|
for dt in data:
|
|
|
|
country2curr[dt['country']] = dt['currency_code']
|
|
|
|
|
|
|
|
return country2curr
|
|
|
|
|
|
|
|
|
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 = []
|
2019-10-29 20:20:19 +01:00
|
|
|
self.interval_factor(60)
|
2018-01-07 20:27:11 +01:00
|
|
|
self.interval(1)
|
2017-09-07 13:54:29 +02:00
|
|
|
self._nextcheck = 0
|
|
|
|
|
2019-11-24 14:16:35 +01:00
|
|
|
src = self.parameter("source", DEFAULT_SRC)
|
2019-11-24 14:09:24 +01:00
|
|
|
if src == "auto":
|
|
|
|
self._base = self.find_local_currency()
|
|
|
|
else:
|
|
|
|
self._base = src
|
|
|
|
|
2019-11-24 15:06:04 +01:00
|
|
|
self._symbols = []
|
|
|
|
for d in self.parameter("destination", DEFAULT_DEST).split(","):
|
|
|
|
if d == 'auto':
|
|
|
|
new = self.find_local_currency()
|
|
|
|
else:
|
|
|
|
new = d
|
|
|
|
if new != self._base:
|
|
|
|
self._symbols.append(new)
|
2019-11-24 14:09:24 +01:00
|
|
|
|
2017-09-07 13:54:29 +02:00
|
|
|
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:
|
2019-11-24 15:06:58 +01:00
|
|
|
rate_float = float(rate.replace(',',''))
|
|
|
|
if format_currency:
|
|
|
|
rates.append(format_currency(rate_float, sym))
|
|
|
|
else:
|
|
|
|
rate = self.fmt_rate(rate)
|
|
|
|
rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym))
|
2017-09-10 09:34:02 +02:00
|
|
|
|
2019-11-24 15:06:58 +01:00
|
|
|
basefmt = u"{}".format(self.parameter("sourceformat", "{}={}"))
|
2019-11-24 14:09:24 +01:00
|
|
|
ratefmt = u"{}".format(self.parameter("destinationdelimiter", "="))
|
2017-09-10 18:37:11 +02:00
|
|
|
|
2019-11-24 15:06:58 +01:00
|
|
|
if format_currency:
|
|
|
|
base_val = format_currency(1, self._base)
|
|
|
|
else:
|
|
|
|
base_val = '1{}'.format(SYMBOL[self._base] if self._base in SYMBOL else self._base)
|
|
|
|
|
|
|
|
return basefmt.format(base_val, 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
|
|
|
|
2019-11-24 14:09:24 +01:00
|
|
|
def find_local_currency(self):
|
|
|
|
'''Use geolocation lookup to find local currency'''
|
|
|
|
try:
|
2019-11-24 14:38:50 +01:00
|
|
|
country = get_local_country()
|
|
|
|
currency_map = load_country_to_currency()
|
2019-11-24 15:06:58 +01:00
|
|
|
return currency_map.get(country, DEFAULT_SRC)
|
2019-11-24 14:38:50 +01:00
|
|
|
except:
|
2019-11-24 15:06:58 +01:00
|
|
|
return DEFAULT_SRC
|
2019-11-24 14:09:24 +01:00
|
|
|
|
2019-11-24 14:16:35 +01:00
|
|
|
def fmt_rate(self, rate):
|
|
|
|
float_rate = float(rate.replace(',', ''))
|
|
|
|
if not 0.01 < float_rate < 100:
|
2019-11-24 15:06:58 +01:00
|
|
|
ret = rate
|
2019-11-24 14:16:35 +01:00
|
|
|
else:
|
2019-11-24 15:06:58 +01:00
|
|
|
ret = "%.3g" % float_rate
|
|
|
|
|
|
|
|
return ret
|
2019-11-24 14:09:24 +01:00
|
|
|
|
2017-09-07 13:54:29 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|