add wychavon_gov_uk

This commit is contained in:
5ila5
2024-06-19 19:36:41 +02:00
committed by 5ila5
parent 2f04ba6519
commit ad061f6b83
4 changed files with 118 additions and 1 deletions

View File

@@ -1415,6 +1415,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Wirral Council](/doc/source/wirral_gov_uk.md) / wirral.gov.uk
- [Woking Borough Council](/doc/source/jointwastesolutions_org.md) / woking.gov.uk
- [Wokingham Borough Council](/doc/source/wokingham_gov_uk.md) / wokingham.gov.uk
- [Wychavon District Council](/doc/source/wychavon_gov_uk.md) / wychavon.gov.uk
- [Wyre Forest District Council](/doc/source/wyreforestdc_gov_uk.md) / wyreforestdc.gov.uk
</details>

View File

@@ -0,0 +1,82 @@
from datetime import datetime
import requests
import urllib3
from bs4 import BeautifulSoup, Tag
from waste_collection_schedule import Collection # type: ignore[attr-defined]
# With verify=True the POST fails due to a SSLCertVerificationError.
# Using verify=False works, but is not ideal. The following links may provide a better way of dealing with this:
# https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
# https://urllib3.readthedocs.io/en/1.26.x/user-guide.html#ssl
# This line suppresses the InsecureRequestWarning when using verify=False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
TITLE = "Wychavon District Council"
DESCRIPTION = "Source for Wychavon District Council."
URL = "https://wychavon.gov.uk/"
TEST_CASES = {
"10013938132": {"uprn": 10013938132},
"10013938131": {"uprn": "10013938131"},
"100121280854": {"uprn": 100121280854},
}
ICON_MAP = {
"Non-recyclable": "mdi:trash-can",
"Garden": "mdi:leaf",
"Recycling": "mdi:recycle",
}
API_URL = "https://selfservice.wychavon.gov.uk/wdcroundlookup/HandleSearchScreen"
class Source:
def __init__(self, uprn: str | int):
self._uprn: str | int = uprn
def fetch(self) -> list[Collection]:
data = {"alAddrsel": self._uprn}
# get json file
r = requests.post(API_URL, data=data, verify=False)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find("table", {"class": "table table-striped"})
rows = table.find_all("tr")
entries: list[Collection] = []
for row in rows:
if not isinstance(row, Tag):
continue
tds: list[Tag] = row.find_all("td")
if len(tds) < 3:
continue
# remove everything inside tds[1] that's not text directly inside the tag
for tag in tds[1].find_all():
if not isinstance(tag, Tag):
continue
if tag.name != "br":
tag.decompose()
collection_type = tds[1].text.strip()
date_strs = [date.text.strip() for date in tds[2] if date.text.strip()]
for date_str in date_strs:
try:
# Like Thursday 11/07/2024
date = datetime.strptime(date_str, "%A %d/%m/%Y").date()
except ValueError:
continue
entries.append(
Collection(
date, collection_type, ICON_MAP.get(collection_type.split()[0])
)
)
return entries

View File

@@ -0,0 +1,34 @@
# Wychavon District Council
Support for schedules provided by [Wychavon District Council](https://wychavon.gov.uk/), serving Wychavon, UK.
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: wychavon_gov_uk
args:
uprn: "UPRN"
```
### Configuration Variables
**uprn**
*(String | Integer) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: wychavon_gov_uk
args:
uprn: "10013938132"
```
## How to get the source argument
An easy way to discover your Unique Property Reference Number (UPRN) is by going to <https://www.findmyaddress.co.uk/> and entering in your address details.

File diff suppressed because one or more lines are too long