Skip to content

Commit

Permalink
Merge pull request #1003 from bengutt/master
Browse files Browse the repository at this point in the history
feat: Add South Staffordshire District Council
  • Loading branch information
dp247 authored Nov 13, 2024
2 parents bebe057 + fbabd4c commit 9bca786
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions uk_bin_collection/tests/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,12 @@
"wiki_name": "South Ribble Council",
"wiki_note": "You will need to use [FindMyAddress](https://www.findmyaddress.co.uk/search) to find your UPRN."
},
"SouthStaffordshireDistrictCouncil": {
"uprn": "200004523954",
"url": "https://www.sstaffs.gov.uk/where-i-live?uprn=200004523954",
"wiki_name": "South Staffordshire District Council",
"wiki_note": "The URL needs to be `https://www.sstaffs.gov.uk/where-i-live?uprn=<Your_UPRN>`. Replace `<Your_UPRN>` with your UPRN."
},
"SouthTynesideCouncil": {
"house_number": "1",
"postcode": "NE33 3JW",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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_date(self, date_str):
months = {
"January": "01",
"February": "02",
"March": "03",
"April": "04",
"May": "05",
"June": "06",
"July": "07",
"August": "08",
"September": "09",
"October": "10",
"November": "11",
"December": "12",
}
day, date, month_abbr, year = date_str.split()
month = months[month_abbr]
return f"{date}/{month}/{year}"

def add_bin_types_to_collection(
self, bin_data: {"bins": []}, collection_date: str, collectionType: str
):
if "Grey Bin" in collectionType:
bin_data["bins"].append(
{
"type": "Grey Bin",
"collectionDate": self.parse_date(collection_date),
}
)
if "Green Bin" in collectionType:
bin_data["bins"].append(
{
"type": "Green Bin",
"collectionDate": self.parse_date(collection_date),
}
)

if "Blue Bin" in collectionType:
bin_data["bins"].append(
{
"type": "Blue Bin",
"collectionDate": self.parse_date(collection_date),
}
)

def parse_data(self, page: str, **kwargs) -> dict:
# Make a BS4 object
soup = BeautifulSoup(page.text, features="html.parser")
soup.prettify()

# Initialize the bin data structure
bin_data = {"bins": []}

collectionDatesSection = soup.find("div", id="showCollectionDates")

# Find next date
collection_date = collectionDatesSection.find(
"p", class_="collection-date"
).getText()

# convert to date
collection_type = collectionDatesSection.find(
"p", class_="collection-type"
).getText()

self.add_bin_types_to_collection(bin_data, collection_date, collection_type)

# Find the table with collection dates
table = collectionDatesSection.find("table", class_="leisure-table")

# Extract the rows containing the bin collection information
rows = table.find_all("tr")

# Loop through the rows and extract bin data
for row in rows:
cells = row.find_all("td")
if len(cells) == 2:
collection_date = cells[1].get_text(strip=True)
collection_type = cells[0].get_text(strip=True)

self.add_bin_types_to_collection(
bin_data, collection_date, collection_type
)

return bin_data

0 comments on commit 9bca786

Please sign in to comment.