Infer currency from local ip address
This commit is contained in:
parent
7d989a6fca
commit
ff74e8377d
1 changed files with 40 additions and 6 deletions
|
@ -8,7 +8,7 @@ Requires the following python packages:
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
* currency.interval: Interval in minutes between updates, default is 1.
|
* currency.interval: Interval in minutes between updates, default is 1.
|
||||||
* currency.source: Source currency (defaults to "GBP")
|
* currency.source: Source currency (defaults to "GBP"). Set to "auto" infer the local one.
|
||||||
* currency.destination: Comma-separated list of destination currencies (defaults to "USD,EUR")
|
* currency.destination: Comma-separated list of destination currencies (defaults to "USD,EUR")
|
||||||
* currency.sourceformat: String format for source formatting; Defaults to "{}: {}" and has two variables,
|
* currency.sourceformat: String format for source formatting; Defaults to "{}: {}" and has two variables,
|
||||||
the base symbol and the rate list
|
the base symbol and the rate list
|
||||||
|
@ -28,6 +28,8 @@ except ImportError:
|
||||||
SYMBOL = {
|
SYMBOL = {
|
||||||
"GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥"
|
"GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥"
|
||||||
}
|
}
|
||||||
|
DEFAULT_DEST = "USD,EUR,GBP"
|
||||||
|
DEFAULT_SRC = "GBP"
|
||||||
|
|
||||||
API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}"
|
API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}"
|
||||||
|
|
||||||
|
@ -39,20 +41,30 @@ class Module(bumblebee.engine.Module):
|
||||||
self._data = []
|
self._data = []
|
||||||
self.interval_factor(60)
|
self.interval_factor(60)
|
||||||
self.interval(1)
|
self.interval(1)
|
||||||
self._base = self.parameter("source", "GBP")
|
|
||||||
self._symbols = self.parameter("destination", "USD,EUR").split(",")
|
|
||||||
self._nextcheck = 0
|
self._nextcheck = 0
|
||||||
|
|
||||||
|
src = self.parameter("source", "auto")
|
||||||
|
if src == "auto":
|
||||||
|
self._base = self.find_local_currency()
|
||||||
|
else:
|
||||||
|
self._base = src
|
||||||
|
|
||||||
|
dest = [d for d in self.parameter("destination", DEFAULT_DEST).split(",")
|
||||||
|
if d != self._base]
|
||||||
|
self._symbols = dest
|
||||||
|
|
||||||
|
|
||||||
def price(self, widget):
|
def price(self, widget):
|
||||||
if len(self._data) == 0:
|
if len(self._data) == 0:
|
||||||
return "?"
|
return "?"
|
||||||
|
|
||||||
rates = []
|
rates = []
|
||||||
for sym, rate in self._data:
|
for sym, rate in self._data:
|
||||||
rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym))
|
rate = float(rate)
|
||||||
|
rates.append(u"{:.3g}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym))
|
||||||
|
|
||||||
basefmt = u"{}".format(self.parameter("sourceformat", "{}: {}"))
|
basefmt = u"{}".format(self.parameter("sourceformat", "1{}={}"))
|
||||||
ratefmt = u"{}".format(self.parameter("destinationdelimiter", "|"))
|
ratefmt = u"{}".format(self.parameter("destinationdelimiter", "="))
|
||||||
|
|
||||||
return basefmt.format(SYMBOL[self._base] if self._base in SYMBOL else self._base, ratefmt.join(rates))
|
return basefmt.format(SYMBOL[self._base] if self._base in SYMBOL else self._base, ratefmt.join(rates))
|
||||||
|
|
||||||
|
@ -66,4 +78,26 @@ class Module(bumblebee.engine.Module):
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def find_local_currency(self):
|
||||||
|
'''Use geolocation lookup to find local currency'''
|
||||||
|
try:
|
||||||
|
r = requests.get('https://ipvigilante.com/')
|
||||||
|
if not r.ok: return DEFAULT_SRC
|
||||||
|
dt = r.json()
|
||||||
|
if dt['status'] != 'success':
|
||||||
|
return DEFAULT_SRC
|
||||||
|
country = dt['data']['country_name']
|
||||||
|
|
||||||
|
r = requests.get("https://raw.githubusercontent.com/samayo/country-json/master/src/country-by-currency-code.json")
|
||||||
|
if not r.ok: return DEFAULT_SRC
|
||||||
|
data = r.json()
|
||||||
|
country2curr = {}
|
||||||
|
for dt in data:
|
||||||
|
country2curr[dt['country']] = dt['currency_code']
|
||||||
|
|
||||||
|
return country2curr.get(country, DEFAULT_SRC)
|
||||||
|
except:
|
||||||
|
return DEFAULT_SRC
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue