From fc5184fa778b3dc450f806616cece1c684d41556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Tue, 19 Dec 2017 22:33:00 +0100 Subject: [PATCH] get currency from SNB rss to work --- .../didgeridoo/currencies/exchange_rates.py | 19 +- .../templates/currencies/index.html | 2 +- django/didgeridoo/currencies/views.py | 25 +- django/didgeridoo/rss | 621 ++++++++++++++++++ 4 files changed, 649 insertions(+), 18 deletions(-) create mode 100644 django/didgeridoo/rss diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py index ebbbb12..2900892 100644 --- a/django/didgeridoo/currencies/exchange_rates.py +++ b/django/didgeridoo/currencies/exchange_rates.py @@ -20,15 +20,17 @@ def get_exchange_rate(): # ~~~~~~~~~~~~~~~~~~~~~ # Online Resource block: # ~~~~~~~~~~~~~~~~~~~~~ - # SNB_URL = 'https://www.snb.ch/selector/de/mmr/exfeed/rss' - # urlsocket = urllib.request.urlopen(SNB_URL) - # root = ET.parse(urlsocket) - # root = ET.ElementTree(root) + SNB_URL = 'https://www.snb.ch/selector/de/mmr/exfeed/rss' + urlsocket = urllib.request.urlopen(SNB_URL) + root = ET.parse(urlsocket) + root = ET.ElementTree(root) # ~~~~~~~~~~~~~~~~~~~~~ # development block: # ~~~~~~~~~~~~~~~~~~~~~ - root = ET.ElementTree(file='rss') + # root = ET.ElementTree(file='rss') + # ~~~~~~~~~~~~~~~~~~~~~ + # Namespaces ns = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'none': 'http://purl.org/rss/1.0/', 'dc': 'http://purl.org/dc/elements/1.1/', @@ -45,7 +47,7 @@ def get_exchange_rate(): xml_datestring = xml_datetime_string.split('T')[0] # parse string to date object: xml_date = datetime.date(datetime.strptime(xml_datestring, "%Y-%m-%d")) - + exchange_rates = {} for item in root.findall('none:item', ns): # THE CURRENCY DATE: datetime_str = item.find('dc:date', ns).text @@ -107,11 +109,12 @@ def get_exchange_rate(): # print("date:", date, " 1 ", target_currency, " costs: ", # CHFvalue, "CHF and 1 ", base_currency, " costs: ", # FOREIGNvalue_round, target_currency) - exchange_rates = {target_currency: FOREIGNvalue_round} + exchange_rates.update( + {target_currency: FOREIGNvalue_round}) # Print the Dictionary: # print(exchange_rates) - return(exchange_rates) else: break + return(exchange_rates) # for development its preferable to see that the for loop is done: # print('no more fresh data!') diff --git a/django/didgeridoo/currencies/templates/currencies/index.html b/django/didgeridoo/currencies/templates/currencies/index.html index edfd456..3c1e8a7 100644 --- a/django/didgeridoo/currencies/templates/currencies/index.html +++ b/django/didgeridoo/currencies/templates/currencies/index.html @@ -4,7 +4,7 @@
-

Music Shop

+

Currencies in CHF

{% if currency_list %}