corrected offline error to be catched.

This commit is contained in:
Ivan Hörler 2018-01-11 19:21:13 +01:00
parent 6cf9d77cae
commit b02e8c1343
3 changed files with 180 additions and 170 deletions

View File

@ -24,117 +24,121 @@ def get_exchange_rate():
# ~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~
today = datetime.now().strftime("%Y-%m-%d") today = datetime.now().strftime("%Y-%m-%d")
SNB_URL = 'https://www.snb.ch/selector/de/mmr/exfeed/rss' SNB_URL = 'https://www.snb.ch/selector/de/mmr/exfeed/rss'
urlsocket = urllib.request.urlopen(SNB_URL) urlsocket = ''
root = ET.parse(urlsocket) try:
root = ET.ElementTree(root) urlsocket = urllib.request.urlopen(SNB_URL)
except urllib.error.URLError as e:
print('err: urllib.request.urlopen: ', e.reason)
if urlsocket:
root = ET.parse(urlsocket)
root = ET.ElementTree(root)
# ~~~~~~~~~~~~~~~~~~~~~
# development block:
# ~~~~~~~~~~~~~~~~~~~~~
# today = "2018-01-08"
# try:
# root = ET.ElementTree(file='rss')
# except Exception as e:
# print('exchange_rates.py_urlsocket failed %s (
# %s) on date: %s for %s'
# % (e, type(e), root))
# ~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~ # Namespaces
# development block: ns = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
# ~~~~~~~~~~~~~~~~~~~~~ 'none': 'http://purl.org/rss/1.0/',
# today = "2018-01-03" 'dc': 'http://purl.org/dc/elements/1.1/',
# try: 'dcterms': 'http://purl.org/dc/terms/',
# root = ET.ElementTree(file='rss') 'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/'
# except Exception as e: }
# print('exchange_rates.py_urlsocket failed %s ( # Pathvariables to XML Namespaces
# %s) on date: %s for %s' rate_path = 'cb:statistics/cb:exchangeRate/'
# % (e, type(e), root)) observation_path = 'cb:statistics/cb:exchangeRate/cb:observation/'
# ~~~~~~~~~~~~~~~~~~~~~ exchange_rates = {}
for item in root.findall('none:item', ns):
# for eatch item n the list we grab the release date
# to evaluate if its fresh data or old:
# Namespaces # THE CURRENCY DATE:
ns = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', datetime_str = item.find('dc:date', ns).text
'none': 'http://purl.org/rss/1.0/', # convert string to date object:
'dc': 'http://purl.org/dc/elements/1.1/', # https://stackoverflow.com/a/12282040/4061870
'dcterms': 'http://purl.org/dc/terms/', # seams like snb striked the microsecond somewhere
'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/' # between Nov. and Dez. 2017 so maybe first check
} # time type is with milliseconds:
# Pathvariables to XML Namespaces
rate_path = 'cb:statistics/cb:exchangeRate/'
observation_path = 'cb:statistics/cb:exchangeRate/cb:observation/'
exchange_rates = {}
for item in root.findall('none:item', ns):
# for eatch item n the list we grab the release date
# to evaluate if its fresh data or old:
# THE CURRENCY DATE:
datetime_str = item.find('dc:date', ns).text
# convert string to date object:
# https://stackoverflow.com/a/12282040/4061870
# seams like snb striked the microsecond somewhere
# between Nov. and Dez. 2017 so maybe first check
# time type is with milliseconds:
try:
date = datetime.strptime(''.join(
datetime_str.rsplit(':', 1)),
"%Y-%m-%dT%H:%M:%S%z").strftime(
"%Y-%m-%d")
except Exception as e:
print('%s (%s)' % (e, type(e)))
try: try:
date = datetime.strptime(''.join( date = datetime.strptime(''.join(
datetime_str.rsplit(':', 1)), datetime_str.rsplit(':', 1)),
"%Y-%m-%dT%H:%M:%S.%f%z").strftime( "%Y-%m-%dT%H:%M:%S%z").strftime(
"%Y-%m-%d") "%Y-%m-%d")
continue
except Exception as e: except Exception as e:
print('%s (%s)' % (e, type(e))) print('%s (%s)' % (e, type(e)))
continue try:
# Print dates for development: date = datetime.strptime(''.join(
# print("date:", date, "today:", today) datetime_str.rsplit(':', 1)),
# only the values of today are used so check for date in XML: "%Y-%m-%dT%H:%M:%S.%f%z").strftime(
if date == today: "%Y-%m-%d")
# now search for the currency exchange rate: continue
target_currency = item.find(rate_path + except Exception as e:
'cb:targetCurrency', ns).text print('%s (%s)' % (e, type(e)))
value = float(item.find(observation_path + continue
'cb:value', ns).text) # Print dates for development:
value = float(value) # convert to float # print("date:", date, "today:", today)
foreign_value = value # copy to new value to have both. # only the values of today are used so check for date in XML:
if date == today:
# now search for the currency exchange rate:
target_currency = item.find(rate_path +
'cb:targetCurrency', ns).text
value = float(item.find(observation_path +
'cb:value', ns).text)
value = float(value) # convert to float
foreign_value = value # copy to new value to have both.
if item.find(observation_path + 'cb:unit_mult', ns) is None: if item.find(observation_path + 'cb:unit_mult', ns) is None:
# because it's dangerous to check for present, # because it's dangerous to check for present,
# i check for none here and have to set the target # i check for none here and have to set the target
# to 1. as im multiplying it later. # to 1. as im multiplying it later.
unit_mult = float("1.0") unit_mult = float("1.0")
else:
# shift left by 2 digits with "/"
# https://stackoverflow.com/questions/8362792/
# because some currencys differ widly from CHF
unit_mult = item.find(observation_path +
'cb:unit_mult', ns).text
# unit_mult defaults to '0' so we check for 8 decimal
# values (2..-6) they represent the fracton value to
# calculate the correct decimalpoint.
if unit_mult == '2': # thinking of Bitcoins
unit_mult = '0.01'
if unit_mult == '1':
unit_mult = '0.10'
if unit_mult == '-1':
unit_mult = '10'
if unit_mult == '-2': # Japan Yen
unit_mult = '100'
if unit_mult == '-3':
unit_mult = '1000'
if unit_mult == '-4':
unit_mult = '10000'
if unit_mult == '-5':
unit_mult = '100000'
if unit_mult == '-6': # indian rupies
unit_mult = '1000000'
unit_mult = float(unit_mult) # convert to float
# calculate the Currency to CHF:
foreign_value = 1 / value
foreign_value *= unit_mult
value = value / unit_mult
# truncate it to decimal values provided by the xml:
foreign_value_round = round(foreign_value, 5)
# Print nice setup of all calculated currencys for development:
# print("date:", date, " 1 ", target_currency, " costs: ",
# CHFvalue, "CHF and 1 ", base_currency, " costs: ",
# FOREIGNvalue_round, target_currency)
exchange_rates.update(
{target_currency: foreign_value_round})
# Print the Dictionary:
# print(exchange_rates)
else: else:
# shift left by 2 digits with "/" continue
# https://stackoverflow.com/questions/8362792/ return(exchange_rates, today)
# because some currencys differ widly from CHF
unit_mult = item.find(observation_path +
'cb:unit_mult', ns).text
# unit_mult defaults to '0' so we check for 8 decimal
# values (2..-6) they represent the fracton value to
# calculate the correct decimalpoint.
if unit_mult == '2': # thinking of Bitcoins
unit_mult = '0.01'
if unit_mult == '1':
unit_mult = '0.10'
if unit_mult == '-1':
unit_mult = '10'
if unit_mult == '-2': # Japan Yen
unit_mult = '100'
if unit_mult == '-3':
unit_mult = '1000'
if unit_mult == '-4':
unit_mult = '10000'
if unit_mult == '-5':
unit_mult = '100000'
if unit_mult == '-6': # indian rupies
unit_mult = '1000000'
unit_mult = float(unit_mult) # convert to float
# calculate the Currency to CHF:
foreign_value = 1 / value
foreign_value *= unit_mult
value = value / unit_mult
# truncate it to decimal values provided by the xml:
foreign_value_round = round(foreign_value, 5)
# Print nice setup of all calculated currencys for development:
# print("date:", date, " 1 ", target_currency, " costs: ",
# CHFvalue, "CHF and 1 ", base_currency, " costs: ",
# FOREIGNvalue_round, target_currency)
exchange_rates.update(
{target_currency: foreign_value_round})
# Print the Dictionary:
# print(exchange_rates)
else:
break
return(exchange_rates, today)

