summaryrefslogtreecommitdiff
path: root/jobs/telexoo.py
blob: 110ba72cab845fe95fc8293940944a86953fb336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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"))