-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
131 lines (102 loc) · 4.45 KB
/
converter.py
File metadata and controls
131 lines (102 loc) · 4.45 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from xml.etree import ElementTree as ET
import requests
import uuid
from decimal import Decimal
import calendar
BASE_URL = 'https://sdw-wsrest.ecb.europa.eu/service/data'
GENERIC = '{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}'
MESSAGE = '{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message}'
def converter(from_currency_list, to_currency_list, from_date, to_date):
base = 'EUR'
headers = {
"authorization": "Basic ZXV1c2VybmFtZTpwYXNzd29yZA=="
}
currencies = {}
form_currency_string = '+'.join(from_currency_list)
from_date = substract_time_if_needed(from_date)
for to in to_currency_list:
if to != base:
form_currency_string += "+" + to
url = BASE_URL+'/EXR/D.'+form_currency_string+'..SP00.A?detail=dataonly&startPeriod=' + from_date + '&endPeriod='\
+ to_date
print(url)
res = requests.get(url, headers=headers)
if res.status_code == 404:
raise Exception('the wanted Currencies was not found')
if not res.text:
raise Exception('no values in this dates')
tree = ET.fromstring(res.text)
data = tree.find(MESSAGE + 'DataSet')
for series in data:
from_currency = ''
currency_metadata = series.find(GENERIC + 'SeriesKey')
for key in currency_metadata:
key_id = key.attrib.get('id')
if 'CURRENCY' == key_id:
from_currency = key.attrib.get('value')
currencies[from_currency] = {}
obslist = series.findall(GENERIC + 'Obs')
for obs in obslist:
rate_date = obs.find(GENERIC + 'ObsDimension')
rate_value = obs.find(GENERIC + 'ObsValue')
date = rate_date.attrib.get('value')
value = rate_value.attrib.get('value')
currencies[from_currency][date] = value
print(currencies)
build_xml_result(currencies, from_currency_list, to_currency_list)
def substract_time_if_needed(from_date):
from datetime import timedelta, datetime
from_datetime = datetime.strptime(from_date, '%Y-%m-%d')
result = from_datetime
curr_day = calendar.day_name[from_datetime.weekday()]
if 'Saturday' in curr_day:
result = from_datetime - timedelta(days=1)
elif 'Sunday' in curr_day:
result = from_datetime - timedelta(days=2)
return str(result.date())
def build_xml_result(currencies, from_currency_list, to_currency_list):
from datetime import date
result = ET.Element('data', attrib={'id': str(uuid.uuid4()), 'date': str(date.today())})
for from_cur in from_currency_list:
for to_cur in to_currency_list:
if from_cur is to_cur:
continue
series_result = ET.SubElement(result, 'currency')
series_result.set('from', from_cur)
series_result.set('to', to_cur)
if from_cur is 'EUR':
for date in currencies[to_cur]:
val = str(currencies[to_cur][date])
day_result = ET.SubElement(series_result, 'day')
day_result.set('time', date)
day_result.set('value', val)
elif to_cur is 'EUR':
for date in currencies[from_cur]:
val = str(Decimal(1) / Decimal(currencies[from_cur][date]))
day_result = ET.SubElement(series_result, 'day')
day_result.set('time', date)
day_result.set('value', val)
else:
for date in currencies[from_cur]:
val = str(Decimal(1) / Decimal(currencies[from_cur][date]) * Decimal(currencies[to_cur][date]))
day_result = ET.SubElement(series_result, 'day')
day_result.set('time', date)
day_result.set('value', val)
indent(result)
result_tree = ET.ElementTree(result)
result_tree.write("file.xml", xml_declaration=True, encoding='utf-8', method="xml")
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
converter(['ILS', 'USD'], ['JPY', 'EUR'], '2020-06-18', '2020-06-21')