-
Notifications
You must be signed in to change notification settings - Fork 100
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 #321 from MystifiedMeat/HarrogateBoroughCouncil
Adding support for Harrogate Borough Council
- Loading branch information
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
uk_bin_collection/tests/council_schemas/HarrogateBoroughCouncil.schema
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,39 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-06/schema#", | ||
"$ref": "#/definitions/Welcome7", | ||
"definitions": { | ||
"Welcome7": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"bins": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/Bin" | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"bins" | ||
], | ||
"title": "Welcome7" | ||
}, | ||
"Bin": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"type": { | ||
"type": "string" | ||
}, | ||
"collectionDate": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"collectionDate", | ||
"type" | ||
], | ||
"title": "Bin" | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
uk_bin_collection/tests/outputs/HarrogateBoroughCouncil.json
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,36 @@ | ||
{ | ||
"bins": [ | ||
{ | ||
"type": "Recycling", | ||
"collectionDate": "25/08/2023" | ||
}, | ||
{ | ||
"type": "Refuse", | ||
"collectionDate": "02/09/2023" | ||
}, | ||
{ | ||
"type": "Recycling", | ||
"collectionDate": "08/09/2023" | ||
}, | ||
{ | ||
"type": "Refuse", | ||
"collectionDate": "15/09/2023" | ||
}, | ||
{ | ||
"type": "Recycling", | ||
"collectionDate": "22/09/2023" | ||
}, | ||
{ | ||
"type": "Refuse", | ||
"collectionDate": "29/09/2023" | ||
}, | ||
{ | ||
"type": "Recycling", | ||
"collectionDate": "06/10/2023" | ||
}, | ||
{ | ||
"type": "Refuse", | ||
"collectionDate": "13/10/2023" | ||
} | ||
] | ||
} |
68 changes: 68 additions & 0 deletions
68
uk_bin_collection/uk_bin_collection/councils/HarrogateBoroughCouncil.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,68 @@ | ||
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) | ||
|
||
soup = BeautifulSoup(page.text, features="html.parser") | ||
soup.prettify() | ||
|
||
data = {"bins": []} | ||
|
||
headers = { | ||
'accept-language': 'en-GB,en;q=0.9', | ||
'cache-control': 'no-cache', | ||
} | ||
|
||
req_data = { | ||
'uprn': user_uprn, | ||
} | ||
|
||
url = f'https://secure.harrogate.gov.uk/inmyarea/Property/?uprn={user_uprn}' | ||
|
||
requests.packages.urllib3.disable_warnings() | ||
response = requests.post(url, headers=headers) | ||
|
||
soup = BeautifulSoup(response.text, features="html.parser") | ||
soup.prettify() | ||
|
||
collections = [] | ||
|
||
# Find section with bins in | ||
table = soup.find_all("table", {"class": "hbcRounds"})[1] | ||
|
||
# For each bin section, get the text and the list elements | ||
for row in table.find_all('tr'): | ||
bin_type = row.find('th').text | ||
td = row.find('td') | ||
for span in td.find_all('span'): | ||
span.extract() | ||
collectionDate = td.text.strip() | ||
next_collection = datetime.strptime(collectionDate, "%a %d %b %Y") | ||
collections.append((bin_type, next_collection)) | ||
|
||
# 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 |