From d127a9f584e2e48a67fe864b1ef19b7953dc97d5 Mon Sep 17 00:00:00 2001 From: Robert Bradley Date: Sat, 16 Dec 2023 20:41:00 +0000 Subject: [PATCH] feat: #264 Adding Oldham --- .../features/validate_council_outputs.feature | 1 + uk_bin_collection/tests/input.json | 5 ++ .../councils/OldhamCouncil.py | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 uk_bin_collection/uk_bin_collection/councils/OldhamCouncil.py diff --git a/uk_bin_collection/tests/features/validate_council_outputs.feature b/uk_bin_collection/tests/features/validate_council_outputs.feature index 1d50ab48a1..4abc79bae5 100644 --- a/uk_bin_collection/tests/features/validate_council_outputs.feature +++ b/uk_bin_collection/tests/features/validate_council_outputs.feature @@ -80,6 +80,7 @@ Feature: Test each council output matches expected results | NorthTynesideCouncil | None | None | | NorthumberlandCouncil | http://selenium:4444 | local | | NorthWestLeicestershire | http://selenium:4444 | local | + | OldhamCouncil | None | None | | PortsmouthCityCouncil | http://selenium:4444 | local | | PrestonCityCouncil | http://selenium:4444 | local | | ReadingBoroughCouncil | None | None | diff --git a/uk_bin_collection/tests/input.json b/uk_bin_collection/tests/input.json index e6e8a11da0..88061939f0 100644 --- a/uk_bin_collection/tests/input.json +++ b/uk_bin_collection/tests/input.json @@ -499,6 +499,11 @@ "url": "https://www.northumberland.gov.uk/Waste/Bins/Bin-Calendars.aspx", "web_driver": "http://selenium:4444", "wiki_name": "Northumberland Council" + }, + "OldhamCouncil": { + "url": "https://portal.oldham.gov.uk/bincollectiondates/details?uprn=422000033556", + "wiki_name": "Oldham Council", + "wiki_note": "Replace UPRN in URL with your own UPRN." }, "PortsmouthCityCouncil": { "skip_get_url": true, diff --git a/uk_bin_collection/uk_bin_collection/councils/OldhamCouncil.py b/uk_bin_collection/uk_bin_collection/councils/OldhamCouncil.py new file mode 100644 index 0000000000..8dc498c269 --- /dev/null +++ b/uk_bin_collection/uk_bin_collection/councils/OldhamCouncil.py @@ -0,0 +1,51 @@ +from uk_bin_collection.uk_bin_collection.common import * +from uk_bin_collection.uk_bin_collection.get_bin_data import \ + AbstractGetBinDataClass + +from bs4 import BeautifulSoup + + +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: + + data = {"bins": []} + soup = BeautifulSoup(page.text, 'html.parser') + + # Find all tables with the class "data-table confirmation" + tables = soup.find_all('table', class_='data-table confirmation') + for table in tables: + rows = table.find_all('tr') + bin_type = None + bin_collection = None + + # Search for the bin color in the table headers + th_element = table.find('th') + if th_element: + bin_type = th_element.text.strip() + + for row in rows: + header = row.find('b') + if header: + header_text = header.text.strip() + value_cell = row.find('td', class_='coltwo') + if value_cell: + value_text = value_cell.text.strip() + + if header_text == 'Collection Date': + bin_collection = value_text + + if bin_type and bin_collection: + dict_data = { + "type": bin_type, + "collectionDate": datetime.strptime(bin_collection, "%d/%m/%Y").strftime(date_format) + } + + data["bins"].append(dict_data) + + return data