-
-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <[email protected]> Co-authored-by: 5ila5 <[email protected]>
- Loading branch information
1 parent
bd62c25
commit 6895e9a
Showing
4 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
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
111 changes: 111 additions & 0 deletions
111
custom_components/waste_collection_schedule/waste_collection_schedule/source/bury_gov_uk.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,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 |
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,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. |
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