Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Torbay Council #184

Merged
merged 3 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions uk_bin_collection/tests/council_schemas/TorbayCouncil.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Welcome10",
"definitions": {
"Welcome10": {
"type": "object",
"additionalProperties": false,
"properties": {
"bins": {
"type": "array",
"items": {
"$ref": "#/definitions/Bin"
}
}
},
"required": [
"bins"
],
"title": "Welcome10"
},
"Bin": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"type": "string"
},
"collectionDate": {
"type": "string"
}
},
"required": [
"collectionDate",
"type"
],
"title": "Bin"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Feature: Test each council output matches expected results in /outputs
| StockportBoroughCouncil |
| TamesideMBCouncil |
| TonbridgeAndMallingBC |
| TorbayCouncil |
| TorridgeDistrictCouncil |
| ValeofGlamorganCouncil |
| WarwickDistrictCouncil |
Expand Down
4 changes: 4 additions & 0 deletions uk_bin_collection/tests/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
"postcode": "ME19 4JS",
"uprn": "10002914589"
},
"TorbayCouncil": {
"url": "https://www.torbay.gov.uk/recycling/bin-collections/",
"uprn": "100041053198"
},
"TorridgeDistrictCouncil": "10091078762",
"ValeofGlamorganCouncil": {
"url": "https://www.valeofglamorgan.gov.uk/en/living/Recycling-and-Waste/",
Expand Down
60 changes: 60 additions & 0 deletions uk_bin_collection/tests/outputs/TorbayCouncil.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"bins": [
{
"type": "Food Caddy",
"collectionDate": "22/02/2023"
},
{
"type": "Paper Bag",
"collectionDate": "22/02/2023"
},
{
"type": "Recycling Box",
"collectionDate": "22/02/2023"
},
{
"type": "Residual Bin",
"collectionDate": "22/02/2023"
},
{
"type": "Food Caddy",
"collectionDate": "01/03/2023"
},
{
"type": "Paper Bag",
"collectionDate": "01/03/2023"
},
{
"type": "Recycling Box",
"collectionDate": "01/03/2023"
},
{
"type": "Food Caddy",
"collectionDate": "08/03/2023"
},
{
"type": "Paper Bag",
"collectionDate": "08/03/2023"
},
{
"type": "Recycling Box",
"collectionDate": "08/03/2023"
},
{
"type": "Residual Bin",
"collectionDate": "08/03/2023"
},
{
"type": "Food Caddy",
"collectionDate": "15/03/2023"
},
{
"type": "Paper Bag",
"collectionDate": "15/03/2023"
},
{
"type": "Recycling Box",
"collectionDate": "15/03/2023"
}
]
}
48 changes: 48 additions & 0 deletions uk_bin_collection/uk_bin_collection/councils/TorbayCouncil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from uk_bin_collection.uk_bin_collection.common import *
from uk_bin_collection.uk_bin_collection.get_bin_data import AbstractGetBinDataClass


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:
uprn = kwargs.get("uprn")
check_uprn(uprn)

headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
"Connection": "keep-alive",
"Host": "online.torbay.gov.uk",
"Origin": "https://www.torbay.gov.uk",
"Referer": "https://www.torbay.gov.uk/",
"sec-ch-ua": "\"Chromium\";v=\"110\", \"Not A(Brand\";v=\"24\", \"Google Chrome\";v=\"110\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
requests.packages.urllib3.disable_warnings()
response = requests.get(f"https://online.torbay.gov.uk/services.bartec/collections?uprn={uprn}", headers=headers)
if response.status_code != 200:
raise ValueError("No bin data found for provided UPRN.")
json_data = json.loads(response.text)

data = {"bins": []}
for c in json_data:
dict_data = {
"type": c["Service"].replace("Empty ", "").strip(),
"collectionDate": datetime.strptime(
c["NextCollection"].strip(), "%d %B %Y"
).strftime(date_format),
}
data["bins"].append(dict_data)

return data