Skip to content

Commit

Permalink
AWB Emsland request this AND next years data instead of only next yea…
Browse files Browse the repository at this point in the history
…rs (#3134)

* Add optional arg to override request period for AWB Emsland

* replace override field with automatic mechanism to get all available icals

* reformatting

---------

Co-authored-by: 5ila5 <[email protected]>
  • Loading branch information
niklasdoerfler and 5ila5 authored Dec 12, 2024
1 parent 0eaa5e4 commit 740922e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions doc/source/awb_emsland_de.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://www.awb-emsland.de/service/abfuhrkalender/>. 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".

0 comments on commit 740922e

Please sign in to comment.