# Copyright (C) 2022 Yuchen Pei. # # This file is part of ptvtt. # # ptvtt is free software: you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # ptvtt is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General # Public License for more details. # You should have received a copy of the GNU Affero General Public # License along with ptvtt. If not, see . from wsgiref.simple_server import make_server from urllib.parse import parse_qs import ptv_client import html_view import util def is_dep_query(params): return all(key in params for key in ('route-type', 'stop', 'route', 'stop-name')) def application(environ, start_response): params = parse_qs(environ['QUERY_STRING']) if 'q' in params: query = str(params.get('q', [''])[0]) response_body = html_view.format_stop_and_route_name_table( list(ptv_client.get_stop_and_routes(ptv_client.search(query)))) title = f'Search results for {query}' response_html = html_view.html(title, response_body) elif is_dep_query(params): departures = ptv_client.get_departures(str(params['route-type'][0]), str(params['stop'][0]), str(params['route'][0])) filtered_deps = util.filter_departures(departures['departures']) direction_names = ptv_client.get_direction_names( str(params['route'][0])) response_body = html_view.format_departure_table( filtered_deps, direction_names) title = html_view.format_departure_title( str(params['stop-name'][0]), int(params['route-type'][0]), str(params.get('route-number', [''])[0])) response_html = html_view.html(title, response_body) else: response_html = html_view.landing_page() 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()