Skip to content

Commit

Permalink
Merge pull request #482 from robbrad/204_Forest_of_Dean_District
Browse files Browse the repository at this point in the history
feat: adding #204 Forest_of_Dean_District
  • Loading branch information
dp247 authored Dec 15, 2023
2 parents 9e4a696 + ed28fa2 commit a18c18c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Feature: Test each council output matches expected results
| EnvironmentFirst | None | None |
| ErewashBoroughCouncil | None | None |
| FenlandDistrictCouncil | None | None |
| ForestOfDeanDistrictCouncil | http://selenium:4444 | local |
| GatesheadCouncil | http://selenium:4444 | local |
| GlasgowCityCouncil | None | None |
| GuildfordCouncil | None | None |
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 @@ -251,6 +251,15 @@
"uprn": "200002981143",
"url": "https://www.fenland.gov.uk/article/13114/",
"wiki_name": "Fenland District Council"
},
"ForestOfDeanDistrictCouncil": {
"skip_get_url": true,
"house_number": "ELMOGAL, PARKEND ROAD, BREAM, LYDNEY",
"postcode": "GL15 6JT",
"url": "https://community.fdean.gov.uk/s/waste-collection-enquiry",
"wiki_name": "Forest of Dean District Council",
"wiki_note": "Pass the full address in the house number and postcode in",
"web_driver": "http://selenium:4444"
},
"GatesheadCouncil": {
"house_number": "Bracken Cottage",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from bs4 import BeautifulSoup
from datetime import datetime
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
from selenium.webdriver.common.keys import Keys
import time

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:
page = "https://community.fdean.gov.uk/s/waste-collection-enquiry"

data = {"bins": []}

house_number = kwargs.get("paon")
postcode = kwargs.get("postcode")
full_address = f"{house_number}, {postcode}"
web_driver = kwargs.get("web_driver")

# Create Selenium webdriver
driver = create_webdriver(web_driver)
driver.get(page)

# If you bang in the house number (or property name) and postcode in the box it should find your property
wait = WebDriverWait(driver, 60)
address_entry_field = wait.until(
EC.presence_of_element_located((By.XPATH, '//*[@id="combobox-input-19"]'))
)

address_entry_field.send_keys(str(full_address))

address_entry_field = wait.until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="combobox-input-19"]'))
)
address_entry_field.click()
address_entry_field.send_keys(Keys.BACKSPACE)
address_entry_field.send_keys(str(full_address[len(full_address)-1]))

first_found_address = wait.until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="dropdown-element-19"]/ul'))
)

first_found_address.click()
# Wait for the 'Select your property' dropdown to appear and select the first result
next_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//lightning-button/button")))
next_btn.click()
bin_data = wait.until(
EC.presence_of_element_located((By.XPATH, "//span[contains(text(), 'Container')]"))
)

soup = BeautifulSoup(driver.page_source, features="html.parser")

rows = soup.find_all('tr', class_='slds-hint-parent')
current_year = datetime.now().year

for row in rows:
columns = row.find_all('td')
if columns:
container_type = row.find('th').text.strip()
collection_day = columns[0].text.strip()

# Parse the date from the string
parsed_date = datetime.strptime(collection_day, '%a, %d %B')
if parsed_date < datetime(parsed_date.year, parsed_date.month, parsed_date.day):
parsed_date = parsed_date.replace(year=current_year + 1)
else:
parsed_date = parsed_date.replace(year=current_year)
# Format the date as %d/%m/%Y
formatted_date = parsed_date.strftime('%d/%m/%Y')

# Add the bin type and collection date to the 'data' dictionary
data["bins"].append(
{
"type": container_type,
"collectionDate": formatted_date
}
)

return data

0 comments on commit a18c18c

Please sign in to comment.