Skip to content

Commit

Permalink
Merge pull request #74 from mampfes/rh-entsorgung
Browse files Browse the repository at this point in the history
Rh entsorgung
  • Loading branch information
mampfes authored Sep 9, 2021
2 parents 757503e + 149dd34 commit ad4135b
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ Currently the following service providers are supported:
- [Abfall_Kreis_Tuebingen.de](./doc/source/abfall_kreis_tuebingen_de.md)
- [AbfallNavi.de (RegioIT.de)](./doc/source/abfallnavi_de.md)
- [Abfallwirtschaft Stuttgart](./doc/source/stuttgart_de.md)
- [Abfallwirtschaft Zollernalbkreis](./doc/source/abfall_zollernalbkreis_de.md)
- [AWBKoeln.de](./doc/source/awbkoeln_de.md)
- [AWIDO-online.de](./doc/source/awido_de.md)
- [BSR.de / Berliner Stadtreinigungsbetriebe](./doc/source/bsr_de.md)
- [Jumomind.de](./doc/source/jumomind_de.md)
- [Muellmax.de](./doc/source/muellmax_de.md)
- [Stadtreinigung.Hamburg](./doc/source/stadtreinigung_hamburg.md)
- [Abfallwirtschaft Zollernalbkreis](./doc/source/abfall_zollernalbkreis_de.md)
- [MyMuell App](./doc/source/jumomind_de.md)
- [Rhein-Hunsrück Entsorgung (RHE)](./doc/source/rh_entsorgung_de.md)
- [Sector27.de](./doc/source/sector27_de.md)
- [Stadtreinigung.Hamburg](./doc/source/stadtreinigung_hamburg.md)

### Netherlands

Expand Down
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
68 changes: 68 additions & 0 deletions doc/source/rh_entsorgung_de.md
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
```
5 changes: 3 additions & 2 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ Currently the following service providers are supported:
- [Abfall_Kreis_Tuebingen.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/abfall_kreis_tuebingen_de.md)
- [AbfallNavi.de (RegioIT.de)](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/abfallnavi_de.md)
- [Abfallwirtschaft Stuttgart](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stuttgart_de.md)
- [Abfallwirtschaft Zollernalbkreis](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/abfall_zollernalbkreis_de.md)
- [AWBKoeln.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/awbkoeln_de.md)
- [AWIDO-online.de](./doc/source/awido_de.md)
- [BSR.de / Berliner Stadtreinigungsbetriebe](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/bsr_de.md)
- [Jumomind.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/jumomind_de.md)
- [Muellmax.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/muellmax_de.md)
- [Stadtreinigung.Hamburg](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stadtreinigung_hamburg.md)
- [Abfallwirtschaft Zollernalbkreis](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/abfall_zollernalbkreis_de.md)
- [MyMuell App](./doc/source/jumomind_de.md)
- [Rhein-Hunsrück Entsorgung (RHE)](./doc/source/rh_entsorgung_de.md)
- [Sector27.de](./doc/source/sector27_de.md)
- [Stadtreinigung.Hamburg](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stadtreinigung_hamburg.md)

### Netherlands

Expand Down

0 comments on commit ad4135b

Please sign in to comment.