Adding Bury Council, UK (#2088)

* Adding Bury Council, UK

* Changing ID to UPRN

* Adding docs and info.md

* Adding docs and info.md

* Changing regex and uprn to ID

* reformatting + one id test case integer + change UPRN to id in md file

---------

Co-authored-by: Joe Ashworth <joe@joeashworth.io>
Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
This commit is contained in:
Joe Ashworth
2024-05-17 13:09:00 +01:00
committed by GitHub
parent bd62c25aac
commit 6895e9a9ac
4 changed files with 169 additions and 1 deletions

View File

@@ -1218,6 +1218,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Broxtowe Borough Council](/doc/source/broxtowe_gov_uk.md) / broxtowe.gov.uk
- [Buckinghamshire Waste Collection - Former Chiltern, South Bucks or Wycombe areas](/doc/source/chiltern_gov_uk.md) / chiltern.gov.uk
- [Burnley Council](/doc/source/burnley_gov_uk.md) / burnley.gov.uk
- [Bury Council](/doc/source/bury_gov_uk.md) / bury.gov.uk
- [Cambridge City Council](/doc/source/cambridge_gov_uk.md) / cambridge.gov.uk
- [Canterbury City Council](/doc/source/canterbury_gov_uk.md) / canterbury.gov.uk
- [Cardiff Council](/doc/source/cardiff_gov_uk.md) / cardiff.gov.uk

View File

@@ -0,0 +1,111 @@
import re
from datetime import datetime
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "Bury Council"
DESCRIPTION = "Source for bury.gov.uk services for Bury Council, UK."
URL = "https://bury.gov.uk"
TEST_CASES = {
"Test_Address_001": {"postcode": "bl81dd", "address": "2 Oakwood Close"},
"Test_Address_002": {"postcode": "bl8 2sg", "address": "9, BIRKDALE DRIVE"},
"Test_Address_003": {"postcode": "BL8 3DG", "address": "18, slaidburn drive"},
"Test_ID_001": {"id": 649158},
"Test_ID_002": {"id": "593456"},
}
ICON_MAP = {
"brown": "mdi:leaf",
"grey": "mdi:trash-can",
"green": "mdi:package-variant",
"blue": "mdi:bottle-soda-classic",
}
NAME_MAP = {
"brown": "Garden",
"grey": "General",
"green": "Paper/Cardboard",
"blue": "Plastic/Cans/Glass",
}
HEADERS = {
"Accept": "*/*",
"Accept-Language": "en-GB,en;q=0.9",
"Connection": "keep-alive",
"Ocp-Apim-Trace": "true",
"Origin": "https://bury.gov.uk",
"Referer": "https://bury.gov.uk",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"Sec-GPC": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
}
class Source:
def __init__(self, postcode=None, address=None, id=None):
if id is None and (postcode is None or address is None):
raise ValueError("Postcode and address must be provided")
self._id = str(id).zfill(6) if id is not None else None
self._postcode = postcode
self._address = address
def compare_address(self, address) -> bool:
return (
self._address.replace(",", "").replace(" ", "").upper()
== address.replace(",", "").replace(" ", "").upper()
)
def get_id(self, s):
url = "https://www.bury.gov.uk/app-services/getProperties"
params = {"postcode": self._postcode}
r = s.get(url, headers=HEADERS, params=params)
r.raise_for_status()
data = r.json()
if data["error"] is True:
raise ValueError("Invalid postcode")
for item in data["response"]:
if self.compare_address(item["addressLine1"]):
self._id = item["id"]
break
if self._id is None:
raise ValueError("Invalid address")
def fetch(self):
s = requests.Session()
if self._id is None:
self.get_id(s)
# Retrieve the schedule
params = {"id": self._id}
response = s.get(
"https://www.bury.gov.uk/app-services/getPropertyById",
headers=HEADERS,
params=params,
)
data = response.json()
# Define a regular expression pattern to match ordinal suffixes
ordinal_suffix_pattern = r"(?<=\d)(?:st|nd|rd|th)"
entries = []
for bin_name, bin_info in data["response"]["bins"].items():
# Remove the ordinal suffix from the date string
date_str_without_suffix = re.sub(
ordinal_suffix_pattern, "", bin_info["nextCollection"]
)
entries.append(
Collection(
date=datetime.strptime(
date_str_without_suffix,
"%A %d %B %Y",
).date(),
t=NAME_MAP[bin_name],
icon=ICON_MAP.get(bin_name),
)
)
return entries

56
doc/source/bury_gov_uk.md Normal file
View File

@@ -0,0 +1,56 @@
# Bury Council
Support for schedules provided by [Bury Council](https://www.bury.gov.uk/), serving Bury, UK.
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: bury_gov_uk
args:
id: PROPERTY_ID
postcode: POSTCODE
address: ADDRESS
```
### Configuration Variables
**id**<br>
*(string) (optional)*
**postcode**<br>
*(string) (optional)*
**address**<br>
*(string) (optional)*
## Example using UPRN
```yaml
waste_collection_schedule:
sources:
- name: bury_gov_uk
args:
id: "647186"
```
## Example using Address and Postcode
```yaml
waste_collection_schedule:
sources:
- name: bury_gov_uk
args:
address: "1 Oakwood Close"
postcode: "BL8 1DD"
```
## How to find your `PROPERTY_ID`
Your PROPERTY_ID is the collection of numbers at the end of the url when viewing your collection schedule in Developer Tools on the Bury Council web site.
For example: https://www.bury.gov.uk/app-services/getPropertyById?id=647186
You have to navigate to https://www.bury.gov.uk/waste-and-recycling/bin-collection-days-and-alerts, open Dev Tools, Select Network and then input your Postcode and select your Address. The URL should appear as network traffic.

File diff suppressed because one or more lines are too long