From 3c3df5fe680e4b5245753673034857619234fcda Mon Sep 17 00:00:00 2001 From: Dan Baker Date: Thu, 2 Jan 2025 12:55:20 +0000 Subject: [PATCH] feat: Hart District Council --- uk_bin_collection/tests/input.json | 9 ++- .../councils/HartDistrictCouncil.py | 67 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) mode change 100644 => 100755 uk_bin_collection/tests/input.json create mode 100755 uk_bin_collection/uk_bin_collection/councils/HartDistrictCouncil.py diff --git a/uk_bin_collection/tests/input.json b/uk_bin_collection/tests/input.json old mode 100644 new mode 100755 index 58df932495..f07df2850a --- a/uk_bin_collection/tests/input.json +++ b/uk_bin_collection/tests/input.json @@ -840,6 +840,13 @@ "wiki_name": "Harrogate Borough Council", "wiki_note": "Pass the UPRN, which can be found at [this site](https://secure.harrogate.gov.uk/inmyarea). URL doesn't need to be passed." }, + "HartDistrictCouncil": { + "skip_get_url": true, + "uprn": "100062349291", + "url": "https://www.hart.gov.uk/", + "wiki_name": "Hart District Council", + "wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find the UPRN." + }, "HartlepoolBoroughCouncil": { "url": "https://www.hartlepool.gov.uk", "uprn": "100110019551", @@ -2120,4 +2127,4 @@ "wiki_name": "York Council", "wiki_note": "Provide your UPRN." } -} \ No newline at end of file +} diff --git a/uk_bin_collection/uk_bin_collection/councils/HartDistrictCouncil.py b/uk_bin_collection/uk_bin_collection/councils/HartDistrictCouncil.py new file mode 100755 index 0000000000..edce893514 --- /dev/null +++ b/uk_bin_collection/uk_bin_collection/councils/HartDistrictCouncil.py @@ -0,0 +1,67 @@ +import json +from datetime import datetime + +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_uprn = kwargs.get("uprn") + check_uprn(user_uprn) + + URI = f"https://www.hart.gov.uk/bbd-whitespace/next-collection-dates?uri=entity%3Anode%2F172&uprn={user_uprn}" + + response = requests.get(URI) + response_table = response.json() + + soup = BeautifulSoup(response_table[0]["data"], "html.parser") + # Make a BS4 object + # Find all the rows in the table + rows = soup.find_all("tr") + + # Initialize an empty list to hold the bin data + bins = [] + + # Iterate through each row + for row in rows: + cells = row.find_all("td") + + # Check if there are exactly 3 cells in the row + if len(cells) == 3: + bin_type = cells[0].get_text(strip=True) + collection_date = self.format_date(cells[2].get_text(strip=True)) + + # Create a dictionary for each bin and append to the bins list + bins.append({"type": bin_type, "collectionDate": collection_date}) + + return {"bins": bins} + + def format_date(self, date_str): + # Get the current date and year + current_date = datetime.now() + current_year = current_date.year + + # Parse the provided date string (e.g. "23 January") + date_obj = datetime.strptime(date_str, "%d %B") + + # Check if the provided date has already passed this year + if date_obj.replace(year=current_year) < current_date: + # If the date has passed this year, assume the next year + date_obj = date_obj.replace(year=current_year + 1) + else: + # Otherwise, use the current year + date_obj = date_obj.replace(year=current_year) + + # Format the date in "DD/MM/YYYY" format + return date_obj.strftime("%d/%m/%Y")