-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
121 lines (86 loc) · 3.58 KB
/
app.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from src import TrainPageGetter, StationsGetter, StationTimetableGetter, config
from flask import Flask, jsonify, send_from_directory
from flask_cors import CORS
from datetime import datetime, timedelta
from dateutil import tz, parser
app = Flask(__name__)
CORS(app)
# Generate the station lookup table
config.global_station_list = {}
stations = StationsGetter.get_stations()
for station in stations:
config.global_station_list[station["name"]] = station["station_id"]
@app.route('/static/<path:path>')
def send_res(path):
return send_from_directory('static', path)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/train/<string:train_id>')
def get_train(train_id):
train_list = TrainPageGetter.get_train(train_id)
return jsonify(train_list)
@app.route('/get-stations/')
def get_stations():
return jsonify(stations)
@app.route('/station/<int:station_id>')
def get_timetable(station_id):
timetable = StationTimetableGetter.get_timetable(station_id)
return jsonify(timetable)
def timetable_departures_filter(timetable):
departures_timetable = []
for item in timetable:
if item['is_origin'] or item['is_stop']:
departures_timetable.append(item)
return departures_timetable
def timetable_arrivals_filter(timetable):
departures_timetable = []
for item in timetable:
if item['is_destination'] or item['is_stop']:
departures_timetable.append(item)
return departures_timetable
def timestamp_current_filter(timetable):
current_timetable = []
for item in timetable:
timezone = tz.gettz('Europe/Bucharest')
beginning = datetime.now(tz=timezone) - timedelta(hours=1)
end = datetime.now(tz=timezone) + timedelta(hours=3)
if item['arrival_timestamp']:
arrival = parser.isoparse(item['arrival_timestamp'])
if beginning <= arrival <= end:
current_timetable.append(item)
continue
if beginning <= arrival + timedelta(minutes=item['delay']) <= end:
current_timetable.append(item)
continue
if item['departure_timestamp']:
departure = parser.isoparse(item['departure_timestamp'])
if beginning <= departure <= end:
current_timetable.append(item)
continue
if beginning <= departure + timedelta(minutes=item['delay']) <= end:
current_timetable.append(item)
continue
return current_timetable
@app.route('/station/<int:station_id>/departures')
def get_departures_timetable(station_id):
timetable = StationTimetableGetter.get_timetable(station_id)
timetable = timetable_departures_filter(timetable)
return jsonify(timetable)
@app.route('/station/<int:station_id>/departures/current')
def get_current_departures_timetable(station_id):
timetable = StationTimetableGetter.get_timetable(station_id)
timetable = timestamp_current_filter(timetable)
timetable = timetable_departures_filter(timetable)
return jsonify(timetable)
@app.route('/station/<int:station_id>/arrivals')
def get_arrivals_timetable(station_id):
timetable = StationTimetableGetter.get_timetable(station_id)
timetable = timetable_arrivals_filter(timetable)
return jsonify(timetable)
@app.route('/station/<int:station_id>/arrivals/current')
def get_current_arrivals_timetable(station_id):
timetable = StationTimetableGetter.get_timetable(station_id)
timetable = timestamp_current_filter(timetable)
timetable = timetable_arrivals_filter(timetable)
return jsonify(timetable)