diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2018-07-17 12:34:04 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2018-07-17 12:49:49 +0200 |
commit | 69c220cda3d8c0a95327630f5752dad36cb82261 (patch) | |
tree | fad9fbe78cf717f4dd17b8e9d996ab9a54b7d3e2 /jobs/telexoo.py | |
download | datasources-69c220cda3d8c0a95327630f5752dad36cb82261.tar.gz datasources-69c220cda3d8c0a95327630f5752dad36cb82261.zip |
Squashed commit
Diffstat (limited to 'jobs/telexoo.py')
-rwxr-xr-x | jobs/telexoo.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/jobs/telexoo.py b/jobs/telexoo.py new file mode 100755 index 0000000..110ba72 --- /dev/null +++ b/jobs/telexoo.py @@ -0,0 +1,55 @@ +import codecs +import json +import random +import re +import urllib.parse +import urllib.request +from collections import namedtuple +from decimal import Decimal + +from currencies.config import * + +URL = "https://telexoo.tegona.com/convert/" + +Quote = namedtuple('Quote', ['curr_from', 'curr_to', 'rate']) + + +def execute(curr_from, curr_to): + MULT = random.randint(1000, 9999) + CURRENCY = { + MONEY_CURRENCY_EUR: "EUR", + MONEY_CURRENCY_CHF: "CHF", + MONEY_CURRENCY_USD: "USD", + MONEY_CURRENCY_GBP: "GBP", + MONEY_CURRENCY_PLN: "PLN" + } + curr_from = CURRENCY[curr_from] + curr_to = CURRENCY[curr_to] + params = urllib.parse.urlencode({ + 's1': curr_from, + 's2': curr_to, + 'amount': str(MULT), + 'action': 'sell', + 'language': 'en', + 'verbose': '0', + }) + request = urllib.request.Request(URL + "?" + params) + request.add_header( + "User-Agent", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; FSL 7.0.6.01001)") + with urllib.request.urlopen(request) as f: + f2 = codecs.getreader('utf-8')(f) + response = json.load(f2) + result_raw = response[0]['result'].replace(",", "") + match = re.match("^{} ([0-9\.]*)$".format(curr_to), result_raw) + if not match: + raise Exception("Invalid response in 'result' field") + result = Decimal(match.groups()[0]) / MULT + return Quote(curr_to, curr_to, float(result)) + + +if __name__ == "__main__": + from pprint import pprint + + pprint(execute("CHF", "EUR")) + pprint(execute("CHF", "GBP")) |