From 69c220cda3d8c0a95327630f5752dad36cb82261 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Tue, 17 Jul 2018 12:34:04 +0200 Subject: Squashed commit --- jobs/telexoo.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 jobs/telexoo.py (limited to 'jobs/telexoo.py') 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")) -- cgit v1.2.1