Skip to content

Commit

Permalink
Merge pull request #859 from jacklau16/792_add_wokingham_council
Browse files Browse the repository at this point in the history
  • Loading branch information
robbrad authored Oct 8, 2024
2 parents 0b05b7e + 8b3ecc8 commit 5ce1f38
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
7 changes: 7 additions & 0 deletions uk_bin_collection/tests/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,13 @@
"wiki_name": "Woking Borough Council/Joint Waste Solutions",
"wiki_note": "Works with all collection areas that use Joint Waste Solutions. Just use the correct URL."
},
"WokinghamBoroughCouncil": {
"house_number": "90",
"postcode": "RG40 2HR",
"skip_get_url": true,
"url": "https://www.wokingham.gov.uk/rubbish-and-recycling/waste-collection/find-your-bin-collection-day",
"wiki_name": "Wokingham Borough Council"
},
"WychavonDistrictCouncil": {
"postcode": "WR3 7RU",
"skip_get_url": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from uk_bin_collection.uk_bin_collection.common import *
from uk_bin_collection.uk_bin_collection.get_bin_data import AbstractGetBinDataClass


# 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": []}
source_date_format = "%d/%m/%Y"
timeout = 10
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, None, __name__)
driver.get(
"https://www.wokingham.gov.uk/rubbish-and-recycling/waste-collection/find-your-bin-collection-day"
)

# Wait for the postcode field to appear then populate it
inputElement_postcode = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.ID, "edit-postcode-search-csv"))
)
inputElement_postcode.send_keys(user_postcode)

# Simulates hitting the "Enter" key to submit
inputElement_postcode.send_keys(Keys.RETURN)

# Select the exact address from the drop down box
WebDriverWait(driver, timeout).until(
EC.element_to_be_clickable(
(
By.XPATH,
""
"//*[@id='edit-address-options-csv']//option[starts-with(normalize-space(.), '"
+ user_paon
+ "')]",
)
)
).click()

# Wait for the Show collection dates button to appear, then click it to get the collection dates
inputElement_show_dates_button = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="edit-show-collection-dates-csv"]')
)
)
inputElement_show_dates_button.send_keys(Keys.RETURN)

# Wait for the collection dates elements to load
collection_date_cards = WebDriverWait(driver, timeout).until(
EC.presence_of_all_elements_located(
(By.XPATH, '//div[@class = "card__content"]')
)
)

for collection_date_card in collection_date_cards:
waste_type = collection_date_card.find_element(
By.XPATH, './/h3[@class = "heading heading--sub heading--tiny"]'
)
collection_date = collection_date_card.find_element(
By.XPATH, './/span[@class = "card__date"]'
)
dt_collection_date = datetime.strptime(
collection_date.text.split(" ")[1], source_date_format
)
dict_data = {
"type": waste_type.text,
"collectionDate": dt_collection_date.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 5ce1f38

Please sign in to comment.