diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/cheshire_east_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/cheshire_east_gov_uk.py index 2f6e01e1c..b9abf0ba6 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/cheshire_east_gov_uk.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/cheshire_east_gov_uk.py @@ -1,82 +1,79 @@ -import logging - from datetime import datetime -from bs4 import BeautifulSoup import requests -import urllib.parse +from bs4 import BeautifulSoup from waste_collection_schedule import Collection TITLE = "cheshireeast.gov.uk" -DESCRIPTION = ( - "Source for cheshireeast.gov.uk services for Cheshire East" -) +DESCRIPTION = "Source for cheshireeast.gov.uk services for Cheshire East" URL = "cheshireeast.gov.uk" -TEST_CASES = { - "houseUPRN" : {"uprn": "100010132071"}, - "houseAddress": {"postcode":"WA16 0AY", "name_number": "1"} -} -API_URLS = { - "address_search": "https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/Search", - "collection": "https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/GetBartecJobList?uprn={}", + +TEST_CASES = { + "houseUPRN": {"uprn": "100010132071"}, + "houseAddress": {"postcode": "WA16 0AY", "name_number": "1"}, } -ICONS = { +ICON_MAP = { "General Waste": "mdi:trash-can", "Mixed Recycling": "mdi:recycle", "Garden Waste": "mdi:leaf", } -_LOGGER = logging.getLogger(__name__) - class Source: def __init__(self, uprn=None, postcode=None, name_number=None): self._uprn = uprn self._postcode = postcode - self.name_number = name_number + self._name_number = name_number def fetch(self): session = requests.Session() - responseContent=None - if(self._postcode and self.name_number): + if self._postcode and self._name_number: # Lookup postcode and number to get UPRN - r = session.get(f"https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/Search?postcode={urllib.parse.quote(self._postcode.encode('utf8'))}&propertyname={self.name_number}") + params = { + "postcode": self._postcode, + "propertyname": self._name_number, + } + r = session.get( + "https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/Search", + params=params, + ) + r.raise_for_status() soup = BeautifulSoup(r.text, features="html.parser") s = soup.find("a", attrs={"class": "get-job-details"}) - self._uprn = s['data-uprn'] + print(s) + if s is None: + raise Exception("address not found") + self._uprn = s["data-uprn"] + + if self._uprn is None: + raise Exception("uprn not set") + + params = {"uprn": self._uprn} + r = session.get( + "https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/GetBartecJobList", + params=params, + ) + r.raise_for_status() - if self._uprn: - r = session.get(f"https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/GetBartecJobList?uprn={self._uprn}") - responseContent = r.text - - soup = BeautifulSoup(responseContent, features="html.parser") + soup = BeautifulSoup(r.text, features="html.parser") s = soup.find_all("td", attrs={"class": "visible-cell"}) entries = [] for cell in s: labels = cell.find_all("label") - date = None - type = None if labels: - for i,label in enumerate(labels): - if(i == 1): - date = datetime.strptime(label.text, "%d/%m/%Y") - if(i == 2): - for round_type in ICONS: - if round_type.upper() in label.text.upper(): - type = round_type + date = datetime.strptime(labels[1].text, "%d/%m/%Y").date() + type = labels[2].text.removeprefix("Empty Standard ") entries.append( - Collection( - date = date.date(), - t = type, - icon = ICONS.get(type), - ) + Collection( + date=date, + t=type, + icon=ICON_MAP.get(type), + ) ) - - return entries - + return entries diff --git a/doc/source/cheshire_east_gov_uk.md b/doc/source/cheshire_east_gov_uk.md index 11f29d9ce..3f2c899c8 100644 --- a/doc/source/cheshire_east_gov_uk.md +++ b/doc/source/cheshire_east_gov_uk.md @@ -10,6 +10,11 @@ waste_collection_schedule: - name: cheshire_east_gov_uk args: uprn: UPRN +``` + +```yaml +waste_collection_schedule: + sources: - name: cheshire_east_gov_uk args: postcode: POSTCODE @@ -37,9 +42,13 @@ waste_collection_schedule: - name: cheshire_east_gov_uk args: uprn: 100010132071 +``` + +```yaml +waste_collection_schedule: + sources: - name: cheshire_east_gov_uk args: postcode: WA16 0AY name_number: 1 ``` -