add odby_wingston_gov_uk

It is a nearly 1:1 copy of the charnwood_gov_uk source with changed urls
This commit is contained in:
5ila5
2024-09-08 15:11:29 +02:00
committed by 5ila5
parent 3fe8ed0ed4
commit af828fd50c
5 changed files with 158 additions and 1 deletions

View File

@@ -1717,6 +1717,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [North Yorkshire Council - Scarborough](/doc/source/northyorks_scarborough_gov_uk.md) / northyorks.gov.uk
- [North Yorkshire Council - Selby](/doc/source/northyorks_selby_gov_uk.md) / northyorks.gov.uk
- [Nottingham City Council](/doc/source/nottingham_city_gov_uk.md) / nottinghamcity.gov.uk
- [Oadby and Wigston Council](/doc/source/oadby_wigston_gov_uk.md) / oadby-wigston.gov.uk
- [Oxford City Council](/doc/source/oxford_gov_uk.md) / oxford.gov.uk
- [Peterborough City Council](/doc/source/peterborough_gov_uk.md) / peterborough.gov.uk
- [Portsmouth City Council](/doc/source/portsmouth_gov_uk.md) / portsmouth.gov.uk

View File

@@ -9338,6 +9338,11 @@
"module": "nottingham_city_gov_uk",
"default_params": {}
},
{
"title": "Oadby and Wigston Council",
"module": "oadby_wigston_gov_uk",
"default_params": {}
},
{
"title": "Oxford City Council",
"module": "oxford_gov_uk",

View File

@@ -0,0 +1,119 @@
# This is a nearly 1:1 copy of the `Source` class from `charnwood_gov_uk.py`. The only difference is the `API_URL` and `SEARCH_URL` variables.
from datetime import date, timedelta
import requests
from bs4 import BeautifulSoup
from dateutil.parser import parse
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "Oadby and Wigston Council"
DESCRIPTION = "Source for Oadby and Wigston Council."
URL = "https://www.oadby-wigston.gov.uk"
TEST_CASES = {
"111, Main Street, Swithland": {
"address": "56, Sussex Road, Wigston, Leicestershire"
},
"2, The Banks, Sileby": {
"address": "89, Leicester Road, Leicester, Leicestershire"
},
}
ICON_MAP = {
"Refuse": "mdi:trash-can",
"Garden Waste": "mdi:leaf",
"Recycling": "mdi:recycle",
}
API_URL = "https://my.oadby-wigston.gov.uk/my-property-finder"
SEARCH_URL = "https://my.oadby-wigston.gov.uk/data/ac/addresses.json"
class Source:
def __init__(self, address: str):
self._address_search: str = address
self._address_compare: str = address.lower().replace(" ", "").replace(",", "")
self._address_id = None
def _match_address(self, address: str) -> bool:
return (
address.lower().replace(" ", "").replace(",", "") == self._address_compare
)
@staticmethod
def _parse_date(date_str: str) -> date:
if date_str.lower() == "today":
return date.today()
if date_str.lower() == "tomorrow":
return date.today() + timedelta(days=1)
return parse(date_str).date()
def _get_address_id(self):
params = {
"term": self._address_search,
}
r = requests.get(SEARCH_URL, params=params)
r.raise_for_status()
data = r.json()
if not data:
raise ValueError(
"No address found for search term: " + self._address_search
)
for address in data:
if self._match_address(address["label"]):
self._address_id = address["value"]
return
raise ValueError(
"Address not found, use one of the following: "
+ ", ".join([address["label"] for address in data])
)
def fetch(self) -> list[Collection]:
fresh_id = False
if not self._address_id:
self._get_address_id()
fresh_id = True
try:
return self._get_collections()
except Exception:
if fresh_id:
raise
self._get_address_id()
return self._get_collections()
def _get_collections(self) -> list[Collection]:
if not self._address_id:
raise ValueError("Address not set")
args = {"address_id": self._address_id}
# get json file
r = requests.get(API_URL, params=args)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
collection_panel = soup.find("div", {"class": "refusecollectiondates"})
if not collection_panel:
raise ValueError("No collection panel found")
entries = []
for li in collection_panel.select("li"):
date_tag = li.find("strong")
if not date_tag:
continue
date_str = date_tag.text.strip()
waste_type_tag = date_tag.find_next("a")
if not waste_type_tag:
continue
waste_type = waste_type_tag.text.strip()
date_ = self._parse_date(date_str)
entries.append(Collection(date_, waste_type, icon=ICON_MAP.get(waste_type)))
return entries

View File

@@ -0,0 +1,32 @@
# Charnwood
Support for schedules provided by [Oadby and Wigston Council](https://www.oadby-wigston.gov.uk), serving Charnwood, UK.
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: oadby_wigston_gov_uk
args:
address: ADDRESS
```
### Configuration Variables
**address**
*(String) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: oadby_wigston_gov_uk
args:
address: 89, Leicester Road, Leicester, Leicestershire
```
## How to get the source argument
You can check [https://my.oadby-wigston.gov.uk/](https://my.oadby-wigston.gov.uk/). The address should exactly match the autocomplete suggestion.

File diff suppressed because one or more lines are too long