-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimeTableApi.py
More file actions
75 lines (61 loc) · 3.09 KB
/
Copy pathTimeTableApi.py
File metadata and controls
75 lines (61 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
A JSON HTTP API for realtime timetables. Requires a static GTFS feed, VehiclePositions.pb and TripUpdates.pb.
"""
import argparse
import json
import logging
import sys
from datetime import datetime, timedelta
import flask
# Initialize the logger before importing our other module. This way we see the output for the other module as well.
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
# Initialize the logger before importing our other module. This way we see the output for the other module as well.
from GtfsTimeTable import TimeTableQueryEngine, GtfsArchiveFetcher
from RealtimeDataFetcher import RealtimeDataFetcher
app = flask.Flask(__name__)
@app.route('/departures/<stop_id>', methods=['GET'])
def departures(stop_id):
gtfs_path = GtfsArchiveFetcher.fetch_and_extract(args.gtfs_url, "gtfs/")
window_start = datetime.now() - timedelta(minutes=10)
window_end = datetime.now() + timedelta(hours=2)
resp = flask.Response(json.dumps(query_engine.create_departures_timetable(stop_id, window_start, window_end)))
resp.headers['Content-encoding'] = 'UTF-8'
resp.headers['Content-type'] = 'Application/json'
return resp
@app.route('/stops/', methods=['GET'])
def stops():
gtfs_path = GtfsArchiveFetcher.fetch_and_extract(args.gtfs_url, "gtfs/")
resp = flask.Response(json.dumps(query_engine.list_queryable_stops()))
resp.headers['Content-encoding'] = 'UTF-8'
resp.headers['Content-type'] = 'Application/json'
return resp
parser = argparse.ArgumentParser(
description="Start a JSON HTTP API based on GTFS and GTFS-RT data"
)
parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
optional = parser.add_argument_group('optional arguments')
required.add_argument("--gtfs", dest="gtfs_url",
help="the url to the gtfs zip file. Include an API key if the gtfs feed requires this.",
required=True)
required.add_argument("--trip-updates",
help="the url to the tripupdates.pb file. Include an API key if the realtime feed requires this.",
dest="trip_updates", required=True)
required.add_argument("--vehicle-positions",
help="the url to the vehiclepositions.pb file. Include an API key if the realtime feed requires this.",
dest="vehicle_positions", required=True)
optional.add_argument("--uncached", help="this option will reduce memory significantly, but queries will be slow",
action='store_true')
args = parser.parse_args()
realtime_data_fetcher = RealtimeDataFetcher(args.trip_updates, args.vehicle_positions)
# The Archive fetcher will only fetch a new file when needed
gtfs_path = GtfsArchiveFetcher.fetch_and_extract(args.gtfs_url, "gtfs/")
query_engine = TimeTableQueryEngine(gtfs_path, realtime_data_fetcher, reduce_memory_usage=args.uncached)
app.config["DEBUG"] = False
app.run()