From 29b4b1ac90bfe54f1d7d83a3b3e4e1a1305332fa Mon Sep 17 00:00:00 2001 From: Kamil Dudka Date: Mon, 28 May 2018 14:21:35 +0200 Subject: [PATCH] Resolves: #1574835 - make units_cur work again --- units_cur | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/units_cur b/units_cur index 5541355..d00c12c 100755 --- a/units_cur +++ b/units_cur @@ -5,14 +5,17 @@ from __future__ import absolute_import, division, print_function # # -version = '4.1' +version = '4.2' -# 30 October 2017 +# Version 4.2: 18 April 2018 +# +# Handle case of empty/malformed entry returned from the server +# +# Version 4.1: 30 October 2017 # # Fixed to include USD in the list of currency codes. # -# Version 4 -# 2 October 2017 +# Version 4: 2 October 2017 # # Complete rewrite to use Yahoo YQL API due to removal of TimeGenie RSS feed. # Switched to requests library using JSON. One program now runs under @@ -258,22 +261,27 @@ except requests.exceptions.RequestException as e: format(e)) exit(1) -rates = [ data['resource']['fields']['price'] for data in webdata] -codes = [ data['resource']['fields']['symbol'][0:3] for data in webdata] rate_index = 1 - -for (code,rate) in zip(codes,rates): - if code not in currency.keys(): - if (verbose): - stderr.write('Got unknown currency with code {}\n'.format(code)) - else: - if not currency[code][rate_index]: - currency[code][rate_index] = '1|{} US$'.format(rate) - elif verbose: - stderr.write('Got value "{}" for currency "{}" but ' - 'it is already defined\n'.format(rate, code)) - +for data in webdata: + entry = data['resource']['fields'] + if 'price' not in entry or 'symbol' not in entry: # Skip empty/bad entries + if verbose: + stderr.write('Got bad entry from server: '+str(entry)+'\n') + else: + rate = entry['price'] + code = entry['symbol'][0:3] + if code not in currency.keys(): + if (verbose): + stderr.write('Got unknown currency with code {}\n'.format(code)) + else: + if not currency[code][rate_index]: + currency[code][rate_index] = '1|{} US$'.format(rate) + elif verbose: + stderr.write('Got value "{}" for currency "{}" but ' + 'it is already defined\n'.format(rate, code)) + + # Delete currencies where we have no rate data for code in currency.keys(): if not currency[code][rate_index]: -- 2.14.3