add_southkesteven_gov_uk (#2159)

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
This commit is contained in:
5ila5
2024-06-18 19:00:52 +02:00
committed by GitHub
parent 2065fae437
commit 50e4c2c204
4 changed files with 76 additions and 14 deletions

View File

@@ -1366,6 +1366,7 @@ Waste collection schedules in the following formats and countries are supported.
- [South Gloucestershire Council](/doc/source/southglos_gov_uk.md) / southglos.gov.uk
- [South Hams District Council](/doc/source/fccenvironment_co_uk.md) / southhams.gov.uk
- [South Holland District Council](/doc/source/sholland_gov_uk.md) / sholland.gov.uk
- [South Kesteven District Council](/doc/source/southkesteven_gov_uk.md) / southkesteven.gov.uk
- [South Norfolk Council](/doc/source/south_norfolk_and_broadland_gov_uk.md) / southnorfolkandbroadland.gov.uk
- [South Oxfordshire District Council](/doc/source/binzone_uk.md) / southoxon.gov.uk
- [South Somerset District Council](/doc/source/iweb_itouchvision_com.md) / somerset.gov.uk

View File

@@ -1,5 +1,6 @@
import logging
import re
from datetime import datetime
from datetime import datetime, timedelta
import bs4
import requests
@@ -7,10 +8,13 @@ from waste_collection_schedule import Collection # type: ignore[attr-defined]
URL = "https://southkesteven.gov.uk"
TEST_CASES = {
"Test_001": {"address_id": 70158},
"Long Bennington": {"address_id": 33399},
"Bourne": {"address_id": "7351"},
"Grantham": {"address_id": 18029},
}
_LOGGER = logging.getLogger(__name__)
ICON_MAP = {
"Black": "mdi:trash-can",
"black": "mdi:trash-can",
"gray": "mdi:recycle",
"purple": "mdi:newspaper",
}
@@ -38,25 +42,35 @@ class Source:
r.raise_for_status()
collections = []
print(r.text)
soup = bs4.BeautifulSoup(r.content, "html.parser")
date_translate = {
"Tomorrow": datetime.now().date() + timedelta(days=1),
"Today": datetime.now().date(),
}
# Find next collection
# find p looks like <p>Your next bin collection date is <span class="alert__heading alpha">Wed 19 June 2024</span></p>
next_collection_p = soup.find(
"p", text=re.compile("Your next bin collection date is")
lambda tag: tag.name == "p"
and "Your next bin collection date is" in tag.text
)
# above does not work so try this
if next_collection_p is not None:
if next_collection_p is None:
_LOGGER.warning(
"No next collection text found, continuing to look for future collections"
)
else:
date_str = next_collection_p.find("span").text
date = datetime.strptime(date_str, "%A %d %B %Y").date()
date = (
date_translate.get(date_str)
or datetime.strptime(date_str, "%A %d %B %Y").date()
)
bin_type = NEXT_BIN_TYPE_REGEX.search(
next_collection_p.find_next("p").text
).group(1)
collections.append(Collection(date, bin_type, ICON_MAP.get(bin_type)))
else:
raise ValueError("No next collection found")
# Find all Future collections
s = soup.find_all("h3", text="Future collections")
@@ -70,13 +84,28 @@ class Source:
1
)
print(collection_futher_info_link)
for collection in collections_list.find_next_sibling("ul").find_all("li"):
# like: "Thu 29 August 2024"
date_str = collection.text
date = datetime.strptime(date_str, "%a %d %B %Y").date()
try:
date = (
date_translate.get(date_str)
or datetime.strptime(date_str, "%a %d %B %Y").date()
)
except ValueError:
_LOGGER.warning(
f"Failed to parse date {date_str}, skipping this collection"
)
continue
collections.append(Collection(date, bin_type, ICON_MAP.get(bin_type)))
return []
# filter out duplicate entries
collections = list(
{
(collection.date, collection.type): collection
for collection in collections
}.values()
)
return collections

View File

@@ -0,0 +1,32 @@
# South Kesteven District Council
Support for schedules provided by [South Kesteven District Council](https://southkesteven.gov.uk)
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: southkesteven_gov_uk
args:
address_id: ADDRESS_ID
```
### Configuration Variables
**address_id**
*(int|string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: southkesteven_gov_uk
args:
address_id: "33399"
```
## How to find your `address_id`
Go to the [South Kesteven bin collection](https://pre.southkesteven.gov.uk/BinSearch.aspx) website and open search for your address. Either inspect the page where you would select your address (after postcode search) where you can find the `address_id` as value of the `value` attribute of the `option` tag containing your address. Alternatively, you can switch to the network tab of your browser's developer tools and click on "View your bin days" to see the request to the server. The `address_id` is in the payload of the request.

File diff suppressed because one or more lines are too long