Created
September 1, 2017 13:48
-
-
Save teror4uks/b55617b20212b2bf11ef420569b29edb to your computer and use it in GitHub Desktop.
CentralBankCurrency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import urllib3 | |
class Valute: | |
def __init__(self, node): | |
self.node = node | |
for n in node.iter(): | |
if "ID" in n.attrib: | |
self.id = n.attrib["ID"] | |
elif n.tag == "NumCode": | |
self.numcode = n.text | |
elif n.tag == "CharCode": | |
self.charcode = n.text | |
elif n.tag == "Nominal": | |
self.nominal = n.text | |
elif n.tag == "Name": | |
self.name = n.text | |
elif n.tag == "Value": | |
self.value = n.text | |
def __str__(self): | |
return "ID: {0}\n" \ | |
"NumCode: {1}\n" \ | |
"CharCode: {2}\n" \ | |
"Nominal: {3}\n" \ | |
"Name: {4}\n" \ | |
"Value: {5}".format(self.id, | |
self.numcode, | |
self.charcode, | |
self.nominal, | |
self.name, | |
self.value) | |
class CentralBankCurrency: | |
def __init__(self, url='http://www.cbr.ru/scripts/XML_daily.asp', METHOD="GET"): | |
http = urllib3.PoolManager() | |
self.url = url | |
self.METHOD = METHOD | |
self.request = http.request(self.METHOD, url) | |
self.root = ET.fromstring(self.request.data) | |
self.valutes = {} | |
for v in self.root.findall('Valute'): | |
valute = Valute(v) | |
self.valutes[valute.charcode] = valute | |
super(CentralBankCurrency, self).__setattr__(valute.charcode.lower(), valute) | |
if __name__ == "__main__": | |
c = CentralBankCurrency() | |
print(c.jpy) | |
print(c.eur.value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment