From a21168b5cdf3b4e6e49a6ea74b58ced8c9ed1c9b Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Thu, 21 Dec 2023 18:18:57 +0100 Subject: [PATCH] umwelverbaende_at now supports calendar option + ties fetch next year in Dec --- .../source/umweltverbaende_at.py | 40 ++++++++++++++----- doc/source/umweltverbaende_at.md | 35 ++++++++++++++-- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/umweltverbaende_at.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/umweltverbaende_at.py index 70ee7684..6a511f96 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/umweltverbaende_at.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/umweltverbaende_at.py @@ -2,7 +2,7 @@ from datetime import datetime import requests from bs4 import BeautifulSoup -from waste_collection_schedule import Collection +from waste_collection_schedule import Collection # type: ignore[attr-defined] TITLE = "Die NÖ Umweltverbände" DESCRIPTION = ( @@ -138,6 +138,11 @@ EXTRA_INFO = [ ] TEST_CASES = { + "krems Langenlois": { + "district": "krems", + "municipal": "Langenlois", + "calendar": "Gobelsburg, Mittelberg, Reith, Schiltern, Zöbing", + }, # "Amstetten": {"district": "amstetten", "municipal": "?"}, # No schedules listed on website "Bruck/Leitha": {"district": "bruck", "municipal": "Berg"}, "Baden": {"district": "baden", "municipal": "Hernstein"}, @@ -148,7 +153,8 @@ TEST_CASES = { "Klosterneuburg": {"district": "klosterneuburg", "municipal": "Klosterneuburg"}, "Korneuburg": {"district": "korneuburg", "municipal": "Bisamberg"}, "Krems": {"district": "krems", "municipal": "Aggsbach"}, - "Stadt Krems": {"district": "kremsstadt", "municipal": "Rehberg"}, + "Stadt Krems Old Version": {"district": "kremsstadt", "municipal": "Rehberg"}, + "Stadt Krems New Version": {"district": "kremsstadt", "calendar": "Rehberg"}, "Lilienfeld": {"district": "lilienfeld", "municipal": "Annaberg"}, # "Laa/Thaya": {"district": "laa", "municipal": "Staatz"}, # schedules use www.gaul-laa.at "Mödling": {"district": "moedling", "municipal": "Wienerwald"}, @@ -179,9 +185,15 @@ ICON_MAP = { class Source: - def __init__(self, district, municipal): + def __init__(self, district, municipal=None, calendar=None): self._district = district.lower() self._municipal = municipal + self._calendar = calendar.lower() if calendar else None + if ( + district == "kremsstadt" and not calendar + ): # Keep compatibility with old configs + self._calendar = self._municipal + self._municipal = None def get_icon(self, waste_text: str) -> str: for waste in ICON_MAP: @@ -200,17 +212,28 @@ class Source: return def fetch(self): + now = datetime.now() + entries = self.get_data(now.year) + if now.month != 12: + return entries + try: + entries.extend(self.get_data(now.year + 1)) + except Exception: + pass + return entries + + def get_data(self, year): s = requests.Session() # Select appropriate url, the "." allows stpoelten/stpoeltenland and krems/kremsstadt to be distinguished for item in EXTRA_INFO: if (self._district.lower() + ".") in item["url"]: district_url = item["url"] - r0 = s.get(f"{district_url}?kat=32") + r0 = s.get(f"{district_url}?kat=32&jahr={year}") soup = BeautifulSoup(r0.text, "html.parser") # Get list of municipalities and weblinks # kremsstadt lists collections for all municipals on the main page so skip that district - if self._district != "kremsstadt": + if self._municipal: table = soup.find_all("div", {"class": "col-sm-9"}) for item in table: weblinks = item.find_all("a", {"class": "weblink"}) @@ -226,10 +249,9 @@ class Source: entries = [] for day in schedule: txt = day.text.strip().split(" \u00a0") - if ( - self._district == "kremsstadt" - ): # Filter for kremstadt rayon here because it was skipped earlier - if self._municipal.upper() in txt[2].upper(): + if self._calendar: # Filter for calendar if there are multiple calendars + if self._calendar.upper() in txt[2].upper(): + txt[2] = txt[2].split(":")[-1].strip() self.append_entry(entries, txt) else: # Process all other municipals self.append_entry(entries, txt) diff --git a/doc/source/umweltverbaende_at.md b/doc/source/umweltverbaende_at.md index 2cd24293..32acbb89 100644 --- a/doc/source/umweltverbaende_at.md +++ b/doc/source/umweltverbaende_at.md @@ -10,7 +10,8 @@ waste_collection_schedule: - name: umweltverbaende_at args: district: DISTRICT_ARG - municipal: MUNICIPAL/RAYON + municipal: MUNICIPAL + calendar: CALENDAR ``` **district** @@ -19,12 +20,19 @@ waste_collection_schedule: Lower Austrian district, see table below for valid DISTRICT_ARG **muncipal** -*(string) (required)* +*(string) (optional)* Municipal within the district for which the collection schedule is required. The municipal should be spelt as it appears on the Abholtermine page. There is no need to include the "Marktgemeinde", "Gemeinde", or "Stadtgeminde" text. +Is not needed for Stadt Krems you should provide a calendar for each Rayon. + +*deprecated (still works for Stadt Krems)*: For Stadt Krems, the district is divided into 12 Rayon, so supply your Rayon name for the municipal arg. For example: _Rehberg (Rayon 30)_ would be `Rehberg`, whereas _Innenstadt 2 (Rayon 200)_ would be `Innenstadt 2` +**calendar** +(string) (optional) + +If you see multiple collection calendars for your municipal (different streets or Rayons), you can specify the calendar name here. The calendar name should be spelt as it appears on the Abholtermine page below `Kalenderansicht`. ## Examples @@ -37,6 +45,7 @@ waste_collection_schedule: municipal: "Marchegg" # Municipal ``` + ```yaml waste_collection_schedule: sources: @@ -44,8 +53,28 @@ waste_collection_schedule: args: district: "waidhofen" # Waidhofen/Thaya municipal: "Kautzen" # Municipal - ``` + +```yaml +waaste_collection_schedule: + sources: + - name: umweltverbaende_at + args: + district: "krems" # Krems + municipal: "Langenlois" # Municipal + calendar: "Gobelsburg, Mittelberg, Reith, Schiltern, Zöbing" # Rayon +``` + +```yaml +waste_collection_schedule: + sources: + - name: umweltverbaende_at + args: + district: "kermsstadt" # Stadt Krems + calendar: "Rehberg" # Rayon +``` + +*Old Version* ```yaml waste_collection_schedule: sources: