def callDvb(stopsByName): logging.info("Retrieve line info for each stop") # Problem: stop does not have to make sense for getting a result (tried "tst") # Problem: based on current time # better approach: https://github.com/kiliankoe/vvo/blob/master/documentation/webapi.md#lines (but need to get stopId using the pointFinder) changePoints = [] countUniqueStops = len(stopsByName) for index, stop in enumerate(list(stopsByName)): if index % 3 == 0: logging.info("progress: {}/{}".format(index + 1, countUniqueStops)) time.sleep(5) name = stop["properties"]["name"] # TODO: if name: try: dvbResponse = dvb.monitor(name) except requests.HTTPError: logging.error( "dvb said no for {}, timeout for 30s".format(name)) time.sleep(10) try: # poor mans retry dvbResponse = dvb.monitor(name) except requests.HTTPError: continue if dvbResponse: lines = list( set([ info.get("line", None) for info in dvbResponse if info ])) stop["properties"]["lines"] = lines # TODO: show this based on color only? (could save lines for every stop) # if more than 1 line departes from this stop, it is a change point canChange = len(lines) > 1 if canChange: changePoints.append(stop) changePoints = geojson.FeatureCollection(changePoints) with open("out/data/dvbChangePoints.json", 'w', encoding='UTF-8') as outfile: geojson.dump(changePoints, outfile)
def handle(self, handler_input): # type: (HandlerInput) -> Response session_attr = handler_input.attributes_manager.session_attributes stop = "" counter = 0 speech_text = "" repromt = "" time_offset = 0 # how many minutes in the future, 0 for now if "stop" in session_attr: stop = session_attr["stop"] counter = int( session_attr["counter"]) + 1 else: speech_text = "Du musst erst eine Haltestelle nennen, bevor ich dir weitere Linien an dieser sagen kann!" repromt = "Bitte sag mir den Namen der Haltestelle." handler_input.response_builder.speak(speech_text).ask(repromt) return handler_input.response_builder.response num_results = 2 * counter result = dvb.monitor(stop, time_offset, num_results) speech_text = "Die naechsten Verbindungen an der Haltestelle {stop} sind: ".format(stop=stop) for i in range(num_results-2,len(result)): line = result[i]["line"] direction = result[i]["direction"] arrival_min = "" if int(result[i]["arrival"]) == 1: arrival_min = "einer Minute" else: arrival_min = "{} Minuten".format(result[i]["arrival"]) if i == len(result) - 1: speech_text += "Linie {line} nach {direction} in {arrival}. ".format( line=line, direction=direction, arrival=arrival_min) elif i == len(result) - 2: speech_text += "Linie {line} nach {direction} in {arrival} und ".format( line=line, direction=direction, arrival=arrival_min) else: speech_text += "Linie {line} nach {direction} in {arrival}, ".format( line=line, direction=direction, arrival=arrival_min) speech_text += "Wenn du weitere wissen möchtest, sag einfach \"weitere\"." repromt += "Möchtest du noch mehr Linien wissen?" handler_input.response_builder.speak(speech_text).ask(repromt).set_card( SimpleCard("DVB Navigator", speech_text)) return handler_input.response_builder.response
def handle(self, handler_input): # type: (HandlerInput) -> Response session_attr = handler_input.attributes_manager.session_attributes slots = handler_input.request_envelope.request.intent.slots stop = slots["Station"].value session_attr["stop"] = stop session_attr["counter"] = 1 time_offset = 0 # how many minutes in the future, 0 for now num_results = 2 speech_text = "" repromt = "" result = dvb.monitor(stop, time_offset, num_results) if result == None: speech_text = "Ich habe die Haltestelle {} nicht gefunden. Bitte sag mir noch einmal den Namen der Haltestelle.".format(stop) repromt = "Bitte sag mir noch einmal den Namen der Haltestelle." handler_input.response_builder.speak(speech_text).ask(repromt) return handler_input.response_builder.response else: speech_text = "Die naechsten Verbindungen an der Haltestelle {stop} sind: ".format(stop=stop) for i in range(len(result)): line = result[i]["line"] direction = result[i]["direction"] arrival_min = "" if int(result[i]["arrival"]) == 1: arrival_min = "einer Minute" else: arrival_min = "{} Minuten".format(result[i]["arrival"]) if i == len(result) - 1: speech_text += "Linie {line} nach {direction} in {arrival}. ".format( line=line, direction=direction, arrival=arrival_min) elif i == len(result) - 2: speech_text += "Linie {line} nach {direction} in {arrival} und ".format( line=line, direction=direction, arrival=arrival_min) else: speech_text += "Linie {line} nach {direction} in {arrival}, ".format( line=line, direction=direction, arrival=arrival_min) speech_text += "Möchtest du noch weitere Linien Wissen? Sag einfach \"weitere\"." repromt += "Möchtest du noch mehr Linien wissen?" handler_input.response_builder.speak(speech_text).ask(repromt).set_card( SimpleCard("DVB Navigator", speech_text)) return handler_input.response_builder.response
def get_departures(station, city='Dresden', min_minutes=None, nextStopCount=4): """ Queries the DVB 'API' for the next departures from a single station. :param station: The name of the Station as String. :param city: City in which the station is located. Defaults to 'Dresden'. :param min_minutes: Minimal amount of time between now and the earliest departure (in minutes, as integer). `None` by default. :param nextStopCount: Number of next stops to be displayed (int). Defaults to 3 Stops. :return: A dict which contains the name of the station as key and a list of upcoming departures as value. """ minutes = min_minutes if min_minutes is not None else 0 stops = dvb.monitor(stop=station, offset=minutes, limit=nextStopCount, city=city) formatted_stops = [{'number': stop['line'], 'name': stop['direction'], 'minutes': stop['arrival']} for stop in stops] return {station.lower().replace('%20', ''): formatted_stops}
def handle(self, handler_input): # type: (HandlerInput) -> Response slots = handler_input.request_envelope.request.intent.slots stop = slots["Station"].value line = int(slots["TramBusNumber"].value) time_offset = 0 # how many minutes in the future, 0 for now num_results = 20 result = dvb.monitor(stop, time_offset, num_results) speech_text = "Die naechsten Linien {line} an der Haltestelle {stop} sind: ".format(stop=stop, line = str(line)) if result == None: speech_text = "Ich habe die Haltestelle {} nicht gefunden.".format(stop) repromt = "Bitte sag mir noch einmal die Linie und den Namen der Haltestelle!" handler_input.response_builder.speak(speech_text).ask(repromt) return handler_input.response_builder.response else: correct_List = [] for i in range(len(result)): if str(line) == result[i]["line"]: if len(correct_List) != 0: counter = 0 for l in correct_List: if l["line"] == result[i]["line"] and l["direction"] == result[i]["direction"]: counter += 1 if counter == 1: continue else: correct_List.append(result[i]) else: correct_List.append(result[i]) if len(correct_List) == 0: speech_text = "Die Linie {line} faehrt an der Haltestelle {stop} nicht!".format(line=str(line), stop=stop) repromt = "Bitte sag mir noch einmal die Linie und den Namen der Haltestelle!" for i in range(len(correct_List)): line = correct_List[i]["line"] direction = correct_List[i]["direction"] arrival_min = "" if int(correct_List[i]["arrival"]) == 1: arrival_min = "einer Minute" else: arrival_min = "{} Minuten".format(correct_List[i]["arrival"]) if i == len(correct_List) - 1: speech_text += "Linie {line} nach {direction} in {arrival}.".format( line=line, direction=direction, arrival=arrival_min) elif i == len(correct_List) - 2: speech_text += "Linie {line} nach {direction} in {arrival} und ".format( line=line, direction=direction, arrival=arrival_min) else: speech_text += "Linie {line} nach {direction} in {arrival}, ".format( line=line, direction=direction, arrival=arrival_min) handler_input.response_builder.speak(speech_text).set_card( SimpleCard("DVB Navigator", speech_text)).set_should_end_session( True) return handler_input.response_builder.response
args=parser.parse_args() if args.set: if args.set in stations: station=args.set else: sys.stderr.write("Selected set not in defined sets! Add your set to the sourcecode or choose from existing:"+bcolors.WARNING) for k,v in stations.items(): sys.stderr.write(" "+str(k)) sys.stderr.write("\n") exit(-1) args=parser.parse_args() if args.help: parser.print_help(sys.stderr) exit() signal.signal(signal.SIGINT,signal_handler) call(["tput","civis"]) while True: lines=shutil.get_terminal_size((85,25)).lines if lines%2==0: lines-=1 num=int(lines/len(stations[station])) sys.stdout.write("\x1B[H\x1B[J") sys.stdout.write(bcolors.OKGREEN+"{:<30s}{:<13s}{:<30s}{:>7s}".format("Haltestelle","Linie","Richtung","Ankunft")+bcolors.ENDC) table=[] for street in stations[station]: sys.stdout.write("\n") for elem in dvb.monitor(street,1,num-1,"Dresden"): sys.stdout.write("\n{:<30s}".format(street)+bcolors.YELLOW+"{:<13s}".format(elem["line"])+bcolors.ENDC+"{:<30s}".format(elem["direction"])+"{:>7d}".format(elem["arrival"])) sleep(10)