Skip to content

Commit

Permalink
[plugin usd] change to use apilayer
Browse files Browse the repository at this point in the history
The original currency conversion API data.fixer.io redirects already to apilayer.com and I couldn't find
a way to create API keys for the former data.fixer.io API, so I updated the url and passing of parameters. Especially the api key is passed now as header instead as a url param what's certainly reasonable for security.

apilayer blocks urllib requests as bot requests (for at least for the free account).  apilayers blocks urllib requests even with different user-agents.  So, the solution is to add more headers like described in
https://stackoverflow.com/questions/13303449/urllib2-httperror-http-error-403-forbidden
  • Loading branch information
hanfried committed Sep 9, 2022
1 parent 226d428 commit fa89248
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions plugins/usd.py
Expand Up @@ -7,6 +7,7 @@
import json

vd.option('fixer_key', '', 'API Key for fixer.io')
vd.option('fixer_currency_cache_days', 1, 'Cache days for currency conversions')

currency_symbols = {
'$': 'USD',
Expand All @@ -19,9 +20,25 @@
'₫': 'VND',
}

def currency_rates_json(date='latest'):
url = 'http://data.fixer.io/api/%s?access_key=%s' % (date, vd.options.fixer_key)
return vd.urlcache(url).read_text()
def currency_rates_json(date='latest', base='USD'):
url = 'https://api.apilayer.com/fixer/%s?base=%s' % (date, base)
return vd.urlcache(
url,
days=vd.options.fixer_currency_cache_days,
headers={
# First need to set some additional headers as otherwise apilayers will block it with a 403
# See also https://stackoverflow.com/questions/13303449/urllib2-httperror-http-error-403-forbidden
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',

# Finally set Apikey
'apikey': vd.options.fixer_key
}
).read_text()

@functools.lru_cache()
def currency_rates():
Expand All @@ -37,6 +54,7 @@ def currency_multiplier(src_currency, dest_currency):
usd_mult = eur_usd_mult/eur_src_mult
if dest_currency == 'USD':
return usd_mult

return usd_mult/currency_rates()[dest_currency]

def USD(s):
Expand Down

0 comments on commit fa89248

Please sign in to comment.