-
-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from mampfes/rh-entsorgung
Rh entsorgung
- Loading branch information
Showing
4 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...components/waste_collection_schedule/waste_collection_schedule/source/rh_entsorgung_de.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import re | ||
from datetime import date | ||
from html.parser import HTMLParser | ||
|
||
import requests | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
|
||
TITLE = "RH Entsorgung" | ||
DESCRIPTION = "Source for RHE (Rhein Hunsrück Entsorgung)." | ||
URL = "https://www.rh-entsorgung.de" | ||
TEST_CASES = { | ||
"Horn": { | ||
"city": "Rheinböllen", | ||
"street": "Erbacher Straße", | ||
"house_number": 13, | ||
"address_suffix": "A", | ||
}, | ||
"Bärenbach": { | ||
"city": "Bärenbach", | ||
"street": "Schwarzener Straße", | ||
"house_number": 10, | ||
}, | ||
} | ||
|
||
|
||
# Parser for HTML input (hidden) text | ||
class HiddenInputParser(HTMLParser): | ||
def __init__(self): | ||
super().__init__() | ||
self._args = {} | ||
|
||
@property | ||
def args(self): | ||
return self._args | ||
|
||
def handle_starttag(self, tag, attrs): | ||
if tag == "input": | ||
d = dict(attrs) | ||
if str(d["type"]).lower() == "hidden": | ||
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 = city | ||
self._street = street | ||
self._hnr = house_number | ||
self._suffix = address_suffix | ||
self._garbage_types = garbage_types | ||
|
||
def fetch(self): | ||
r = requests.get( | ||
"https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet", | ||
params={"SubmitAction": "wasteDisposalServices", "InFrameMode": "TRUE"}, | ||
) | ||
r.encoding = "utf-8" | ||
|
||
parser = HiddenInputParser() | ||
parser.feed(r.text) | ||
|
||
args = parser.args | ||
args["Ort"] = self._city | ||
args["Strasse"] = self._street | ||
args["Hausnummer"] = str(self._hnr) | ||
args["Hausnummerzusatz"] = self._suffix | ||
args["Zeitraum"] = "Die Leerungen der nächsten 3 Monate" | ||
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( | ||
"https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet", | ||
data=args, | ||
) | ||
|
||
r = requests.post( | ||
"https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet", | ||
data=args, | ||
) | ||
r.encoding = "utf-8" | ||
|
||
date_parser = CollectionParser() | ||
date_parser.feed(r.text) | ||
return date_parser.entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Rhein-Hunsrück Entsorgung (RHE) | ||
|
||
Support for schedules provided by [RHE](https://www.rh-entsorgung.de) (Rhein-Hunsrück Entsorgung) located in Rhineland-Palatinate, Germany. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: rh_entsorgung_de | ||
args: | ||
city: CITY | ||
street: STREET | ||
house_number: HNR | ||
address_suffix: HNR_SUFFIX | ||
garbage_types: | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
- 5 | ||
``` | ||
### Configuration Variables | ||
**city**<br> | ||
*(string) (required)* | ||
**street**<br> | ||
*(string) (required)* | ||
**house_number**<br> | ||
*(integer) (required)* | ||
**address_suffix**<br> | ||
*(string) (optional) (default: "")* | ||
**garbage_types**<br> | ||
*(list of integers) (optional) (default: [1,2,3,4,5])* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: rhe_de | ||
args: | ||
city: "Alterkülz" | ||
street: "Brühlweg" | ||
house_number: 1 | ||
``` | ||
## 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: | ||
``` | ||
1: Restmülltonne | ||
2: Biotonne | ||
3: Papiertonne | ||
4: Gelber Sack | ||
5: Problemmüll | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters