mirror of
https://github.com/sascha-hemi/hacs_waste_collection_schedule.git
synced 2026-03-21 05:06:33 +01:00
fix rh_entsorgung_de
see also bmv_at
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import re
|
||||
from datetime import date
|
||||
from html.parser import HTMLParser
|
||||
|
||||
import requests
|
||||
from waste_collection_schedule import Collection # type: ignore[attr-defined]
|
||||
from waste_collection_schedule.service.ICS import ICS
|
||||
|
||||
TITLE = "RH Entsorgung"
|
||||
DESCRIPTION = "Source for RHE (Rhein Hunsrück Entsorgung)."
|
||||
@@ -40,70 +39,24 @@ class HiddenInputParser(HTMLParser):
|
||||
self._args[d["name"]] = d["value"] if "value" in d else ""
|
||||
|
||||
|
||||
class CollectionParser(HTMLParser):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._entries: list[Collection] = []
|
||||
self._current_type: str = None
|
||||
self._capture_type: bool = False
|
||||
self._capture_date: bool = False
|
||||
self._date_pattern = re.compile(
|
||||
r"(?P<day>\d{2})\.(?P<month>\d{2})\.(?P<year>\d{4})"
|
||||
)
|
||||
|
||||
@property
|
||||
def entries(self):
|
||||
return self._entries
|
||||
|
||||
def handle_starttag(self, tag: str, attrs) -> None:
|
||||
if tag == "p":
|
||||
d = dict(attrs)
|
||||
if str(d["class"]).lower() == "work":
|
||||
self._capture_type = True
|
||||
if self._current_type is not None and tag == "td":
|
||||
d = dict(attrs)
|
||||
if ("class" in d) and ("dia_c_abfuhrdatum" in str(d["class"])):
|
||||
self._capture_date = True
|
||||
|
||||
def handle_data(self, data: str) -> None:
|
||||
if self._capture_type:
|
||||
self._current_type = data
|
||||
if self._capture_date:
|
||||
match = self._date_pattern.match(data)
|
||||
self._entries.append(
|
||||
Collection(
|
||||
date(int(match.group(3)), int(match.group(2)), int(match.group(1))),
|
||||
self._current_type,
|
||||
)
|
||||
)
|
||||
|
||||
def handle_endtag(self, tag: str) -> None:
|
||||
if tag == "p" and self._capture_type:
|
||||
self._capture_type = False
|
||||
if tag == "td" and self._capture_date:
|
||||
self._capture_date = False
|
||||
|
||||
|
||||
class Source:
|
||||
def __init__(
|
||||
self,
|
||||
city: str,
|
||||
street: str,
|
||||
house_number: int,
|
||||
address_suffix: str = "",
|
||||
garbage_types: list[int] = [1, 2, 3, 4, 5],
|
||||
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._garbage_types = garbage_types
|
||||
self._ics = ICS()
|
||||
|
||||
def fetch(self):
|
||||
r = requests.get(
|
||||
session = requests.session()
|
||||
|
||||
r = session.get(
|
||||
"https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet",
|
||||
params={"SubmitAction": "wasteDisposalServices", "InFrameMode": "TRUE"},
|
||||
)
|
||||
r.raise_for_status()
|
||||
r.encoding = "utf-8"
|
||||
|
||||
parser = HiddenInputParser()
|
||||
@@ -114,25 +67,36 @@ class Source:
|
||||
args["Strasse"] = self._street
|
||||
args["Hausnummer"] = str(self._hnr)
|
||||
args["Hausnummerzusatz"] = self._suffix
|
||||
args["Zeitraum"] = "Die Leerungen der nächsten 3 Monate"
|
||||
args["SubmitAction"] = "CITYCHANGED"
|
||||
r = session.post(
|
||||
"https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet",
|
||||
data=args,
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
args["SubmitAction"] = "forward"
|
||||
for type in range(1, 6):
|
||||
args[f"ContainerGewaehlt_{type}"] = (
|
||||
"on" if type in self._garbage_types else "off"
|
||||
)
|
||||
|
||||
# First request returns wrong city. has to be called twice!
|
||||
r = requests.post(
|
||||
args["ContainerGewaehltRM"] = "on"
|
||||
args["ContainerGewaehltBM"] = "on"
|
||||
args["ContainerGewaehltLVP"] = "on"
|
||||
args["ContainerGewaehltPA"] = "on"
|
||||
args["ContainerGewaehltPrMuell"] = "on"
|
||||
r = session.post(
|
||||
"https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet",
|
||||
data=args,
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
r = requests.post(
|
||||
args["ApplicationName"] = "com.athos.kd.rheinhunsrueck.AbfuhrTerminModel"
|
||||
args["SubmitAction"] = "filedownload_ICAL"
|
||||
r = session.post(
|
||||
"https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet",
|
||||
data=args,
|
||||
)
|
||||
r.encoding = "utf-8"
|
||||
r.raise_for_status()
|
||||
|
||||
date_parser = CollectionParser()
|
||||
date_parser.feed(r.text)
|
||||
return date_parser.entries
|
||||
dates = self._ics.convert(r.text)
|
||||
|
||||
entries = []
|
||||
for d in dates:
|
||||
entries.append(Collection(d[0], d[1]))
|
||||
return entries
|
||||
|
||||
@@ -13,12 +13,6 @@ waste_collection_schedule:
|
||||
street: STREET
|
||||
house_number: HNR
|
||||
address_suffix: HNR_SUFFIX
|
||||
garbage_types:
|
||||
- 1
|
||||
- 2
|
||||
- 3
|
||||
- 4
|
||||
- 5
|
||||
```
|
||||
|
||||
### Configuration Variables
|
||||
@@ -35,9 +29,6 @@ waste_collection_schedule:
|
||||
**address_suffix**<br>
|
||||
*(string) (optional) (default: "")*
|
||||
|
||||
**garbage_types**<br>
|
||||
*(list of integers) (optional) (default: [1,2,3,4,5])*
|
||||
|
||||
## Example
|
||||
|
||||
```yaml
|
||||
@@ -52,18 +43,4 @@ waste_collection_schedule:
|
||||
|
||||
## How to get the source arguments
|
||||
|
||||
### city, street and house_number
|
||||
|
||||
These values are the location you want to query for. Make sure, the writing is exactly as it is on [https://www.rh-entsorgung.de/de/Service/Abfallkalender/](https://www.rh-entsorgung.de/de/Service/Abfallkalender/). Typos will result in the collection schedule for the default location *(Alterkülz, Brühlweg)*, so make sure to validate the returned schedule after setting up the integration. As `house_number` expects a numeric input, address suffixes have to be provided via the `address_suffix` argument.
|
||||
|
||||
### garbage_types
|
||||
|
||||
Garbage types are mapped as follows:
|
||||
|
||||
```text
|
||||
1: Restmülltonne
|
||||
2: Biotonne
|
||||
3: Papiertonne
|
||||
4: Gelber Sack
|
||||
5: Problemmüll
|
||||
```
|
||||
These values are the location you want to query for. Make sure, the writing is exactly as it is on [https://www.rh-entsorgung.de/de/Service/Abfallkalender/](https://www.rh-entsorgung.de/de/Service/Abfallkalender/). Typos will result in the collection schedule for the default location *(Alterkülz, Brühlweg)*, so make sure to validate the returned schedule after setting up the integration. As `house_number` expects a numeric input, address suffixes have to be provided via the `address_suffix` argument.
|
||||
Reference in New Issue
Block a user