Move logic into dedicated functions
This commit is contained in:
parent
6ad4a559d3
commit
2ed8a53f3d
1 changed files with 24 additions and 19 deletions
|
@ -36,6 +36,26 @@ DEFAULT_SRC_FALLBACK = "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={}"
|
||||||
|
|
||||||
|
|
||||||
|
def get_local_country():
|
||||||
|
r = requests.get('https://ipvigilante.com/')
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
class Module(bumblebee.engine.Module):
|
||||||
def __init__(self, engine, config):
|
def __init__(self, engine, config):
|
||||||
super(Module, self).__init__(engine, config,
|
super(Module, self).__init__(engine, config,
|
||||||
|
@ -84,25 +104,10 @@ class Module(bumblebee.engine.Module):
|
||||||
def find_local_currency(self):
|
def find_local_currency(self):
|
||||||
'''Use geolocation lookup to find local currency'''
|
'''Use geolocation lookup to find local currency'''
|
||||||
try:
|
try:
|
||||||
r = requests.get('https://ipvigilante.com/')
|
country = get_local_country()
|
||||||
if not r.ok:
|
currency_map = load_country_to_currency()
|
||||||
return DEFAULT_SRC_FALLBACK
|
return currency_map.get(country, DEFAULT_SRC_FALLBACK)
|
||||||
dt = r.json()
|
except:
|
||||||
if dt['status'] != 'success':
|
|
||||||
return DEFAULT_SRC_FALLBACK
|
|
||||||
country = dt['data']['country_name']
|
|
||||||
|
|
||||||
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.get(country, DEFAULT_SRC)
|
|
||||||
except Exception as e:
|
|
||||||
return DEFAULT_SRC_FALLBACK
|
return DEFAULT_SRC_FALLBACK
|
||||||
|
|
||||||
def fmt_rate(self, rate):
|
def fmt_rate(self, rate):
|
||||||
|
|
Loading…
Reference in a new issue