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..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 @@ -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 @@ -74,7 +75,49 @@ def __init__( self._suffix = address_suffix 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: + 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( @@ -93,6 +136,10 @@ def fetch(self): args["Hausnummer"] = str(self._hnr) args["Hausnummerzusatz"] = self._suffix args["SubmitAction"] = "CITYCHANGED" + + if year: + args["Zeitraum"] = year + r = session.post( SERVLET, data=args, diff --git a/doc/source/awb_emsland_de.md b/doc/source/awb_emsland_de.md index f8bb6f45d..f90797e18 100644 --- a/doc/source/awb_emsland_de.md +++ b/doc/source/awb_emsland_de.md @@ -55,4 +55,5 @@ 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".