From 149dd348c67672e0609e40a3e9d8cc8c8c9af9dd Mon Sep 17 00:00:00 2001 From: mampfes Date: Sat, 14 Aug 2021 17:08:59 +0200 Subject: [PATCH] remove multiform data handling (not necessary) --- .../source/rh_entsorgung_de.py | 97 ++++++++----------- 1 file changed, 42 insertions(+), 55 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/rh_entsorgung_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/rh_entsorgung_de.py index 43010c4fa..aaa993519 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/rh_entsorgung_de.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/rh_entsorgung_de.py @@ -1,7 +1,8 @@ -from datetime import date import re -import requests +from datetime import date from html.parser import HTMLParser + +import requests from waste_collection_schedule import Collection # type: ignore[attr-defined] TITLE = "RH Entsorgung" @@ -11,23 +12,16 @@ "Horn": { "city": "Rheinböllen", "street": "Erbacher Straße", - "house_number": 13, - "address_suffix": "A" + "house_number": 13, + "address_suffix": "A", }, "Bärenbach": { "city": "Bärenbach", "street": "Schwarzener Straße", - "house_number": 10 - } + "house_number": 10, + }, } -# Generates request data payload to be sent as 'form-data' -def to_multipart_form_data(args: dict[str, str]) -> dict[str, (None, str)]: - form_data_dict: dict[str, (None, str)] = {} - for key, value in args.items(): - form_data_dict[key] = (None, value) - return form_data_dict - # Parser for HTML input (hidden) text class HiddenInputParser(HTMLParser): @@ -54,7 +48,8 @@ def __init__(self) -> None: self._capture_type: bool = False self._capture_date: bool = False self._date_pattern = re.compile( - r'(?P\d{2})\.(?P\d{2})\.(?P\d{4})') + r"(?P\d{2})\.(?P\d{2})\.(?P\d{4})" + ) @property def entries(self): @@ -63,11 +58,11 @@ def entries(self): def handle_starttag(self, tag: str, attrs) -> None: if tag == "p": d = dict(attrs) - if(str(d["class"]).lower() == "work"): + if str(d["class"]).lower() == "work": self._capture_type = True - if self._current_type != None and tag == "td": + 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"])): + if ("class" in d) and ("dia_c_abfuhrdatum" in str(d["class"])): self._capture_date = True def handle_data(self, data: str) -> None: @@ -78,7 +73,7 @@ def handle_data(self, data: str) -> None: self._entries.append( Collection( date(int(match.group(3)), int(match.group(2)), int(match.group(1))), - self._current_type + self._current_type, ) ) @@ -90,60 +85,52 @@ def handle_endtag(self, tag: str) -> None: class Source: - def __init__(self, - city: str, - street: str, - house_number: int, - address_suffix: str = "", - garbage_types: list[int] = [1, 2, 3, 4, 5]): + 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 append_custom_args(self, args, submit_action: str = None): + 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" - - if submit_action != None: - args["SubmitAction"] = submit_action - + args["SubmitAction"] = "forward" for type in range(1, 6): - args["ContainerGewaehlt_{}".format( - type)] = "on" if type in self._garbage_types else "off" - - return args - - def fetch(self): - parser = HiddenInputParser() - r = requests.post( - "https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet?SubmitAction=wasteDisposalServices&InFrameMode=TRUE") - r.encoding = "utf-8" - - parser.feed(r.text) - args = to_multipart_form_data( - self.append_custom_args(parser.args, 'forward')) + 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?SubmitAction=wasteDisposalServices&InFrameMode=TRUE", - files=args, - cookies=r.cookies, - headers={ - 'Cache-Control': 'no-cache' - }) + "https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet", + data=args, + ) r = requests.post( - "https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet?SubmitAction=wasteDisposalServices&InFrameMode=TRUE", - files=args, - cookies=r.cookies, - headers={ - 'Cache-Control': 'no-cache' - }) + "https://aao.rh-entsorgung.de/WasteManagementRheinhunsrueck/WasteManagementServlet", + data=args, + ) r.encoding = "utf-8" date_parser = CollectionParser()