-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #424 from dp247/417-request-for-liverpool-city-cou…
…ncil feat: Add support for Liverpool City Council
- Loading branch information
Showing
5 changed files
with
247 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
uk_bin_collection/uk_bin_collection/LiverpoolCityCouncil.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.