View File

@ -14,13 +14,13 @@
<tr> <tr>
<th scope="col">DATE</th> <th scope="col">DATE</th>
<th scope="col">RATE</th> <th scope="col">RATE</th>
<tr> </tr>
{% for currency in currency_USD_list %} {% for currency in currency_USD_list %}
<tr>
<td scope="col">{{ currency.date.date }}</td> <td scope="col">{{ currency.date.date }}</td>
<td scope="col">{{ currency.exchange_rate_to_chf }}</td> <td scope="col">{{ currency.exchange_rate_to_chf }}</td>
</tr>
{% endfor %} {% endfor %}
<tr>
</table> </table>
{% else %} {% else %}
<p class="alert"> <p class="alert">
@ -34,13 +34,13 @@
<tr> <tr>
<th scope="col">DATE</th> <th scope="col">DATE</th>
<th scope="col">RATE</th> <th scope="col">RATE</th>
<tr> </tr>
{% for currency in currency_EUR_list %} {% for currency in currency_EUR_list %}
<tr>
<td scope="col">{{ currency.date.date }}</td> <td scope="col">{{ currency.date.date }}</td>
<td scope="col">{{ currency.exchange_rate_to_chf }}</td> <td scope="col">{{ currency.exchange_rate_to_chf }}</td>
</tr>
{% endfor %} {% endfor %}
<tr>
</table> </table>
{% else %} {% else %}
<p class="alert"> <p class="alert">
@ -54,11 +54,12 @@
<tr> <tr>
<th scope="col">DATE</th> <th scope="col">DATE</th>
<th scope="col">RATE</th> <th scope="col">RATE</th>
<tr> </tr>
{% for currency in currency_JPY_list %} {% for currency in currency_JPY_list %}
<tr>
<td scope="col">{{ currency.date.date }}</td> <td scope="col">{{ currency.date.date }}</td>
<td scope="col">{{ currency.exchange_rate_to_chf }}</td> <td scope="col">{{ currency.exchange_rate_to_chf }}</td>
</tr>
{% endfor %} {% endfor %}
<tr> <tr>
</table> </table>
@ -74,11 +75,12 @@
<tr> <tr>
<th scope="col">DATE</th> <th scope="col">DATE</th>
<th scope="col">RATE</th> <th scope="col">RATE</th>
<tr> </tr>
{% for currency in currency_GBP_list %} {% for currency in currency_GBP_list %}
<tr>
<td scope="col">{{ currency.date.date }}</td> <td scope="col">{{ currency.date.date }}</td>
<td scope="col">{{ currency.exchange_rate_to_chf }}</td> <td scope="col">{{ currency.exchange_rate_to_chf }}</td>
</tr>
{% endfor %} {% endfor %}
<tr> <tr>
</table> </table>

