fix aha_region_de + add ladeort argument

This commit is contained in:
5ila5
2024-08-02 22:06:02 +02:00
committed by 5ila5
parent ab14459e60
commit 21c8269958
2 changed files with 65 additions and 36 deletions

View File

@@ -30,6 +30,12 @@ TEST_CASES = {
"hnr": "10",
"zusatz": "A",
},
"Mit Ladeort": {
"gemeinde": "Gehrden",
"strasse": "Kirchstr. / Gehrden",
"hnr": "1",
"ladeort": "Kirchstr. 6, Gehrden / Gehrden",
},
}
ICON_MAP = {
@@ -46,12 +52,18 @@ LOGGER = logging.getLogger(__name__)
class Source:
def __init__(
self, gemeinde: str, strasse: str, hnr: str | int, zusatz: str | int = ""
self,
gemeinde: str,
strasse: str,
hnr: str | int,
zusatz: str | int = "",
ladeort=None,
):
self._gemeinde: str = gemeinde
self._strasse: str = strasse
self._hnr: str = str(hnr)
self._zusatz: str = str(zusatz)
self._ladeort: str | None = ladeort
self._ics = ICS()
def fetch(self):
@@ -94,42 +106,41 @@ class Source:
r = requests.post(API_URL, data=args)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
# find all ICAL download buttons
download_buttons = soup.find_all("button", {"name": "ical_apple"})
ladeort_single = soup.find("input", {"id": "ladeort"})
if not download_buttons:
if not ladeort_single:
ladeort_select = soup.find("select", {"id": "ladeort"})
if not ladeort_select:
raise Exception("No ladeort found")
ladeort_options = ladeort_select.find_all("option")
if not self._ladeort:
raise Exception(
"Invalid response from server, check you configuration if it is correct."
f"Ladeort required for this address, use one of {[ladeort_option.text for ladeort_option in ladeort_options]}"
)
for ladeort_option in ladeort_options:
if ladeort_option.text.lower().replace(
" ", ""
) == self._ladeort.lower().replace(" ", ""):
ladeort_single = ladeort_option
break
if not ladeort_single:
raise Exception(
f"Ladeort not found: {self._ladeort}, use one of {[ladeort_option.text for ladeort_option in ladeort_options]}"
)
entries = []
for button in download_buttons:
# get form data and request ICAL file for every waste type
args = {}
args["ical_apple"] = button["value"]
form = button.parent
for input in form.find_all("input"):
args[input["name"]] = input["value"]
del args["anzeigen"]
args["ladeort"] = ladeort_single["value"]
args["ical"] = "ICAL Jahresübersicht"
r = requests.post(API_URL, data=args)
r.raise_for_status()
r.encoding = "utf-8"
try:
dates = self._ics.convert(r.text)
except ValueError as e:
waste_type = (
button["data-fraktionsname"]
if button.has_attr("data-fraktionsname")
else "unknown"
)
waste_id = button["value"]
LOGGER.warning(
f"No data found for wastetype: {waste_type} with id {waste_id}. The Ical file is empty or corrupted. Failed with error: {e}"
)
continue
raise Exception("got invalid ics file") from e
entries = []
for d in dates:
bin_type = d[1].replace("Abfuhr", "").strip()
entries.append(Collection(d[0], bin_type, ICON_MAP.get(bin_type)))

View File

@@ -30,8 +30,13 @@ waste_collection_schedule:
**zusatz**
*(String | Integer) (optional)*
**ladeort**
*(String | Integer) (optional)* Only required if you need to specify a Abholpaltz on the website
## Example
## Most addresses
```yaml
waste_collection_schedule:
sources:
@@ -44,6 +49,19 @@ waste_collection_schedule:
```
## Addresses requirering Abholpaltz
```yaml
waste_collection_schedule:
sources:
- name: aha_region_de
args:
gemeinde: Gehrden
strasse: Kirchstr. / Gehrden
hnr: "1"
ladeort: "Kirchstr. 6, Gehrden / Gehrden"
```
## How to get the source argument
Find the parameter of your address using [https://www.aha-region.de/abholtermine/abfuhrkalender](https://www.aha-region.de/abholtermine/abfuhrkalender) and write them exactly like on the web page.