From 5c1dbfe0a57244ec4fa7c95651384f0ebc9799c3 Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Tue, 26 Nov 2024 22:18:52 +0100 Subject: [PATCH] add Kumberg_gv_at --- .../source/kumberg_gv_at.py | 55 +++++++++++++++++++ doc/source/kumberg_gv_at.md | 11 ++++ 2 files changed, 66 insertions(+) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/kumberg_gv_at.py create mode 100644 doc/source/kumberg_gv_at.md diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/kumberg_gv_at.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/kumberg_gv_at.py new file mode 100644 index 000000000..79f76e0b8 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/kumberg_gv_at.py @@ -0,0 +1,55 @@ +import re + +import requests +from bs4 import BeautifulSoup +from waste_collection_schedule import Collection # type: ignore[attr-defined] +from waste_collection_schedule.service.ICS import ICS + +TITLE = "Kumberg" +DESCRIPTION = "Source for Kumberg." +URL = "https://www.kumberg.gv.at" +TEST_CASES: dict[str, dict] = {"Whole Kumberg": {}} + + +ICON_MAP = { + "Restmüll": "mdi:trash-can", + "Bio": "mdi:leaf", + "Papier": "mdi:package-variant", + "Gelber Sack": "mdi:recycle", + "Sperrmüll": "mdi:sofa", +} + + +API_URL = "https://www.kumberg.gv.at/kalender/" + + +class Source: + def __init__(self): + self._ics_urls = set[str]() + self._ics = ICS() + + def _first_setup(self): + r = requests.get(API_URL) + soup = BeautifulSoup(r.text, "html.parser") + cal_icons = soup.select("i.fa-calendar-plus") + for icon in cal_icons: + if "abfalltyp" in icon.parent["href"]: + self._ics_urls.add(icon.parent["href"]) + + def fetch(self) -> list[Collection]: + if not self._ics_urls: + self._first_setup() + + collections = [] + for ics_url in self._ics_urls: + r = requests.get(ics_url) + for date_, bin_type in self._ics.convert(r.text): + # remove time like "7.00 - 9.30 Uhr" from bin_type + if re.search(r"\d{1,2}\.\d{2} - \d{1,2}\.\d{2} Uhr", bin_type): + bin_type = re.sub( + r"\d{1,2}\.\d{2} - \d{1,2}\.\d{2} Uhr", "", bin_type + ).strip() + + icon = ICON_MAP.get(bin_type) + collections.append(Collection(date_, bin_type, icon)) + return collections diff --git a/doc/source/kumberg_gv_at.md b/doc/source/kumberg_gv_at.md new file mode 100644 index 000000000..5bd9a6e2f --- /dev/null +++ b/doc/source/kumberg_gv_at.md @@ -0,0 +1,11 @@ +# Kumberg + +Support for schedules provided by [Kumberg](https://www.kumberg.gv.at), serving Kumberg, Austria. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: kumberg_gv_at +```