#!/usr/bin/env python import ptv_client import html_view from wsgiref.simple_server import make_server from urllib.parse import parse_qs import util def application(environ, start_response): d = parse_qs(environ['QUERY_STRING']) if 'q' in d: query = str(d.get('q', [''])[0]) response_body = html_view.format_stop_and_route_name_table( list(ptv_client.get_stop_and_routes(ptv_client.search(query)))) else: print(d) departures = ptv_client.get_departures( str(d['route-type'][0]), str(d['stop'][0]), str(d['route'][0])) filtered_deps = util.filter_departures(departures['departures']) direction_names = ptv_client.get_direction_names(str(d['route'][0])) response_body = html_view.format_departure_table( filtered_deps, direction_names) response_html = html_view.html(response_body) response_headers = [ ('Content-Type', 'text/html'), ('Content-Length', str(len(response_html))) ] start_response('200 OK', response_headers) return [response_html.encode()] httpd = make_server('localhost', 8052, application) httpd.serve_forever()