summaryrefslogtreecommitdiff
path: root/jobs/transferwise.py
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2018-07-17 12:34:04 +0200
committerYves Fischer <yvesf-git@xapek.org>2018-07-17 12:49:49 +0200
commit69c220cda3d8c0a95327630f5752dad36cb82261 (patch)
treefad9fbe78cf717f4dd17b8e9d996ab9a54b7d3e2 /jobs/transferwise.py
downloaddatasources-69c220cda3d8c0a95327630f5752dad36cb82261.tar.gz
datasources-69c220cda3d8c0a95327630f5752dad36cb82261.zip
Squashed commit
Diffstat (limited to 'jobs/transferwise.py')
-rwxr-xr-xjobs/transferwise.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/jobs/transferwise.py b/jobs/transferwise.py
new file mode 100755
index 0000000..a2e30d1
--- /dev/null
+++ b/jobs/transferwise.py
@@ -0,0 +1,55 @@
+import codecs
+import json
+import random
+import re
+import urllib.parse
+import urllib.request
+from collections import namedtuple
+
+APP_URL = "https://transferwise.com/fr/"
+URL = "https://transferwise.com/api/v1/payment/calculate"
+UA = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0"
+
+Data = namedtuple('Data', ['curr_from', 'curr_to', 'rate'])
+
+def get_token():
+ request = urllib.request.Request(APP_URL)
+ request.add_header("User-Agent", UA)
+
+ with urllib.request.urlopen(request) as f:
+ f2 = codecs.getreader('utf-8')(f)
+ for line in f2.readlines():
+ m = re.match(".*config.appToken.*'(.+)'.*", line)
+ if m:
+ g = m.groups()
+ return g[0]
+
+
+def job(currFrom, currTo):
+ token = get_token()
+ MULT = random.randint(100, 100000)
+ data = urllib.parse.urlencode({
+ 'amount': str(MULT),
+ 'amountCurrency': 'source',
+ 'hasDiscount': 'false',
+ 'isFixedRate': 'false',
+ 'isGuaranteedFixedTarget': 'false',
+ 'sourceCurrency': currFrom,
+ 'targetCurrency': currTo,
+ })
+ # print (URL + "?" + data)
+ request = urllib.request.Request(URL + "?" + data)
+ request.add_header("X-Authorization-key", token)
+ request.add_header("X-Authorization-token", "")
+ request.add_header("User-Agent", UA)
+ with urllib.request.urlopen(request) as f:
+ f2 = codecs.getreader('utf-8')(f)
+ response = json.load(f2)
+ return Data(currFrom, currTo, float(response['transferwiseRate']))
+
+
+if __name__ == "__main__":
+ from pprint import pprint
+
+ pprint(job("CHF", "EUR"))
+ pprint(job("CHF", "GBP"))