From da3bd43dd15af5afeddcadfefa23cc57cb6d7b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20D=C3=B6rfler?= Date: Mon, 2 Dec 2024 21:51:27 +0100 Subject: [PATCH 1/3] Add optional arg to override request period for AWB Emsland --- .../source/awb_emsland_de.py | 22 +++++++++++++++++-- doc/source/awb_emsland_de.md | 7 ++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py index c048d2913..78e072687 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py @@ -55,23 +55,36 @@ def handle_starttag(self, tag, attrs): PARAM_TRANSLATIONS = { + "en": { + "year_override": "Request period override", + }, "de": { "city": "Ort", "street": "Straße", "house_number": "Hausnummer", "address_suffix": "Hausnummerzusatz", - } + "year_override": "Abfragezeitraum überschreiben", + }, } +PARAM_DESCRIPTIONS = { + "en": { + "year_override": "Request period override", + }, + "de": { + "year_override": "Abfragezeitraum überschreiben", + }, +} class Source: def __init__( - self, city: str, street: str, house_number: int, address_suffix: str = "" + self, city: str, street: str, house_number: int, address_suffix: str = "", year_override: str = "" ): self._city = city self._street = street self._hnr = house_number self._suffix = address_suffix + self._year_override = year_override self._ics = ICS() def fetch(self): @@ -93,6 +106,10 @@ def fetch(self): args["Hausnummer"] = str(self._hnr) args["Hausnummerzusatz"] = self._suffix args["SubmitAction"] = "CITYCHANGED" + + if self._year_override: + args["Zeitraum"] = self._year_override + r = session.post( SERVLET, data=args, @@ -132,3 +149,4 @@ def fetch(self): entries.append(Collection(d[0], bin_type, icon=ICON_MAP.get(bin_type))) return entries + diff --git a/doc/source/awb_emsland_de.md b/doc/source/awb_emsland_de.md index f8bb6f45d..bd112045f 100644 --- a/doc/source/awb_emsland_de.md +++ b/doc/source/awb_emsland_de.md @@ -13,6 +13,7 @@ waste_collection_schedule: street: STREET house_number: HNR address_suffix: HNR_SUFFIX + year_override: YEAR_OVERRIDE ``` ### Configuration Variables @@ -29,6 +30,9 @@ waste_collection_schedule: **address_suffix** *(string) (optional) (default: "")* +**year_override** +*(string) (optional) (default: "")* + ## Example ```yaml @@ -55,4 +59,7 @@ waste_collection_schedule: ## How to get the source arguments These values are the location you want to query for. Make sure, the writing is exactly as it is on . Typos will result in an parsing error which is printed in the log. As `house_number` expects a numeric input, address suffixes have to be provided via the `address_suffix` argument. + `address_suffix` could be for example a alphanumeric character "A" or a additional house number like "/1". + +`year_override` can be used to override the default request period, defined by the awb emsland server. Example: to query the data for the year 2024, enter "Jahresübersicht 2024" here. From 56845db5f202f46bf876dc18b49c7395cc89dcf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20D=C3=B6rfler?= Date: Wed, 4 Dec 2024 18:48:54 +0100 Subject: [PATCH 2/3] replace override field with automatic mechanism to get all available icals --- .../source/awb_emsland_de.py | 63 ++++++++++++++----- doc/source/awb_emsland_de.md | 6 -- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py index 78e072687..69e2d8518 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py @@ -3,6 +3,7 @@ from html.parser import HTMLParser import requests +from bs4 import BeautifulSoup from waste_collection_schedule import Collection # type: ignore[attr-defined] from waste_collection_schedule.service.ICS import ICS @@ -55,39 +56,67 @@ def handle_starttag(self, tag, attrs): PARAM_TRANSLATIONS = { - "en": { - "year_override": "Request period override", - }, "de": { "city": "Ort", "street": "Straße", "house_number": "Hausnummer", "address_suffix": "Hausnummerzusatz", - "year_override": "Abfragezeitraum überschreiben", - }, + } } -PARAM_DESCRIPTIONS = { - "en": { - "year_override": "Request period override", - }, - "de": { - "year_override": "Abfragezeitraum überschreiben", - }, -} class Source: def __init__( - self, city: str, street: str, house_number: int, address_suffix: str = "", year_override: str = "" + self, city: str, street: str, house_number: int, address_suffix: str = "" ): self._city = city self._street = street self._hnr = house_number self._suffix = address_suffix - self._year_override = year_override self._ics = ICS() + + def get_zeitraum_values(self): + try: + session = requests.session() + + response = session.get( + SERVLET, + params={"SubmitAction": "wasteDisposalServices"}, + ) + response.raise_for_status() + + # Parse the HTML content + soup = BeautifulSoup(response.text, 'html.parser') + + # Find all input elements with the name "Zeitraum" + input_elements = soup.find_all('input', {'name': 'Zeitraum'}) + + # Extract values + values = [input_elem.get('value') for input_elem in input_elements if input_elem.get('value')] + return values + + except requests.exceptions.RequestException as e: + return [] + + def fetch(self): + available_years = self.get_zeitraum_values() + + if available_years: + values = [] + + for year in available_years: + result = self.fetch_year(year) + values.extend(result) + + return values + + else: + return self.fetch_year() + + + def fetch_year(self, year=None): session = requests.session() r = session.get( @@ -107,8 +136,8 @@ def fetch(self): args["Hausnummerzusatz"] = self._suffix args["SubmitAction"] = "CITYCHANGED" - if self._year_override: - args["Zeitraum"] = self._year_override + if year: + args["Zeitraum"] = year r = session.post( SERVLET, diff --git a/doc/source/awb_emsland_de.md b/doc/source/awb_emsland_de.md index bd112045f..f90797e18 100644 --- a/doc/source/awb_emsland_de.md +++ b/doc/source/awb_emsland_de.md @@ -13,7 +13,6 @@ waste_collection_schedule: street: STREET house_number: HNR address_suffix: HNR_SUFFIX - year_override: YEAR_OVERRIDE ``` ### Configuration Variables @@ -30,9 +29,6 @@ waste_collection_schedule: **address_suffix** *(string) (optional) (default: "")* -**year_override** -*(string) (optional) (default: "")* - ## Example ```yaml @@ -61,5 +57,3 @@ waste_collection_schedule: These values are the location you want to query for. Make sure, the writing is exactly as it is on . Typos will result in an parsing error which is printed in the log. As `house_number` expects a numeric input, address suffixes have to be provided via the `address_suffix` argument. `address_suffix` could be for example a alphanumeric character "A" or a additional house number like "/1". - -`year_override` can be used to override the default request period, defined by the awb emsland server. Example: to query the data for the year 2024, enter "Jahresübersicht 2024" here. From 8b7b083156cfad548d9324b85ff5fb6795f0434f Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:30:59 +0100 Subject: [PATCH 3/3] reformatting --- .../source/awb_emsland_de.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py index 69e2d8518..475564f27 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awb_emsland_de.py @@ -75,7 +75,6 @@ def __init__( self._suffix = address_suffix self._ics = ICS() - def get_zeitraum_values(self): try: session = requests.session() @@ -87,18 +86,21 @@ def get_zeitraum_values(self): response.raise_for_status() # Parse the HTML content - soup = BeautifulSoup(response.text, 'html.parser') + soup = BeautifulSoup(response.text, "html.parser") # Find all input elements with the name "Zeitraum" - input_elements = soup.find_all('input', {'name': 'Zeitraum'}) + input_elements = soup.find_all("input", {"name": "Zeitraum"}) # Extract values - values = [input_elem.get('value') for input_elem in input_elements if input_elem.get('value')] + values = [ + input_elem.get("value") + for input_elem in input_elements + if input_elem.get("value") + ] return values - except requests.exceptions.RequestException as e: + except requests.exceptions.RequestException: return [] - def fetch(self): available_years = self.get_zeitraum_values() @@ -111,12 +113,11 @@ def fetch(self): values.extend(result) return values - + else: return self.fetch_year() - - def fetch_year(self, year=None): + def fetch_year(self, year=None): session = requests.session() r = session.get( @@ -178,4 +179,3 @@ def fetch_year(self, year=None): entries.append(Collection(d[0], bin_type, icon=ICON_MAP.get(bin_type))) return entries -