mirror of
https://github.com/sascha-hemi/hacs_waste_collection_schedule.git
synced 2026-03-21 04:06:03 +01:00
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>
This commit is contained in:
@@ -1,19 +1,21 @@
|
|||||||
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from urllib.parse import quote as urlquote
|
from urllib.parse import quote as urlquote
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from waste_collection_schedule import Collection
|
from waste_collection_schedule import Collection # type: ignore[attr-defined]
|
||||||
|
|
||||||
TITLE = "Tewkesbury Borough Council"
|
TITLE = "Tewkesbury Borough Council"
|
||||||
DESCRIPTION = "Home waste collection schedule for Tewkesbury Borough Council"
|
DESCRIPTION = "Home waste collection schedule for Tewkesbury Borough Council"
|
||||||
URL = "https://www.tewkesbury.gov.uk"
|
URL = "https://www.tewkesbury.gov.uk"
|
||||||
TEST_CASES = {
|
TEST_CASES = {
|
||||||
"Council Office": {"postcode": "GL20 5TT"},
|
|
||||||
"Council Office No Spaces": {"postcode": "GL205TT"},
|
|
||||||
"UPRN example": {"uprn": 100120544973},
|
"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 = {
|
ICON_MAP = {
|
||||||
"Refuse": "mdi:trash-can",
|
"Refuse": "mdi:trash-can",
|
||||||
@@ -22,17 +24,28 @@ ICON_MAP = {
|
|||||||
"Food": "mdi:silverware-fork-knife",
|
"Food": "mdi:silverware-fork-knife",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Source:
|
class Source:
|
||||||
def __init__(self, postcode: str = None, uprn: str = None):
|
def __init__(self, postcode: str | None = None, uprn: str | None = None):
|
||||||
self._post_or_uprn = str(uprn) if uprn else postcode
|
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):
|
def fetch(self):
|
||||||
if self._post_or_uprn is None:
|
if self.urpn is None:
|
||||||
raise Exception("postcode not set")
|
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)
|
def get_data(self, uprn, api_url=API_URL):
|
||||||
request_url = API_URL % encoded_postcode
|
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 = requests.get(request_url)
|
||||||
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
@@ -41,18 +54,20 @@ class Source:
|
|||||||
entries = []
|
entries = []
|
||||||
|
|
||||||
if data["status"] != "OK":
|
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"]
|
schedule = data["body"]
|
||||||
for schedule_entry in schedule:
|
for schedule_entry in schedule:
|
||||||
entries.append(
|
entries.append(
|
||||||
Collection(
|
Collection(
|
||||||
date=datetime.strptime(
|
date=datetime.strptime(
|
||||||
schedule_entry["NextCollection"], "%Y-%m-%d").date(),
|
schedule_entry["NextCollection"], "%Y-%m-%d"
|
||||||
|
).date(),
|
||||||
t=schedule_entry["collectionType"],
|
t=schedule_entry["collectionType"],
|
||||||
icon=ICON_MAP.get(schedule_entry["collectionType"])
|
icon=ICON_MAP.get(schedule_entry["collectionType"]),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
return entries
|
return entries
|
||||||
|
|||||||
@@ -1,17 +1,9 @@
|
|||||||
# Tewkesbury City Council
|
# 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
|
## Configuration via configuration.yaml
|
||||||
|
|
||||||
```yaml
|
|
||||||
waste_collection_schedule:
|
|
||||||
sources:
|
|
||||||
- name: tewkesbury_gov_uk
|
|
||||||
args:
|
|
||||||
postcode: POSTCODE
|
|
||||||
```
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
waste_collection_schedule:
|
waste_collection_schedule:
|
||||||
sources:
|
sources:
|
||||||
@@ -22,25 +14,11 @@ waste_collection_schedule:
|
|||||||
|
|
||||||
### Configuration Variables
|
### Configuration Variables
|
||||||
|
|
||||||
**POSTCODE**
|
|
||||||
*(string) (optional)*
|
|
||||||
Not all addresses are supported by postcode. Then you have to provide a UPRN.
|
|
||||||
|
|
||||||
**UPRN**
|
**UPRN**
|
||||||
*(string) (optional)*
|
*(string) (required)*
|
||||||
|
|
||||||
Either `postcode` or `uprn` must be provided.
|
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```yaml
|
|
||||||
waste_collection_schedule:
|
|
||||||
sources:
|
|
||||||
- name: tewkesbury_gov_uk
|
|
||||||
args:
|
|
||||||
postcode: "GL20 5TT"
|
|
||||||
```
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
waste_collection_schedule:
|
waste_collection_schedule:
|
||||||
sources:
|
sources:
|
||||||
|
|||||||
Reference in New Issue
Block a user