Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 2025 Council Pack 3 #1197

Merged
merged 18 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions uk_bin_collection/tests/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@
"wiki_name": "Ards and North Down Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN."
},
"ArdsAndNorthDownCouncil": {
"url": "https://www.ardsandnorthdown.gov.uk",
"wiki_command_url_override": "https://www.ardsandnorthdown.gov.uk",
"uprn": "187136177",
"wiki_name": "Ards and North Down Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN."
},
"ArgyllandButeCouncil": {
"uprn": "125061759",
"skip_get_url": true,
Expand Down Expand Up @@ -212,6 +205,15 @@
"wiki_name": "Bolton Council",
"wiki_note": "To get the UPRN, you will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search). Previously required a single field that was UPRN and full address; now requires UPRN and postcode as separate fields."
},
"BostonBoroughCouncil": {
"postcode": "PE20 1AY",
"skip_get_url": true,
"house_number": "CEDAR",
"web_driver": "http://selenium:4444",
"url": "https://www.boston.gov.uk/findwastecollections",
"wiki_name": "Boston Borough Council",
"wiki_note": "Provide your house number in the `house_number` parameter and postcode in the `postcode` parameter."
},
"BracknellForestCouncil": {
"house_number": "57",
"paon": "57",
Expand Down Expand Up @@ -348,7 +350,7 @@
"CardiffCouncil": {
"skip_get_url": true,
"uprn": "100100112419",
"url": "https://www.cardiff.gov.uk/ENG/resident/Rubbish-and-recycling/When-are-my-bins-collected/Pages/default.aspx",
"url": "https://www.gov.uk",
"wiki_name": "Cardiff Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN."
},
Expand Down Expand Up @@ -611,13 +613,6 @@
"wiki_name": "East Herts Council",
"wiki_note": "Pass the house number and postcode in their respective parameters."
},
"EastHertsCouncil": {
"house_number": "1",
"postcode": "CM20 2FZ",
"skip_get_url": true,
"url": "https://www.eastherts.gov.uk",
"wiki_name": "East Herts Council"
},
"EastLindseyDistrictCouncil": {
"house_number": "1",
"postcode": "PE22 0YD",
Expand All @@ -627,6 +622,14 @@
"wiki_name": "East Lindsey District Council",
"wiki_note": "Pass the house name/number and postcode in their respective parameters. This parser requires a Selenium webdriver."
},
"EastLothianCouncil": {
"house_number": "Flat 1",
"postcode": "EH21 6QA",
"skip_get_url": true,
"url": "https://eastlothian.gov.uk",
"wiki_name": "East Lothian Council",
"wiki_note": "Pass the house number and postcode in their respective parameters"
},
"EastRenfrewshireCouncil": {
"house_number": "23",
"postcode": "G46 6RG",
Expand All @@ -645,6 +648,12 @@
"wiki_name": "East Riding Council",
"wiki_note": "Put the full address as it displays on the council website dropdown when you do the check manually."
},
"EastStaffordshireBoroughCouncil": {
"url": "https://www.eaststaffsbc.gov.uk/bins-rubbish-recycling/collection-dates/68382",
"wiki_command_url_override": "https://www.eaststaffsbc.gov.uk/bins-rubbish-recycling/collection-dates/XXXXX",
"wiki_name": "East Staffordshire Borough Council",
"wiki_note": "Replace `XXXXX` with your property's ID when selecting from https://www.eaststaffsbc.gov.uk/bins-rubbish-recycling/collection-dates."
},
"EastSuffolkCouncil": {
"postcode": "IP11 9FJ",
"skip_get_url": true,
Expand Down Expand Up @@ -745,20 +754,6 @@
"wiki_name": "Flintshire County Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN."
},
"FifeCouncil": {
"url": "https://www.fife.gov.uk",
"wiki_command_url_override": "https://www.fife.gov.uk",
"uprn": "320203521",
"wiki_name": "Fife Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN."
},
"FlintshireCountyCouncil": {
"url": "https://digital.flintshire.gov.uk",
"wiki_command_url_override": "https://digital.flintshire.gov.uk",
"uprn": "100100213710",
"wiki_name": "Flintshire County Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN."
},
"FolkstoneandHytheDistrictCouncil": {
"skip_get_url": true,
"uprn": "50032097",
Expand Down Expand Up @@ -994,6 +989,12 @@
"wiki_name": "Leeds City Council",
"wiki_note": "Pass the house number, postcode, and UPRN. This parser requires a Selenium webdriver."
},
"LeicesterCityCouncil": {
"url": "https://biffaleicester.co.uk",
"uprn": "2465027976",
"wiki_name": "Leicester City Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN."
},
"LichfieldDistrictCouncil": {
"url": "https://www.lichfielddc.gov.uk",
"wiki_command_url_override": "https://www.lichfielddc.gov.uk",
Expand Down Expand Up @@ -1231,7 +1232,7 @@
"url": "https://community.newcastle.gov.uk/my-neighbourhood/ajax/getBinsNew.php?uprn=004510730634",
"wiki_command_url_override": "https://community.newcastle.gov.uk/my-neighbourhood/ajax/getBinsNew.php?uprn=XXXXXXXX",
"wiki_name": "Newcastle City Council",
"wiki_note": "Replace XXXXXXXX with your UPRN."
"wiki_note": "Replace XXXXXXXX with your UPRN. UPRNs need to be 12 digits long so please pad the left hand side with 0s if your UPRN is not long enough"
},
"NewcastleUnderLymeCouncil": {
"url": "https://www.newcastle-staffs.gov.uk",
Expand Down
149 changes: 149 additions & 0 deletions uk_bin_collection/uk_bin_collection/councils/BostonBoroughCouncil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
from bs4 import BeautifulSoup
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 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": []}
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.boston.gov.uk/findwastecollections")

accept_button = WebDriverWait(driver, timeout=30).until(
EC.element_to_be_clickable((By.NAME, "acceptall"))
)
accept_button.click()

# Wait for the postcode field to appear then populate it
inputElement_postcode = WebDriverWait(driver, 30).until(
EC.presence_of_element_located(
(By.ID, "BBCWASTECOLLECTIONS_START_SEARCHPOSTCODE")
)
)
inputElement_postcode.send_keys(user_postcode)

# Click search button
findAddress = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.ID, "BBCWASTECOLLECTIONS_START_START10_NEXT")
)
)
findAddress.click()

# Wait for the custom dropdown container to be visible
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable(
(By.ID, "BBCWASTECOLLECTIONS_COLLECTIONADDRESS_INCIDENTUPRN_chosen")
)
)

# Click on the dropdown to open it
dropdown = driver.find_element(
By.ID, "BBCWASTECOLLECTIONS_COLLECTIONADDRESS_INCIDENTUPRN_chosen"
)
dropdown.click()

# Wait for the dropdown options to be visible
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "chosen-results"))
)

# Locate the desired option using its text
desired_option = driver.find_element(
By.XPATH,
"//li[@class='active-result' and contains(text(), '"
+ user_paon
+ "')]",
)

# Click on the desired option
desired_option.click()

# dropdown.select_by_visible_text(user_paon)

# Click search button
findAddress = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.ID, "BBCWASTECOLLECTIONS_COLLECTIONADDRESS_NEXT3_NEXT")
)
)
findAddress.click()

# Wait for the collections table to appear
WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.ID, "BBCWASTECOLLECTIONS_SERVICE_FIELD859_OUTER")
)
)

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

# Find the container with the bin information
bins = soup.find_all(
"div", class_="grid__cell grid__cell--listitem grid__cell--cols1"
)

current_year = datetime.now().year
next_year = current_year + 1

# Loop through each bin container to extract the details
for bin_div in bins:
# Find the bin type (title text)
bin_type = bin_div.find("h2", class_="item__title").text.strip()

# Find the next collection date
next_collection = (
bin_div.find("div", class_="item__content")
.find("div")
.text.strip()
.replace("Next: ", "")
)

next_collection = datetime.strptime(
remove_ordinal_indicator_from_date_string(next_collection),
"%A %d %B",
)

if (datetime.now().month == 12) and (next_collection.month == 1):
next_collection = next_collection.replace(year=next_year)
else:
next_collection = next_collection.replace(year=current_year)

dict_data = {
"type": bin_type,
"collectionDate": next_collection.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
83 changes: 83 additions & 0 deletions uk_bin_collection/uk_bin_collection/councils/EastLothianCouncil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import requests
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


# 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:

user_postcode = kwargs.get("postcode")
user_paon = kwargs.get("paon")
check_postcode(user_postcode)
check_paon(user_paon)
bindata = {"bins": []}

URI = "http://collectiondates.eastlothian.gov.uk/ajax/your-calendar/load-streets-spring-2024.asp"

payload = {
"postcode": user_postcode,
}

headers = {
"Referer": "http://collectiondates.eastlothian.gov.uk/your-calendar",
"User-Agent": "Mozilla/5.0",
}

# Make the GET request
response = requests.get(URI, headers=headers, params=payload)

# Parse the HTML with BeautifulSoup
soup = BeautifulSoup(response.text, "html.parser")

# Find the select dropdown
select = soup.find("select", id="SelectStreet")

# Find the option that contains "Flat 1"
address = select.find("option", string=lambda text: text and user_paon in text)

URI = "http://collectiondates.eastlothian.gov.uk/ajax/your-calendar/load-recycling-summer-2024.asp"

payload = {
"id": address["value"],
}

# Make the GET request
response = requests.get(URI, headers=headers, params=payload)

# Parse the HTML with BeautifulSoup
soup = BeautifulSoup(response.text, "html.parser")

# Extract collection details
calendar_items = soup.find_all("div", class_="calendar-item")
for item in calendar_items:
waste_label = item.find("div", class_="waste-label").text.strip()
waste_value = item.find("div", class_="waste-value").find("h4").text.strip()

try:
collection_date = datetime.strptime(
remove_ordinal_indicator_from_date_string(waste_value),
"%A %d %B %Y",
)
except ValueError:
continue

dict_data = {
"type": waste_label.replace(" is:", ""),
"collectionDate": collection_date.strftime(date_format),
}
bindata["bins"].append(dict_data)

bindata["bins"].sort(
key=lambda x: datetime.strptime(x.get("collectionDate"), date_format)
)

return bindata
Loading
Loading