From 9984158ec193982ee4015696d3c10056196f77c1 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Thu, 17 Feb 2022 16:33:44 -0700 Subject: [PATCH] Ensure valid str->float conv in currency calc Currency amounts returned by google seem to randomly include unicode chars ('\xa0' noted in #642) which broke the currency calculator included in the project. This ensures that only strings that can be converted to float are ever used in the conversion. Fixes #642 --- app/utils/results.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/utils/results.py b/app/utils/results.py index 3f7ceed..48aa857 100644 --- a/app/utils/results.py +++ b/app/utils/results.py @@ -242,6 +242,9 @@ def check_currency(response: str) -> dict: currency2 = currency_link[1].text currency1 = currency1.rstrip('=').split(' ', 1) currency2 = currency2.split(' ', 1) + + # Handle differences in currency formatting + # i.e. "5.000" vs "5,000" if currency2[0][-3] == ',': currency1[0] = currency1[0].replace('.', '') currency1[0] = currency1[0].replace(',', '.') @@ -250,10 +253,17 @@ def check_currency(response: str) -> dict: else: currency1[0] = currency1[0].replace(',', '') currency2[0] = currency2[0].replace(',', '') - return {'currencyValue1': float(currency1[0]), - 'currencyLabel1': currency1[1], - 'currencyValue2': float(currency2[0]), - 'currencyLabel2': currency2[1] + + currency1_value = float(re.sub(r'[^\d\.]', '', currency1[0])) + currency1_label = currency1[1] + + currency2_value = float(re.sub(r'[^\d\.]', '', currency2[0])) + currency2_label = currency2[1] + + return {'currencyValue1': currency1_value, + 'currencyLabel1': currency1_label, + 'currencyValue2': currency2_value, + 'currencyLabel2': currency2_label } return {}