From 9f10a415a7d7cfdfeae0a947a0168baf3f0423d1 Mon Sep 17 00:00:00 2001 From: Rob Tribe Date: Fri, 5 Jan 2024 13:55:20 +0000 Subject: [PATCH] Tewkesbury Borough Council API URL change + remove postcode option (#1613) * Update Tewkesbury API URL and remove postcode option The API URL for Tewkesbury Borough council has changed, and the postcode option no longer works, so this has been removed. * Remove postcode option from Tewksbury Borough Council Reflect changes in docs to remove postcode option from Tewksbury borough council * allow postcode as depricated method --------- Co-authored-by: 5ila5 <5ila5@users.noreply.github.com> --- .../source/tewkesbury_gov_uk.py | 45 ++++++++++++------- doc/source/tewkesbury_gov_uk.md | 26 +---------- 2 files changed, 32 insertions(+), 39 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/tewkesbury_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/tewkesbury_gov_uk.py index 8c46f228..92e39285 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/tewkesbury_gov_uk.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/tewkesbury_gov_uk.py @@ -1,19 +1,21 @@ +import logging from datetime import datetime from urllib.parse import quote as urlquote import requests -from waste_collection_schedule import Collection +from waste_collection_schedule import Collection # type: ignore[attr-defined] TITLE = "Tewkesbury Borough Council" DESCRIPTION = "Home waste collection schedule for Tewkesbury Borough Council" URL = "https://www.tewkesbury.gov.uk" TEST_CASES = { - "Council Office": {"postcode": "GL20 5TT"}, - "Council Office No Spaces": {"postcode": "GL205TT"}, "UPRN example": {"uprn": 100120544973}, + "Deprecated postcode": {"postcode": "GL20 5TT"}, + "Deprecated postcode No Spaces": {"postcode": "GL205TT"}, } -API_URL = "https://api-2.tewkesbury.gov.uk/general/rounds/%s/nextCollection" +DEPRICATED_API_URL = "https://api-2.tewkesbury.gov.uk/general/rounds/%s/nextCollection" +API_URL = "https://api-2.tewkesbury.gov.uk/incab/rounds/%s/next-collection" ICON_MAP = { "Refuse": "mdi:trash-can", @@ -22,17 +24,28 @@ ICON_MAP = { "Food": "mdi:silverware-fork-knife", } +LOGGER = logging.getLogger(__name__) + class Source: - def __init__(self, postcode: str = None, uprn: str = None): - self._post_or_uprn = str(uprn) if uprn else postcode + def __init__(self, postcode: str | None = None, uprn: str | None = None): + self.urpn = str(uprn) if uprn is not None else None + self.postcode = str(postcode) if postcode is not None else None def fetch(self): - if self._post_or_uprn is None: - raise Exception("postcode not set") + if self.urpn is None: + LOGGER.warning( + "Using deprecated API might not work in the future. Please provide a UPRN." + ) + return self.get_data(self.postcode, DEPRICATED_API_URL) + return self.get_data(self.urpn) - encoded_postcode = urlquote(self._post_or_uprn) - request_url = API_URL % encoded_postcode + def get_data(self, uprn, api_url=API_URL): + if uprn is None: + raise Exception("UPRN not set") + + encoded_urpn = urlquote(uprn) + request_url = api_url % encoded_urpn response = requests.get(request_url) response.raise_for_status() @@ -41,18 +54,20 @@ class Source: entries = [] if data["status"] != "OK": - raise Exception(f"Error fetching data. \"{data['status']}\": \n {data['body']}") - + raise Exception( + f"Error fetching data. \"{data['status']}\": \n {data['body']}" + ) + schedule = data["body"] for schedule_entry in schedule: entries.append( Collection( date=datetime.strptime( - schedule_entry["NextCollection"], "%Y-%m-%d").date(), + schedule_entry["NextCollection"], "%Y-%m-%d" + ).date(), t=schedule_entry["collectionType"], - icon=ICON_MAP.get(schedule_entry["collectionType"]) + icon=ICON_MAP.get(schedule_entry["collectionType"]), ) ) - return entries diff --git a/doc/source/tewkesbury_gov_uk.md b/doc/source/tewkesbury_gov_uk.md index 64d843aa..4699dc7c 100644 --- a/doc/source/tewkesbury_gov_uk.md +++ b/doc/source/tewkesbury_gov_uk.md @@ -1,17 +1,9 @@ # Tewkesbury City Council -Support for upcoming schedules provided by [Tewkesbury City Council](https://www.tewkesbury.gov.uk/waste-and-recycling), serving Tewkesbury (UK) and areas of Gloucestershire. +Support for upcoming schedules provided by [Tewkesbury City Council](https://tewkesbury.gov.uk/services/waste-and-recycling/), serving Tewkesbury (UK) and areas of Gloucestershire. ## Configuration via configuration.yaml -```yaml -waste_collection_schedule: - sources: - - name: tewkesbury_gov_uk - args: - postcode: POSTCODE -``` - ```yaml waste_collection_schedule: sources: @@ -22,25 +14,11 @@ waste_collection_schedule: ### Configuration Variables -**POSTCODE** -*(string) (optional)* -Not all addresses are supported by postcode. Then you have to provide a UPRN. - **UPRN** -*(string) (optional)* - -Either `postcode` or `uprn` must be provided. +*(string) (required)* ## Example -```yaml -waste_collection_schedule: - sources: - - name: tewkesbury_gov_uk - args: - postcode: "GL20 5TT" -``` - ```yaml waste_collection_schedule: sources: