From 6fc32e57107abded98543a4a5f2dd2f56906b5f8 Mon Sep 17 00:00:00 2001 From: myTselection Date: Sat, 7 Sep 2024 23:00:25 +0200 Subject: [PATCH] #56 handle case of missing official nl date --- custom_components/carbu_com/manifest.json | 2 +- custom_components/carbu_com/utils.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/custom_components/carbu_com/manifest.json b/custom_components/carbu_com/manifest.json index 9017b68..fd891de 100644 --- a/custom_components/carbu_com/manifest.json +++ b/custom_components/carbu_com/manifest.json @@ -9,5 +9,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/myTselection/carbu_com/issues", "requirements": ["bs4","requests","voluptuous", "ratelimit"], - "version": "11.2.1" + "version": "11.2.2" } diff --git a/custom_components/carbu_com/utils.py b/custom_components/carbu_com/utils.py index 1da16d7..29abc3a 100644 --- a/custom_components/carbu_com/utils.py +++ b/custom_components/carbu_com/utils.py @@ -897,6 +897,7 @@ def getFuelOfficialNl(self, fueltype: FuelType, country): soup = BeautifulSoup(response.text, 'html.parser') + date_text = None for paragraph in soup.find_all('p', class_='text-xs'): if paragraph.text.strip().startswith("Datum overzicht"): date_text = paragraph.text.replace("Datum overzicht ", "").strip() @@ -924,7 +925,7 @@ def translate_month(dutch_date_str): return f"{spaceSplit[0]} {month_translation.get(spaceSplit[1], spaceSplit[1])} {spaceSplit[2]}" # Convert the date text to a datetime.date object if found - date_object = None + date_object = date.today() if date_text: try: # Translate Dutch month to English @@ -933,7 +934,7 @@ def translate_month(dutch_date_str): date_object = datetime.strptime(translated_date_text, "%d %B %Y").date() except ValueError: # Handle cases where the date format is unexpected or incorrect - date_object = None + date_object = date.today() # Assuming 'soup' is the BeautifulSoup object containing your HTML data = {}