import requests import sys from typing import List import configparser HTTP = requests.session() def yn(s: str) -> bool: return s in ['y', 'Y', 'J', 'j', ''] def search_station(search: str) -> List or None: params = { 'query': search.replace(" ", "+") } resp = HTTP.get("https://abfahrtsmonitor.vrr.de/backend/api/stations/search", params=params) try: resp.raise_for_status() except requests.exceptions.HTTPError: return None return resp.json()['suggestions'] def get_station() -> int: station_id = None while station_id is None: search = input("Which station would you like to monitor? ") print("Getting suggestions...") results = search_station(search) if results: # empty lists and None are False for ptr in range(len(results)): print(str(ptr) + ". " + results[ptr]['value'] + "\t" + results[ptr]['data']) choice_ptr = None if len(results) > 1: while choice_ptr is None: parse = input("Which one? ") try: parse = int(parse) test = results[parse] except ValueError: print("You did not input a number!") except IndexError: print("Number out of range.") choice_ptr = parse else: choice_ptr = 0 station_id = results[choice_ptr]['data'] else: print('Got no results for "' + search + '".') return int(station_id) def get_lines(station_id: int) -> List[str]: print("Getting lines...") resp = HTTP.get("https://abfahrtsmonitor.vrr.de/backend/api/lines/" + str(station_id) + "/search") resp.raise_for_status() results = resp.json() choices = "" if len(results) > 20: page = yn(input("There are more than 20 results. Would you like to view them page-by-page? (Y/n)")) else: page = False if page: print("Paging in batches of 20 results.") results_displayed = 0 for ptr in range(len(results)): if ptr % 20 == 0 or ptr == len(results) - 1 and ptr != 0: choices += input("Please input your choices as a space-separated list (e.g. '0 2 7 15'):\n") + ' ' print(str(ptr) + ". " + results[ptr]['name']) else: for ptr in range(len(results)): print(str(ptr) + ". " + results[ptr]['name']) choices += input("Please input your choices as a space-separated list (e.g. '0 2 7 15'):\n") + ' ' filt_arr = [] for ptr in choices.split(" "): if ptr == '': continue try: filt_arr.append(results[int(ptr)]) except ValueError: print(ptr + " is not a number") except IndexError: print(ptr + " is out of range") return ["{0}:{1}:{2}:{3}".format(r['network'], r['line'], r['supplement'], r['directionCode']) for r in filt_arr] def setup() -> None: station_id = get_station() lines_ch = input("Would you like to choose specific lines? (Y/n)", ) if yn(lines_ch): lines = get_lines(station_id) else: lines = None cfg = configparser.ConfigParser() cfg.add_section('crawl') cfg.add_section('db') cfg['crawl']['station_id'] = str(station_id) cfg['crawl']['use_long_distance'] = 'yes' cfg['crawl']['use_regional_trains'] = 'yes' cfg['crawl']['use_commuter_trains'] = 'yes' cfg['crawl']['use_underground_trains'] = 'yes' cfg['crawl']['use_trams'] = 'yes' cfg['crawl']['use_buses'] = 'yes' cfg['crawl']['use_elevated_trains'] = 'yes' cfg['crawl']['use_lines'] = ",".join(lines) if lines is not None else "" cfg['db']['user'] = 'vrr' cfg['db']['pass'] = 'vrr' cfg['db']['host'] = 'localhost' cfg['db']['database'] = 'vrr' print("Please save the following output to 'vrr.ini' and adjust any further settings:") print("\n" * 3) cfg.write(sys.stdout)