From f057564f8705ddaed1d119e6127bb591c65791b0 Mon Sep 17 00:00:00 2001 From: m26dvd <31007572+m26dvd@users.noreply.github.com> Date: Thu, 12 Dec 2024 23:38:06 +0000 Subject: [PATCH] fix: Swale Borough Council fix: #1080 (cherry picked from commit 6f580b39fb68b8079990221e050ae8dd6d2b7285) --- .../councils/SwaleBoroughCouncil.py | 60 +++++++++++++------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/uk_bin_collection/uk_bin_collection/councils/SwaleBoroughCouncil.py b/uk_bin_collection/uk_bin_collection/councils/SwaleBoroughCouncil.py index 3d607961cb..28202f9fa9 100644 --- a/uk_bin_collection/uk_bin_collection/councils/SwaleBoroughCouncil.py +++ b/uk_bin_collection/uk_bin_collection/councils/SwaleBoroughCouncil.py @@ -26,7 +26,7 @@ def parse_data(self, page: str, **kwargs) -> dict: check_paon(user_paon) # Build URL to parse - council_url = "https://swale.gov.uk/bins-littering-and-the-environment/bins/my-collection-day" + council_url = "https://swale.gov.uk/bins-littering-and-the-environment/bins/check-your-bin-day" # Create Selenium webdriver driver = create_webdriver(web_driver, headless, None, __name__) @@ -35,7 +35,7 @@ def parse_data(self, page: str, **kwargs) -> dict: # Wait for the postcode field to appear then populate it try: inputElement_postcode = WebDriverWait(driver, 10).until( - EC.presence_of_element_located((By.ID, "q462406_q1")) + EC.presence_of_element_located((By.ID, "q485476_q1")) ) inputElement_postcode.send_keys(user_postcode) except Exception: @@ -43,7 +43,7 @@ def parse_data(self, page: str, **kwargs) -> dict: # Click search button findAddress = WebDriverWait(driver, 10).until( - EC.presence_of_element_located((By.ID, "form_email_462397_submit")) + EC.presence_of_element_located((By.ID, "form_email_485465_submit")) ) driver.execute_script("arguments[0].click();", findAddress) @@ -52,7 +52,7 @@ def parse_data(self, page: str, **kwargs) -> dict: EC.element_to_be_clickable( ( By.XPATH, - "//select[@id='SBCYBDAddressList']//option[contains(., '" + "//select[@name='q485480:q1']//option[contains(., '" + user_paon + "')]", ) @@ -61,12 +61,12 @@ def parse_data(self, page: str, **kwargs) -> dict: # Click search button getBins = WebDriverWait(driver, 10).until( - EC.presence_of_element_located((By.ID, "form_email_462397_submit")) + EC.presence_of_element_located((By.ID, "form_email_485465_submit")) ) driver.execute_script("arguments[0].click();", getBins) BinTable = WebDriverWait(driver, 30).until( - EC.presence_of_element_located((By.ID, "SBC-YBD-Main")) + EC.presence_of_element_located((By.ID, "SBCYBDSummary")) ) soup = BeautifulSoup(driver.page_source, features="html.parser") @@ -74,17 +74,41 @@ def parse_data(self, page: str, **kwargs) -> dict: data = {"bins": []} - # Get the collection bullet points on the page and parse them - nextCollections = soup.find("div", {"id": "nextCollections"}) - for c in nextCollections: - collection = c.find_all("strong") - for bin in collection: - split = (bin.text).split(" on ") - bin_type = split[0] - bin_date = datetime.strptime(split[1], "%A %d %b %Y").strftime( - "%d/%m/%Y" - ) - dict_data = {"type": bin_type, "collectionDate": bin_date} - data["bins"].append(dict_data) + next_collection_date = soup.find( + "strong", id="SBC-YBD-collectionDate" + ).text.strip() + + # Extract bins for the next collection + next_bins = [li.text.strip() for li in soup.select("#SBCFirstBins ul li")] + + # Extract future collection details + future_collection_date_tag = soup.find( + "p", text=lambda t: t and "starting from" in t + ) + future_collection_date = ( + future_collection_date_tag.text.split("starting from")[-1].strip() + if future_collection_date_tag + else "No future date found" + ) + + future_bins = [li.text.strip() for li in soup.select("#FirstFutureBins li")] + + for bin in next_bins: + dict_data = { + "type": bin, + "collectionDate": datetime.strptime( + next_collection_date, "%A, %d %B" + ).strftime(date_format), + } + data["bins"].append(dict_data) + + for bin in future_bins: + dict_data = { + "type": bin, + "collectionDate": datetime.strptime( + future_collection_date, "%A, %d %B" + ).strftime(date_format), + } + data["bins"].append(dict_data) return data