forked from belidzs/mnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnb.py
67 lines (54 loc) · 2.06 KB
/
mnb.py
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
from zeep import Client
from lxml import etree
"""
Várt válasz:
<MNBCurrentExchangeRates>
<Day date="2019-02-22">
<Rate unit="1" curr="AUD">199,16000</Rate>
<Rate unit="1" curr="BGN">162,51000</Rate>
</Day>
</MNBCurrentExchangeRates>
"""
class MNBClient:
def __init__(self):
self.wsdl_url = "http://www.mnb.hu/arfolyamok.asmx?wsdl"
self.client = Client(self.wsdl_url)
# lekérés - aktuális napi MNB árfolyamok
# Retrive current MNB daily exchange rates
def get_currencies(self, start_date, end_date, currency_names):
result = self.client.service.GetExchangeRates(start_date,end_date,currency_names)
return result
# Retrieve USD exchange rates of a period (2022-01-01 - 2022-12-28)
def get_exchange_rates(self):
result = self.client.service.GetCurrentExchangeRates()
return result
class XMLParser:
def parse_rates(self, xml_data):
root = etree.fromstring(xml_data)
rates = {}
for day in root.findall('Day'):
date = day.get('date')
rate = day.find('Rate').text
rates[date] = rate
return rates
# innentől kézi XML feldolgozás, mert az MNB lusta volt XSD sémát mellékelni a servicehez
def parse_currencies(self, xml_data):
root = etree.fromstring(xml_data)
datum = root[0].attrib['date']
print('Dátum: {0}'.format(datum))
print('Deviza\tEgység\tÁrfolyam')
for currency in root[0]:
devizanem = currency.attrib['curr']
arfolyam = float(currency.text.replace(',', '.'))
egyseg = int(currency.attrib['unit'])
print('{0}\t{1}\t{2}'.format(devizanem, egyseg, arfolyam))
# Create a client for interacting with the MNB web service
mnb_client = MNBClient()
xml_parser = XMLParser()
result = mnb_client.get_exchange_rates()
xml_parser.parse_currencies(result)
result = mnb_client.get_currencies("2022-01-01", "2022-12-28", "USD")
rates = xml_parser.parse_rates(result)
# Print the result
for date, rate in rates.items():
print(f"{date}: {rate}")