From 45af287c26bf12d5df355f23ac99178539bac5e3 Mon Sep 17 00:00:00 2001 From: fortinm Date: Fri, 7 Mar 2025 23:47:56 -0500 Subject: [PATCH] Fix issue 3879 Signed-off-by: fortinm --- .../source/montreal_ca.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/montreal_ca.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/montreal_ca.py index 6bd4cf3b7..f0473b22b 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/montreal_ca.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/montreal_ca.py @@ -15,7 +15,11 @@ URL = "https://montreal.ca/info-collectes" TEST_CASES = { "Lasalle": {"sector": "LSL4"}, - "Mercier-Hochelaga": {"sector": "MHM_42-5_B"}, + "Mercier-Hochelaga": { + "sector": "MHM_42-5_A", + "food": "MHM-42-S", + "recycling": "MHM-42-S" + }, "Ahuntsic": {"sector": "AC-2"}, "Rosemont": { "sector": "RPP-RE-22-OM", @@ -183,7 +187,7 @@ def parse_collection(self, source_type, schedule_message): break # Stop searching if the day is found # These happens weekly - if source_type in ["Waste", "Food", "Recycling", "Bulky"]: + if not re.search(r'(?:every\s+(?:.*)week|of the month)', schedule_message, re.IGNORECASE): # Iterate through each month and day, and handle the "out of range" error for month in range(1, 13): for day in range(1, 32): @@ -273,11 +277,19 @@ def parse_collection(self, source_type, schedule_message): # Splitting the string by ',' and 'and' to extract individual numbers line = line.replace(";", "") line = line.replace(".", "") + line = line.replace(":", "") + line = line.replace("*", "") try: days_in_month = re.search( rf"\b{month}(.*){MONTH_PATTERN}", line, re.IGNORECASE - ).group(0) + ) + if not days_in_month: + days_in_month = re.search( + rf"(?:\s*{month} {year})(.*)", line, re.IGNORECASE + ).group(1) + else: + days_in_month = days_in_month.group(1) days_in_month = re.split(r", | and ", days_in_month) days_in_month = [ @@ -293,7 +305,7 @@ def parse_collection(self, source_type, schedule_message): date = datetime(year, MONTHS[month], day) days.append(date.date()) # break - except Exception: + except Exception as e: LOGGER.debug("No dates found in string.") break @@ -325,8 +337,9 @@ def get_data_by_source(self, source_type, url): # Not implemented yet pass else: - schedule_message = feature["properties"]["MESSAGE_EN"] - entries += self.parse_collection(source_type, schedule_message) + if feature["properties"]["MESSAGE_EN"]: + schedule_message = feature["properties"]["MESSAGE_EN"] + entries += self.parse_collection(source_type, schedule_message) return entries