-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
156 lines (135 loc) · 6.15 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from flask import Flask, Response, make_response, render_template, request
import bart
import stations
import alerts
import schedule
import bartLogs
import json
import time
import news
app = Flask(__name__)
PORT = json.loads(open("config.json").read())["port"]
def jsonResp(input, status):
# Helper function to create JSON responses for API methods
return Response(json.dumps(input), content_type="application/json"), status
def userAgent():
try:
return request.headers.get('User-Agent')
except:
return ""
def convert_bytes(bytes):
# Define the denominations and their respective sizes in bytes
denominations = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
sizes = [1, 1024, 1024**2, 1024**3, 1024**4, 1024**5]
# Find the appropriate denomination
for i in range(len(sizes)):
if bytes < sizes[i] * 1024 or i == len(sizes) - 1:
return f"{bytes / sizes[i]:.2f} {denominations[i]}"
@app.route('/')
def home():
return render_template("index.html")
@app.route("/api/v1/getPredictions/<station>")
def getPred(station):
requestID = bartLogs.createRequestID()
try:
bart.getEnglishStationNameFromAbbreviation(station)
rsp = bart.getDataStation(station)
except:
rsp = json.dumps({"error": True, "message": "Invalid station name, use /api/v1/getStations to get the list of BART stations"})
print(rsp)
m = make_response(Response(rsp, content_type="application/json"))
m.headers["x-request-id"] = requestID
bartLogs.writeToLogsFile(request.headers.get('X-Forwarded-For'), userAgent(), f"/api/v1/getPredictions/{station}", round(time.time()), requestID, json.loads(rsp)["error"])
return m
@app.route("/api/v1/getStations")
def getStations():
requestID = bartLogs.createRequestID()
try:
rsp = stations.getStations()
except:
rsp = json.dumps({"error": True, "message": "Internal error/BART website error"})
m = make_response(Response(rsp, content_type="application/json"))
m.headers["x-request-id"] = requestID
bartLogs.writeToLogsFile(request.headers.get('X-Forwarded-For'), userAgent(), "/api/v1/getStations", round(time.time()), requestID, json.loads(rsp)["error"])
return m
@app.route("/api/v1/getAlerts")
def getAlerts():
requestID = bartLogs.createRequestID()
try:
rsp = alerts.getAlerts()
except:
rsp = json.dumps({"error": True, "message": "Internal error/BART website error"})
m = make_response(Response(rsp, content_type="application/json"))
m.headers["x-request-id"] = requestID
bartLogs.writeToLogsFile(request.headers.get('X-Forwarded-For'), userAgent(), "/api/v1/getAlerts", round(time.time()), requestID, json.loads(rsp)["error"])
return m
@app.route("/api/v1/getSchedule/<abv>/<month>/<day>/<year>/<tme>/<amPM>")
def getSchedule(abv, month, day, year, tme, amPM):
requestID = bartLogs.createRequestID()
if bart.getEnglishStationNameFromAbbreviation(abv.upper()) != None:
try:
rsp = schedule.getSchedule(abv, month, day, year, tme, amPM)
except:
rsp = json.dumps({"error": True, "message": "Internal error/BART website error"})
else:
rsp = json.dumps({"error": True, "message": "Invalid station"})
m = make_response(Response(rsp, content_type="application/json"))
m.headers["x-request-id"] = requestID
bartLogs.writeToLogsFile(request.headers.get('X-Forwarded-For'), userAgent(), f"/api/v1/getSchedule/{abv}/{month}/{day}/{year}/{tme}/{amPM}", round(time.time()), requestID, json.loads(rsp)["error"])
return m
@app.route("/api/v1/getStation/<station>")
def getStation(station):
requestID = bartLogs.createRequestID()
station = station.upper()
try:
rsp = stations.getStationsByAbvLive(station)
except:
rsp = json.dumps({"error": True, "message": "Internal error/BART website error"})
m = make_response(Response(rsp, content_type="application/json"))
m.headers["x-request-id"] = requestID
bartLogs.writeToLogsFile(request.headers.get('X-Forwarded-For'), userAgent(), f"/api/v1/getStation/{station}", round(time.time()), requestID, json.loads(rsp)["error"])
return m
@app.route("/api/v1/getNews")
def getNT():
requestID = bartLogs.createRequestID()
try:
rsp = json.dumps(news.getLatestTitles())
except:
rsp = json.dumps({"error": True, "message": "Internal error/BART website error"})
m = make_response(Response(rsp, content_type="application/json"))
m.headers["x-request-id"] = requestID
bartLogs.writeToLogsFile(request.headers.get('X-Forwarded-For'), userAgent(), "/api/v1/getNewsTitles", round(time.time()), requestID, json.loads(rsp)["error"])
return m
@app.route("/api/v1/getNewsByYear/<year>")
def getNewsYear(year):
requestID = bartLogs.createRequestID()
try:
rsp = news.getTitlesByYear(year)
except:
rsp = json.dumps({"error": True, "message": "Internal error/BART website error"})
m = make_response(Response(rsp, content_type="application/json"))
m.headers["x-request-id"] = requestID
bartLogs.writeToLogsFile(request.headers.get('X-Forwarded-For'), userAgent(), f"/api/v1/getTitlesByYear/{year}", round(time.time()), requestID, json.loads(rsp)["error"])
return m
@app.route("/api/v1/getNewsContent/<year>/<id>")
def getNewsContent(year, id):
requestID = bartLogs.createRequestID()
try:
rsp = news.getArticleContent(f"https://www.bart.gov/news/{year}/{id}")
except:
rsp = json.dumps({"error": True, "message": "Internal error/BART website error"})
m = make_response(Response(rsp, content_type="application/json"))
m.headers["x-request-id"] = requestID
bartLogs.writeToLogsFile(request.headers.get('X-Forwarded-For'), userAgent(), f"/api/v1/getNewsContent/{year}/{id}", round(time.time()), requestID, json.loads(rsp)["error"])
return m
@app.route("/api/v1/policeReports")
def policeReports():
return "soon"
@app.route("/robots.txt")
def robots():
return Response("User-agent: *\nDisallow:", content_type="text/plain")
@app.errorhandler(404)
def page_not_found(error):
return jsonResp({"error": True, "message": "Route not found"}, 404)
if __name__ == '__main__':
app.run(port=PORT)