-
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 #382 from OliverCullimore/374-cannock-chase-distri…
…ct-council feat: Add support for Cannock Chase District Council
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
uk_bin_collection/tests/council_schemas/CannockChaseDistrictCouncil.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/Welcome8", | ||
"definitions": { | ||
"Welcome8": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"bins": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/Bin" | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"bins" | ||
], | ||
"title": "Welcome8" | ||
}, | ||
"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
28 changes: 28 additions & 0 deletions
28
uk_bin_collection/tests/outputs/CannockChaseDistrictCouncil.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,28 @@ | ||
{ | ||
"bins": [ | ||
{ | ||
"type": "Refuse", | ||
"collectionDate": "26/10/2023" | ||
}, | ||
{ | ||
"type": "Garden", | ||
"collectionDate": "02/11/2023" | ||
}, | ||
{ | ||
"type": "Recycle", | ||
"collectionDate": "02/11/2023" | ||
}, | ||
{ | ||
"type": "Refuse", | ||
"collectionDate": "09/11/2023" | ||
}, | ||
{ | ||
"type": "Garden", | ||
"collectionDate": "16/11/2023" | ||
}, | ||
{ | ||
"type": "Recycle", | ||
"collectionDate": "16/11/2023" | ||
} | ||
] | ||
} |
58 changes: 58 additions & 0 deletions
58
uk_bin_collection/uk_bin_collection/councils/CannockChaseDistrictCouncil.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,58 @@ | ||
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") | ||
user_postcode = kwargs.get("postcode") | ||
check_uprn(user_uprn) | ||
check_postcode(user_postcode) | ||
|
||
# Make SOAP Request | ||
response = requests.post( | ||
"https://ccdc.opendata.onl/DynamicCall.dll", | ||
data="Method=CollectionDates&Postcode=" + user_postcode + "&UPRN=" + user_uprn, | ||
headers={ | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Referer": "https://ccdc.opendata.onl/CCDC_WasteCollection", | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" | ||
} | ||
) | ||
|
||
# Make a BS4 object | ||
soup = BeautifulSoup(response.text, "xml") | ||
soup.prettify() | ||
|
||
if soup.find("ErrorDescription") and soup.find("ErrorDescription").get_text( | ||
strip=True) == "No results returned": | ||
raise ValueError("No collection data found for provided Postcode & UPRN.") | ||
|
||
data = {"bins": []} | ||
|
||
collections = soup.find_all("Collection") | ||
|
||
for i in range(len(collections)): | ||
dict_data = { | ||
"type": collections[i].Service.get_text().replace("Collection Service", "").strip(), | ||
"collectionDate": datetime.strptime( | ||
collections[i].Date.get_text(), | ||
"%d/%m/%Y %H:%M:%S" | ||
).strftime(date_format) | ||
} | ||
data["bins"].append(dict_data) | ||
|
||
data["bins"].sort( | ||
key=lambda x: datetime.strptime(x.get("collectionDate"), date_format) | ||
) | ||
return data |