Skip to content

Commit

Permalink
Merge pull request #424 from dp247/417-request-for-liverpool-city-cou…
Browse files Browse the repository at this point in the history
…ncil

feat: Add support for Liverpool City Council
  • Loading branch information
OliverCullimore authored Nov 8, 2023
2 parents 6f481a0 + 1af5ce7 commit 6028991
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 32 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ to = {format = "poetry", path = "pyproject.toml"}

[tool.poetry.dependencies]
bs4 = "*"
python-dateutil = "*"
holidays = "*"
pandas = "*"
python = ">=3.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Feature: Test each council output matches expected results
| LancasterCityCouncil | None | None |
| LeedsCityCouncil | None | None |
| LisburnCastlereaghCityCouncil | None | None |
| LiverpoolCityCouncil | None | None |
| LondonBoroughHounslow | None | None |
| MaldonDistrictCouncil | None | None |
| MalvernHillsDC | None | None |
Expand Down
6 changes: 6 additions & 0 deletions uk_bin_collection/tests/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@
"url": "https://lisburn.isl-fusion.com",
"wiki_name": "Lisburn and Castlereagh City Council"
},
"LiverpoolCityCouncil": {
"url": "https://liverpool.gov.uk/Bins/BinDatesTable?UPRN=38164600",
"wiki_command_url_override": "https://liverpool.gov.uk/Bins/BinDatesTable?UPRN=XXXXXXXX",
"wiki_name": "Liverpool City Council",
"wiki_note": "Replace XXXXXXXX with your property's UPRN."
},
"LondonBoroughHounslow": {
"skip_get_url": true,
"uprn": "100021577765",
Expand Down
56 changes: 56 additions & 0 deletions uk_bin_collection/uk_bin_collection/LiverpoolCityCouncil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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
from dateutil.relativedelta import relativedelta


# 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:
# Add in some variables we need
data = {"bins": []}
collections = []
curr_date = datetime.today()

# Parse the page
soup = BeautifulSoup(page.text, features="html.parser")
soup.prettify()

# Get all table rows on the page - enumerate gives us an index, which is handy for to keep a row count.
# In this case, the first (0th) row is headings, so we can skip it, then parse the other data.
for idx, row in enumerate(soup.find_all("tr")):
if idx == 0:
continue
row_type = row.find("th").text.strip()
row_data = row.find_all("td")
# When we get the row data, we can loop through it all and parse it to datetime. Because there are no
# years, we must add it in, then check if we need to overflow it to the following year.
for item in row_data:
if item.text.strip() == "Today":
collections.append((row_type, curr_date))
else:
bin_date = datetime.strptime(remove_ordinal_indicator_from_date_string(item.text.strip()),
"%A, %d %B").replace(year=curr_date.year)
if curr_date.month == 12 and bin_date.month == 1:
bin_date = bin_date + relativedelta(years=1)
collections.append((row_type, bin_date))

# Sort the text and list elements by date
ordered_data = sorted(collections, key=lambda x: x[1])

# Put the elements into the dictionary
for item in ordered_data:
dict_data = {
"type": item[0],
"collectionDate": item[1].strftime(date_format),
}
data["bins"].append(dict_data)

return data
Loading

0 comments on commit 6028991

Please sign in to comment.