Skip to content

Commit

Permalink
feat: Add support for Bath and North East Somerset Council
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverCullimore committed Sep 19, 2023
1 parent 2ea235c commit f014a93
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Welcome4",
"definitions": {
"Welcome4": {
"type": "object",
"additionalProperties": false,
"properties": {
"bins": {
"type": "array",
"items": {
"$ref": "#/definitions/Bin"
}
}
},
"required": [
"bins"
],
"title": "Welcome4"
},
"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 @@ -10,6 +10,7 @@ Feature: Test each council output matches expected results in /outputs
| council |
| AylesburyValeCouncil |
| BasingstokeCouncil |
| BathAndNorthEastSomersetCouncil |
| BCPCouncil |
| BexleyCouncil |
| BlackburnCouncil |
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 @@ -12,6 +12,12 @@
"url": "https://www.basingstoke.gov.uk/bincollection",
"wiki_name": "Basingstoke Council"
},
"BathAndNorthEastSomersetCouncil": {
"SKIP_GET_URL": "SKIP_GET_URL",
"uprn": "100120000855",
"url": "https://www.bathnes.gov.uk/webforms/waste/collectionday/",
"wiki_name": "Bath and North East Somerset Council"
},
"BCPCouncil": {
"SKIP_GET_URL": "SKIP_GET_URL",
"uprn": "100040810214",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"bins": [
{
"type": "Black Rubbish Bin",
"collectionDate": "22/09/2023"
},
{
"type": "Recycling Containers",
"collectionDate": "22/09/2023"
},
{
"type": "Garden Waste",
"collectionDate": "29/09/2023"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import json
import requests
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


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)

headers = {
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
"connection": "keep-alive",
"content-type": "application/json",
"host": "www.bathnes.gov.uk",
"referer": "https://www.bathnes.gov.uk/webforms/waste/collectionday/",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"sec-gpc": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
}

requests.packages.urllib3.disable_warnings()
response = requests.get(
f"https://www.bathnes.gov.uk/webapi/api/BinsAPI/v2/getbartecroute/{user_uprn}/true",
headers=headers,
)
if response.text == "":
raise ValueError("Error parsing data. Please check the provided UPRN. "
"If this error continues please open an issue on GitHub.")
json_data = json.loads(response.text)

data = {"bins": []}

if len(json_data["residualNextDate"]) > 0:
dict_data = {
"type": "Black Rubbish Bin",
"collectionDate": datetime.strptime(
json_data["residualNextDate"], "%Y-%m-%dT%H:%M:%S"
).strftime(date_format),
}
data["bins"].append(dict_data)
if len(json_data["recyclingNextDate"]) > 0:
dict_data = {
"type": "Recycling Containers",
"collectionDate": datetime.strptime(
json_data["recyclingNextDate"], "%Y-%m-%dT%H:%M:%S"
).strftime(date_format),
}
data["bins"].append(dict_data)
if len(json_data["organicNextDate"]) > 0:
dict_data = {
"type": "Garden Waste",
"collectionDate": datetime.strptime(
json_data["organicNextDate"], "%Y-%m-%dT%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

0 comments on commit f014a93

Please sign in to comment.