From 28747c1b31b7322f5a907dd7757442387f979b3b Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Sat, 18 Nov 2023 13:06:57 +0100 Subject: [PATCH] fix bromley_gov_uk --- .../source/bromley_gov_uk.py | 64 ++++++++----------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/bromley_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/bromley_gov_uk.py index b7e59e7b9..e9b7899a1 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/bromley_gov_uk.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/bromley_gov_uk.py @@ -1,70 +1,56 @@ -import re -from datetime import datetime +import time import requests from waste_collection_schedule import Collection # type: ignore[attr-defined] +from waste_collection_schedule.service.ICS import ICS TITLE = "London Borough of Bromley" DESCRIPTION = "Source for bromley.gov.uk services for London Borough of Bromley, UK." URL = "https://bromley.gov.uk" TEST_CASES = { - "Test_001": {"property": "6328436"}, + "Test_001": {"property": 6328436}, "Test_002": {"property": "6146611"}, - "Test_003": {"property": 6328436}, + "Test_003": {"property": 6283460}, } ICON_MAP = { - "NON-RECYCLABLE REFUSE": "mdi:trash-can", - "FOOD WASTE": "mdi:food", - "GARDEN WASTE": "mdi:leaf", - "PAPER & CARDBOARD": "mdi:newspaper", - "MIXED RECYCLING (CANS, PLASTICS & GLASS)": "mdi:glass-fragile", + "NON-RECYCLABLE": "mdi:trash-can", + "FOOD": "mdi:food", + "GARDEN": "mdi:leaf", + "PAPER": "mdi:newspaper", + "MIXED": "mdi:glass-fragile", } -REGEX_TITLES = r"([A-za-z;&\-\(\), ]*)[<\/h3]" -REGEX_NEXT = r"([a-zA-Z]*, [0-9]{1,2}[a-z]{2} [A-Za-z]*)[\n]" -REGEX_LAST = r"([a-zA-Z]*, [0-9]{1,2}[a-z]{2} [A-Za-z]*),\sat\s*[0-9]{1,2}:[0-9]{2}" -REGEX_ORDINALS = r"(st|nd|rd|th) " +MAX_COUNT = 15 class Source: def __init__(self, property): self._property = str(property) + self._ics = ICS() def fetch(self): - - today = datetime.now() - today = today.replace(hour=0, minute=0, second=0, microsecond=0) - yr = int(today.year) - s = requests.Session() r = s.get(f"https://recyclingservices.bromley.gov.uk/waste/{self._property}") - r.raise_for_status() - waste_col = re.findall(REGEX_TITLES, r.text) - next_col = re.findall(REGEX_NEXT, r.text) - last_col = re.findall(REGEX_LAST, r.text) - - # website doesn't include the year, so lets add them - # and try and deal with year-end transitions - date_collection = [] - for (t, n, l) in zip(waste_col, next_col, last_col): - x = t.replace("&", "&") - d = re.compile(REGEX_ORDINALS).sub("", n.split(", ")[1]) - if "December" in l and "January" in n: - d = d + str(yr + 1) - else: - d = d + str(yr) - d = datetime.strptime(d, "%d%B%Y").date() - date_collection.append([x, d]) + for _ in range(MAX_COUNT): + r = s.get( + f"https://recyclingservices.bromley.gov.uk/waste/{self._property}/calendar.ics" + ) + try: + dates = self._ics.convert(r.text) + break + except ValueError: + time.sleep(2) # identical to website behaviour (hx-trigger="every 2s") entries = [] - for item in date_collection: + for item in dates: + bin_type = item[1].replace(" collection", "") entries.append( Collection( - date=item[1], - t=item[0], - icon=ICON_MAP.get(item[0].upper()), + date=item[0], + t=bin_type, + icon=ICON_MAP.get(bin_type.split(" ")[0].upper()), ) )