-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
133 lines (106 loc) · 3.12 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
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
from flask import (
Flask,
render_template,
request,
make_response,
send_from_directory,
jsonify,
)
import datetime
import requests
import operators
app = Flask(__name__)
@app.route("/", methods=["GET"])
def index():
resp = make_response(
render_template(
"main.html",
today=datetime.date.today(),
**request.cookies,
)
)
if (
request.cookies.get("expirydate") == None
or datetime.datetime.strptime(
request.cookies.get("expirydate"), "%Y-%m-%d"
).date()
< datetime.date.today()
):
resp.delete_cookie("expirydate")
resp.delete_cookie("ticketholder")
resp.delete_cookie("ticket")
return resp
@app.route("/static/<path:path>")
def send_static_file(path):
return send_from_directory("static", path)
@app.route("/api/submit", methods=["POST"])
def submit():
operator = request.json.get("operator")
if operator == "sj":
op = operators.SJ()
elif operator == "mt":
op = operators.MT()
r = op.submit(
request.json.get("ticket"),
request.json.get("from"),
request.json.get("to"),
request.json.get("departureDate"),
request.json.get("departureTime"),
request.json.get("customer"),
)
if r:
resp = make_response("Request submitted!")
return resp
else:
return f"Something went wrong submitting the request: {r.text}", 500
@app.route(
"/api/departures/<departure_station>/<arrival_station>/<date>", methods=["GET"]
)
def get_departures(departure_station, arrival_station, date):
r = requests.get(
"https://evf-regionsormland.preciocloudapp.net/api/TrainStations/GetDepartureTimeList",
params={
"departureStationId": departure_station,
"arrivalStationId": arrival_station,
"departureDate": date,
},
)
return jsonify(sorted(r.json()["data"]))
def get_train_number(departure_station, arrival_station, departure_time):
r = requests.get(
"https://evf-regionsormland.preciocloudapp.net/api/TrainStations/GetDistance",
params={
"departureStationId": departure_station,
"arrivalStationId": arrival_station,
"departureDate": departure_time,
},
)
return r.json()["data"]["trafikverketTrainId"]
@app.route("/api/arrival_stations/<station>", methods=["GET"])
def get_arrival_stations(station):
arrival_stations = {
"U": ["Cst", "Srv", "Kn", "Mr", "Fvk", "Gä"],
"Kn": ["U", "Cst", "Mr"],
"Mr": ["U", "Cst", "Kn"],
"Cst": ["U", "Kn", "Mr"],
"Srv": ["U", "Fvk", "Gä"],
}
station_names = {
"U": "Uppsala C",
"Cst": "Stockholm C",
"Srv": "Storvreta",
"Fvk": "Furuvik",
"Gä": "Gävle",
"Kn": "Knivsta",
"Mr": "Märsta",
}
return {
"stations": [
{"name": x, "longname": station_names[x]} for x in arrival_stations[station]
]
}
def main():
app.run(debug=True)
if __name__ == "__main__":
main()