Skip to content

Commit

Permalink
remove multiform data handling (not necessary)
Browse files Browse the repository at this point in the history
  • Loading branch information
mampfes committed Sep 9, 2021
1 parent e40745f commit 149dd34
Showing 1 changed file with 42 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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):
Expand All @@ -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<day>\d{2})\.(?P<month>\d{2})\.(?P<year>\d{4})')
r"(?P<day>\d{2})\.(?P<month>\d{2})\.(?P<year>\d{4})"
)

@property
def entries(self):
Expand All @@ -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:
Expand All @@ -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,
)
)

Expand All @@ -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()
Expand Down

0 comments on commit 149dd34

Please sign in to comment.