Skip to content

Commit

Permalink
feat: Hart District Council
Browse files Browse the repository at this point in the history
  • Loading branch information
dansbaker committed Jan 2, 2025
1 parent 7b4b6eb commit 3c3df5f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
9 changes: 8 additions & 1 deletion uk_bin_collection/tests/input.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -2120,4 +2127,4 @@
"wiki_name": "York Council",
"wiki_note": "Provide your UPRN."
}
}
}
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 3c3df5f

Please sign in to comment.