Skip to content

Commit 16d9123

Browse files
committed
A script to update exchange rates stored on statusdb
1 parent 7d411af commit 16d9123

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

update_exchange_rates.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
"""A script to update the exchange rates for 1 EUR in SEK and 1 USD om SEK.
3+
4+
"""
5+
6+
import argparse
7+
import requests
8+
import re
9+
import yaml
10+
from couchdb import Server
11+
import datetime
12+
13+
14+
def main(config, push_to_server=False):
15+
resp = requests.get("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
16+
if not resp.ok:
17+
raise Exception("Got a non-ok return code: {} from ECB. ABORTING".format(resp.status_code))
18+
19+
# Look for the four lines we need using regexes.
20+
# I guess normal people would use an xml-parser
21+
for line in resp.text.split('\n'):
22+
date_r = r".*time='([0-9\-]*).*'"
23+
date_match = re.match(date_r, line)
24+
if date_match:
25+
date = re.findall(date_r, line)[0]
26+
27+
name_r = r'.*:name>(.*)<.*'
28+
name_match = re.match(name_r, line)
29+
if name_match:
30+
source_name = re.findall(name_r, line)[0]
31+
32+
usd_match = re.match(r".*currency='USD'.*", line)
33+
if usd_match:
34+
rate_match = re.findall(r".*rate='([0-9\.]*)'.*", line)
35+
eur_to_usd = float(rate_match[0])
36+
37+
sek_match = re.match(r".*currency='SEK'.*", line)
38+
if sek_match:
39+
rate_match = re.findall(r".*rate='([0-9\.]*)'.*", line)
40+
eur_to_sek = float(rate_match[0])
41+
42+
usd_to_sek = round(eur_to_sek/eur_to_usd, 4)
43+
44+
# Create the doc that will be uploaded
45+
doc = {}
46+
doc['Issued at'] = datetime.datetime.now().isoformat()
47+
doc['Data source date'] = date
48+
doc['Data source'] = source_name
49+
doc['USD_in_SEK'] = usd_to_sek
50+
doc['EUR_in_SEK'] = eur_to_sek
51+
52+
if push_to_server:
53+
with open(config) as settings_file:
54+
server_settings = yaml.load(settings_file)
55+
couch = Server(server_settings.get("couch_server", None))
56+
db = couch['pricing_exchange_rates']
57+
58+
db.save(doc)
59+
else:
60+
print(doc)
61+
62+
63+
if __name__ == '__main__':
64+
parser = argparse.ArgumentParser(description=__doc__)
65+
parser.add_argument('--statusdb_config', required=True,
66+
help='The genomics-status settings.yaml file.')
67+
parser.add_argument('--push', action='store_true', help='Use this tag to '
68+
"make the script push the changes to statusdb")
69+
70+
args = parser.parse_args()
71+
main(args.statusdb_config, push_to_server=args.push)

0 commit comments

Comments
 (0)