From 97547ca6adacd83eef2e2a98fa30d6a920fd13a5 Mon Sep 17 00:00:00 2001 From: mampfes Date: Sat, 2 May 2020 11:23:40 +0200 Subject: [PATCH] improve wizard for stadtreinigung.hamburg fix handling if full street name is given and service provide immediately returns the list of house numbers --- .../package/wizard/stadtreinigung_hamburg.py | 64 +++++++++++++------ 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/custom_components/waste_collection_schedule/package/wizard/stadtreinigung_hamburg.py b/custom_components/waste_collection_schedule/package/wizard/stadtreinigung_hamburg.py index 9cd97d16..d07e8f2e 100755 --- a/custom_components/waste_collection_schedule/package/wizard/stadtreinigung_hamburg.py +++ b/custom_components/waste_collection_schedule/package/wizard/stadtreinigung_hamburg.py @@ -5,6 +5,26 @@ import requests import json from html.parser import HTMLParser +# Parser for HTML input +class InputParser(HTMLParser): + def __init__(self, input_name): + super().__init__() + self._input_name = input_name + self._value = None + + @property + def value(self): + return self._value + + def handle_starttag(self, tag, attrs): + if tag == "input": + for attr in attrs: + if attr[0] == "name" and attr[1] == self._input_name: + for attr2 in attrs: + if attr2[0] == "value": + self._value = attr2[1] + break + break # Parser for HTML option list class OptionParser(HTMLParser): @@ -61,42 +81,46 @@ def main(): ] answers = inquirer.prompt(questions) - args = answers - args["hausnummer"] = "" - args["bestaetigung"] = "true" - args["mode"] = "search" + answers["hausnummer"] = "" + answers["bestaetigung"] = "true" + answers["mode"] = "search" r = requests.post( "https://www.stadtreinigung.hamburg/privatkunden/abfuhrkalender/index.html", - data=args, + data=answers, ) - # parser HTML option list - parser = OptionParser(select_name="asId") - parser.feed(r.text) + # search for street + input_parser = InputParser(input_name="asId") + input_parser.feed(r.text) - questions = [inquirer.List("asId", choices=parser.choices, message="Select street")] - answers = inquirer.prompt(questions) + if input_parser.value is not None: + answers["asId"] = input_parser.value + else: + # query returned a list of streets + parser = OptionParser(select_name="asId") + parser.feed(r.text) + + questions = [inquirer.List("asId", choices=parser.choices, message="Select street")] + answers.update(inquirer.prompt(questions)) # search for building number - args = answers.copy() - args["hausnummer"] = "" - args["bestaetigung"] = "true" - args["mode"] = "search" - r = requests.post( "https://www.stadtreinigung.hamburg/privatkunden/abfuhrkalender/index.html", - data=args, + data=answers, ) # parser HTML option list parser = OptionParser(select_name="hnId") parser.feed(r.text) - questions = [ - inquirer.List("hnId", choices=parser.choices, message="Select house number") - ] - answers.update(inquirer.prompt(questions)) + if len(parser.choices) == 0: + answers["hnId"] = "" + else: + questions = [ + inquirer.List("hnId", choices=parser.choices, message="Select house number") + ] + answers.update(inquirer.prompt(questions)) print("Copy the following statements into your configuration.yaml:\n") print("# waste_collection_schedule source configuration")