Skip to content

Commit

Permalink
Merge pull request #666 from dp247/355-Knowsley-Metropolitan-Borough-…
Browse files Browse the repository at this point in the history
…Council

feat: add support for Knowsley Metropolitan Borough Council
  • Loading branch information
robbrad authored Apr 27, 2024
2 parents bc9e03c + 3a495e7 commit 23d76b1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ Feature: Test each council output matches expected results
| council | selenium_url | selenium_mode |
| KirkleesCouncil | http://selenium:4444 | local |

@KnowsleyMBCouncil
Examples: KnowsleyMBCouncil
| council | selenium_url | selenium_mode |
| KnowsleyMBCouncil | http://selenium:4444 | local |

@LancasterCityCouncil
Examples: LancasterCityCouncil
| council | selenium_url | selenium_mode |
Expand Down
9 changes: 9 additions & 0 deletions uk_bin_collection/tests/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,15 @@
"web_driver": "http://selenium:4444",
"wiki_name": "Kirklees Council"
},
"KnowsleyMBCouncil": {
"house_number": "22",
"postcode": "L36 3UY",
"skip_get_url": true,
"url": "https://knowsleytransaction.mendixcloud.com/link/youarebeingredirected?target=bincollectioninformation",
"web_driver": "http://selenium:4444",
"wiki_name": "Knowsley Metropolitan Borough Council",
"wiki_note": "Pass the postcode in the postcode parameter, wrapped in double quotes and with a space."
},
"LancasterCityCouncil": {
"house_number": "1",
"postcode": "LA1 1RS",
Expand Down
116 changes: 116 additions & 0 deletions uk_bin_collection/uk_bin_collection/councils/KnowsleyMBCouncil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import time

from bs4 import BeautifulSoup
from uk_bin_collection.uk_bin_collection.common import *
from uk_bin_collection.uk_bin_collection.get_bin_data import AbstractGetBinDataClass
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait

# import the wonderful Beautiful Soup and the URL grabber
class CouncilClass(AbstractGetBinDataClass):
"""
Concrete classes have to implement all abstract operations of the
base class. They can also override some operations with a default
implementation.
"""

def parse_data(self, page: str, **kwargs) -> dict:
driver = None
try:
data = {"bins": []}
collections = []
user_paon = kwargs.get("paon")
user_postcode = kwargs.get("postcode")
web_driver = kwargs.get("web_driver")
headless = kwargs.get("headless")
check_paon(user_paon)
check_postcode(user_postcode)

# Create Selenium webdriver
driver = create_webdriver(web_driver, headless)
driver.get(
"https://knowsleytransaction.mendixcloud.com/link/youarebeingredirected?target=bincollectioninformation"
)

# Wait for the postcode field to appear then populate it
inputElement_postcode = WebDriverWait(driver, 30).until(
EC.visibility_of_element_located(
(By.XPATH, '/html/body/div[1]/div/div/div/div/div/div[2]/div/div/div/div/div/div[3]/div/div[1]/div/div[1]/div/div/input')
)
)
inputElement_postcode.send_keys(user_postcode)

# Wait for address search button, then click it
addressSearch_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, '/html/body/div[1]/div/div/div/div/div/div[2]/div/div/div/div/div/div[3]/div/div[1]/div/div[2]/div/button')
)
)
addressSearch_button.click()

# Wait until the address list has loaded
WebDriverWait(driver, 30).until(
EC.presence_of_element_located(
(By.XPATH, '/html/body/div[1]/div/div/div/div/div/div[2]/div/div/div/div/div/div[3]/div/div[1]/div/div[3]/div/div')
)
)

# Select the correct address from the list
addressList_rows = driver.find_elements(By.CLASS_NAME, 'row')
for row in addressList_rows:
option_name = row.text[0:len(user_paon)]
if option_name == user_paon:
break
address_to_select = row.find_element(By.LINK_TEXT, "Choose this address")
address_to_select.click()

# Wait for bin dates to load
WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.XPATH, '/html/body/div[1]/div/div/div/div/div/div[2]/div/div/div/div/div/div[3]/div/div[1]/div/div[4]/div/div')
)
)

# Parse the HTML from the WebDriver
soup = BeautifulSoup(driver.page_source, features="html.parser")
soup.prettify()

z = soup.find("div", {"class": "mx-name-textBox5 mx-textbox form-group"}).find_next(
"div", {"class": "form-control-static"})

maroon_bin_date = datetime.strptime(
soup.find("div", {"class": "mx-name-textBox3 mx-textbox form-group"}).find_next(
"div", {"class": "form-control-static"}).get_text(strip=True), "%A %d/%m/%Y")
collections.append(("Maroon bin", maroon_bin_date))

grey_bin_date = datetime.strptime(
soup.find("div", {"class": "mx-name-textBox4 mx-textbox form-group"}).find_next(
"div", {"class": "form-control-static"}).get_text(strip=True), "%A %d/%m/%Y")
collections.append(("Grey bin", grey_bin_date))

blue_bin_date = datetime.strptime(
soup.find("div", {"class": "mx-name-textBox5 mx-textbox form-group"}).find_next(
"div", {"class": "form-control-static"}).get_text(strip=True), "%A %d/%m/%Y")
collections.append(("Blue bin", blue_bin_date))

ordered_data = sorted(collections, key=lambda x: x[1])
for item in ordered_data:
dict_data = {
"type": item[0].capitalize(),
"collectionDate": item[1].strftime(date_format),
}
data["bins"].append(dict_data)

except Exception as e:
# Here you can log the exception if needed
print(f"An error occurred: {e}")
# Optionally, re-raise the exception if you want it to propagate
raise
finally:
# This block ensures that the driver is closed regardless of an exception
if driver:
driver.quit()

return data

0 comments on commit 23d76b1

Please sign in to comment.