View File

@ -12,67 +12,71 @@ def currencies(request):
# prepares the view all dynamicaly. # prepares the view all dynamicaly.
# It can grow in terms of more Currencies over time automaticaly. # It can grow in terms of more Currencies over time automaticaly.
today = '' today = ''
raw_data = '' raw_data = []
raw_data, today = exchange_rates.get_exchange_rate() try:
print('views raw_data: ', raw_data) # assert False raw_data, today = exchange_rates.get_exchange_rate()
except Exception as e:
print('views raw_data: ', raw_data, 'error:', e) # assert False
message_no = "Already querried today: " message_no = "Already querried today: "
message_yes = " Updated successfully: " message_yes = " Updated successfully: "
for currency, rate in raw_data.items(): if raw_data:
if ExchangeRate.objects.filter( print(raw_data)
date__date=today, for currency, rate in raw_data.items():
name__name=currency): if ExchangeRate.objects.filter(
message_no += currency + ", " date__date=today,
# A: https://stackoverflow.com/a/27802801/4061870 name__name=currency):
else: message_no += currency + ", "
if ExchangeRate_date.objects.filter(date=today)[:1]: # A: https://stackoverflow.com/a/27802801/4061870
try:
# lustigerweise gibt .values() den value und die id
# zurück. Ohne .values() gibts nur den "value"
date_dict = ExchangeRate_date.objects.filter(
date=today).values()
except Exception as e:
print('exdate_exists %s (%s) on %s'
% (e, type(e), today))
else: else:
if ExchangeRate_date.objects.filter(date=today)[:1]:
try:
# lustigerweise gibt .values() den value und die id
# zurück. Ohne .values() gibts nur den "value"
date_dict = ExchangeRate_date.objects.filter(
date=today).values()
except Exception as e:
print('exdate_exists %s (%s) on %s'
% (e, type(e), today))
else:
try:
exdate = ExchangeRate_date.objects.create(
date=today)
exdate.save()
except Exception as e:
print('exdate_not_exists %s (%s) for %s'
% (e, type(e), today))
if ExchangeRate_name.objects.filter(
name=currency)[:1]:
try:
name_dict = ExchangeRate_name.objects.filter(
name=currency).values()
except Exception as e:
print('exname_exists %s (%s) on %s'
% (e, type(e), currency))
else:
try:
exname = ExchangeRate_name.objects.create(
name=currency)
exname.save()
except Exception as e:
print('exname_not_exists %s (%s) on %s'
% (e, type(e), currency))
try: try:
exdate = ExchangeRate_date.objects.create( exrate = ExchangeRate.objects.create(
date=today) # name_id=name_id,
exdate.save() name_id=ExchangeRate_name.objects.get(
except Exception as e: name=currency).id,
print('exdate_not_exists %s (%s) for %s' # date_id=date_id,
% (e, type(e), today)) date_id=ExchangeRate_date.objects.get(
if ExchangeRate_name.objects.filter( date=today).id,
name=currency)[:1]: exchange_rate_to_chf=rate,
try: )
name_dict = ExchangeRate_name.objects.filter( exrate.save()
name=currency).values() message_yes += currency + ", "
except Exception as e:
print('exname_exists %s (%s) on %s'
% (e, type(e), currency))
else:
try:
exname = ExchangeRate_name.objects.create(
name=currency)
exname.save()
except Exception as e:
print('exname_not_exists %s (%s) on %s'
% (e, type(e), currency))
try:
exrate = ExchangeRate.objects.create(
# name_id=name_id,
name_id=ExchangeRate_name.objects.get(
name=currency).id,
# date_id=date_id,
date_id=ExchangeRate_date.objects.get(
date=today).id,
exchange_rate_to_chf=rate,
)
exrate.save()
message_yes += currency + ", "
except Exception as e: except Exception as e:
print('exrate_create %s (%s) on %s for %s' print('exrate_create %s (%s) on %s for %s'
% (e, type(e), currency, today)) % (e, type(e), currency, today))
# prepare messages: # prepare messages:
# python can not swap a char insinde a sting so i have # python can not swap a char insinde a sting so i have