forked from BadgerHobbs/Parkrun-API-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_api.py
410 lines (274 loc) · 14 KB
/
flask_api.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
from flask import Flask, redirect, request, render_template
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import time
import threading
import json
import parkrun_api as parkrun
cache = {}
def RefreshCache():
global cache
cache = {}
countries = parkrun.Country.GetAllCountries()
events = parkrun.Event.GetAllEvents()
parkrun.Event.UpdateEventUrls(events, countries)
cache["countries"] = countries
cache["events"] = events
def CacheClearanceThread():
global cache
while True:
print("Refreshing Cache")
RefreshCache()
time.sleep(32400)
threading.Thread(target=CacheClearanceThread).start()
def GetEventById(eventId):
global cache
for event in cache["events"]:
if str(event.id) == (eventId):
return event
return None
def GetCountryById(countryId):
global cache
for country in cache["countries"]:
if str(country.id) == (countryId):
return country
return None
def ObjectListToDictList(objectList):
global cache
objectDictList = []
for object in objectList:
objectDictList.append(object.__dict__)
return objectDictList
def CacheToDict():
global cache
cacheDict = {}
for cacheItem in cache:
cacheDict[cacheItem] = ObjectListToDictList(cache[cacheItem])
return cacheDict
app = Flask(__name__)
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["15000 per day", "600 per hour"]
)
@app.route("/")
def GithubRedirect():
global cache
return redirect("https://github.com/BadgerHobbs/Parkrun-API-Python", code=302)
@app.route("/docs")
def SimpleAPIViewerDocs():
return render_template('index.html')
@app.route("/v1/cache")
def GetCache():
global cache
return json.dumps(CacheToDict())
@app.route("/v1/clear-cache")
def ClearCache():
global cache
if request.args.get("key") == "12345":
cache = {}
RefreshCache()
return json.dumps(CacheToDict())
else:
return json.dumps({"error": "Invalid key"})
@app.route("/v1/countries")
def GetCountries():
global cache
if "countries" not in cache:
cache["countries"] = parkrun.Country.GetAllCountries()
return json.dumps(ObjectListToDictList(cache["countries"]))
# Event Specific Data
@app.route("/v1/events")
def GetEvents():
global cache
if "events" not in cache:
cache["events"] = parkrun.Event.GetAllEvents()
return json.dumps(ObjectListToDictList(cache["events"]))
@app.route("/v1/events/<event_id>/history")
def GetEventHistory(event_id):
global cache
if f"events/{event_id}/history" not in cache:
cache[f"events/{event_id}/history"] = parkrun.EventHistory.GetEventHistorys(GetEventById(event_id))
return json.dumps(ObjectListToDictList(cache[f"events/{event_id}/history"]))
@app.route("/v1/events/<event_id>/first-finishers")
def GetFirstFinishers(event_id):
global cache
if f"events/{event_id}/first-finishers" not in cache:
cache[f"events/{event_id}/first-finishers"] = parkrun.FirstFinisher.GetFirstFinishers(GetEventById(event_id))
return json.dumps(ObjectListToDictList(cache[f"events/{event_id}/first-finishers"]))
@app.route("/v1/events/<event_id>/age-category-records")
def GetAgeCategoryRecords(event_id):
global cache
if f"events/{event_id}/age-category-records" not in cache:
cache[f"events/{event_id}/age-category-records"] = parkrun.AgeCategoryRecord.GetAgeCategoryRecords(GetEventById(event_id))
return json.dumps(ObjectListToDictList(cache[f"events/{event_id}/age-category-records"]))
@app.route("/v1/events/<event_id>/clubs")
def GetClubs(event_id):
global cache
if f"events/{event_id}/clubs" not in cache:
cache[f"events/{event_id}/clubs"] = parkrun.Club.GetClubs(GetEventById(event_id))
return json.dumps(ObjectListToDictList(cache[f"events/{event_id}/clubs"]))
@app.route("/v1/events/<event_id>/sub-20-women")
def GetSub20Women(event_id):
global cache
if f"events/{event_id}/sub-20-women" not in cache:
cache[f"events/{event_id}/sub-20-women"] = parkrun.Sub20Woman.GetSub20Women(GetEventById(event_id))
return json.dumps(ObjectListToDictList(cache[f"events/{event_id}/sub-20-women"]))
@app.route("/v1/events/<event_id>/sub-17-men")
def GetSub17Men(event_id):
global cache
if f"events/{event_id}/sub-17-men" not in cache:
cache[f"events/{event_id}/sub-17-men"] = parkrun.Sub17Man.GetSub17Men(GetEventById(event_id))
return json.dumps(ObjectListToDictList(cache[f"events/{event_id}/sub-17-men"]))
@app.route("/v1/events/<event_id>/age-graded-league-ranks")
def GetAgeGradedLeagueRanks(event_id):
global cache
quantity = 1000
if request.args.get('quantity'):
quantity = int(request.args.get('quantity'))
if f"events/{event_id}/age-graded-league-ranks?{quantity}" not in cache:
cache[f"events/{event_id}/age-graded-league-ranks?{quantity}"] = parkrun.AgeGradedLeagueRank.GetAgeGradedLeagueRanks(GetEventById(event_id), quantity=quantity)
return json.dumps(ObjectListToDictList(cache[f"events/{event_id}/age-graded-league-ranks?{quantity}"]))
@app.route("/v1/events/<event_id>/fastest-500")
def GetFastest500(event_id):
global cache
if f"events/{event_id}/fastest-500" not in cache:
cache[f"events/{event_id}/fastest-500"] = parkrun.Fastest.GetFastest500(GetEventById(event_id))
return json.dumps(ObjectListToDictList(cache[f"events/{event_id}/fastest-500"]))
# Country Specific Data
@app.route("/v1/countries/<country_id>/week-first-finishers")
def GetWeekFirstFinishersForCountry(country_id):
global cache
if f"countries/{country_id}/week-first-finishers" not in cache:
cache[f"countries/{country_id}/week-first-finishers"] = parkrun.WeekFirstFinisher.GetWeekFirstFinishersForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/week-first-finishers"]))
@app.route("/v1/countries/<country_id>/week-sub-17-runs")
def GetWeekSub17RunsForCountry(country_id):
global cache
if f"countries/{country_id}/week-sub-17-runs" not in cache:
cache[f"countries/{country_id}/week-sub-17-runs"] = parkrun.WeekSub17Run.GetWeekSub17RunsForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/week-sub-17-runs"]))
@app.route("/v1/countries/<country_id>/week-top-age-grades")
def GetWeekTopAgeGradesForCountry(country_id):
global cache
if f"countries/{country_id}/week-top-age-grades" not in cache:
cache[f"countries/{country_id}/week-top-age-grades"] = parkrun.WeekTopAgeGrade.GetWeekTopAgeGradesForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/week-top-age-grades"]))
@app.route("/v1/countries/<country_id>/week-new-category-records")
def GetWeekNewCategoryRecordsForCountry(country_id):
global cache
if f"countries/{country_id}/week-new-category-records" not in cache:
cache[f"countries/{country_id}/week-new-category-records"] = parkrun.WeekNewCategoryRecord.GetWeekNewCategoryRecordsForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/week-new-category-records"]))
@app.route("/v1/countries/<country_id>/course-records")
def GetCourseRecordsForCountry(country_id):
global cache
if f"countries/{country_id}/course-records" not in cache:
cache[f"countries/{country_id}/course-records"] = parkrun.CourseRecord.GetCourseRecordsForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/course-records"]))
@app.route("/v1/countries/<country_id>/attendance-records")
def GetAttendanceRecordsForCountry(country_id):
global cache
if f"countries/{country_id}/attendance-records" not in cache:
cache[f"countries/{country_id}/attendance-records"] = parkrun.AttendanceRecord.GetAttendanceRecordsForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/attendance-records"]))
@app.route("/v1/countries/<country_id>/most-events")
def GetMostEventsForCountry(country_id):
global cache
if f"countries/{country_id}/most-events" not in cache:
cache[f"countries/{country_id}/most-events"] = parkrun.MostEvent.GetMostEventsForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/most-events"]))
@app.route("/v1/countries/<country_id>/largest-clubs")
def GetLargestClubsForCountry(country_id):
global cache
if f"countries/{country_id}/largest-clubs" not in cache:
cache[f"countries/{country_id}/largest-clubs"] = parkrun.LargestClub.GetLargestClubsForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/largest-clubs"]))
@app.route("/v1/countries/<country_id>/joined-100-club")
def GetJoined100ClubsForCountry(country_id):
global cache
if f"countries/{country_id}/joined-100-club" not in cache:
cache[f"countries/{country_id}/joined-100-club"] = parkrun.Joined100Club.GetJoined100ClubsForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/joined-100-club"]))
@app.route("/v1/countries/<country_id>/most-first-finishes")
def GetMostFirstFinishesForCountry(country_id):
global cache
if f"countries/{country_id}/most-first-finishes" not in cache:
cache[f"countries/{country_id}/most-first-finishes"] = parkrun.MostFirstFinish.GetMostFirstFinishesForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/most-first-finishes"]))
@app.route("/v1/countries/<country_id>/freedom-runs")
def GetFreedomRunsForCountry(country_id):
global cache
if f"countries/{country_id}/freedom-runs" not in cache:
cache[f"countries/{country_id}/freedom-runs"] = parkrun.FreedomRun.GetFreedomRunsForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/freedom-runs"]))
@app.route("/v1/countries/<country_id>/historic-numbers")
def GetHistoricNumbersForCountry(country_id):
global cache
if f"countries/{country_id}/historic-numbers" not in cache:
cache[f"countries/{country_id}/historic-numbers"] = parkrun.HistoricNumber.GetHistoricNumbersForCountry(GetCountryById(country_id))
return json.dumps(ObjectListToDictList(cache[f"countries/{country_id}/historic-numbers"]))
# Global Results Data
@app.route("/v1/global/results/week-first-finishers")
def GetWeekFirstFinishersGlobally():
global cache
if f"global/results/week-first-finishers" not in cache:
cache[f"global/results/week-first-finishers"] = parkrun.WeekFirstFinisher.GetWeekFirstFinishersGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/results/week-first-finishers"]))
@app.route("/v1/global/results/new-category-records")
def GetWeekNewCategoryRecordsGlobally():
global cache
if f"global/results/new-category-records" not in cache:
cache[f"global/results/new-category-records"] = parkrun.WeekNewCategoryRecord.GetWeekNewCategoryRecordsGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/results/new-category-records"]))
@app.route("/v1/global/results/sub-17-runs")
def GetWeekSub17RunsGlobally():
global cache
if f"global/results/sub-17-runs" not in cache:
cache[f"global/results/sub-17-runs"] = parkrun.WeekSub17Run.GetWeekSub17RunsGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/results/sub-17-runs"]))
@app.route("/v1/global/results/top-age-grades")
def GetWeekTopAgeGradesGlobally():
global cache
if f"global/results/top-age-grades" not in cache:
cache[f"global/results/top-age-grades"] = parkrun.WeekTopAgeGrade.GetWeekTopAgeGradesGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/results/top-age-grades"]))
@app.route("/v1/global/results/course-records")
def GetCourseRecordsGlobally():
global cache
if f"global/results/course-records" not in cache:
cache[f"global/results/course-records"] = parkrun.CourseRecord.GetCourseRecordsGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/results/course-records"]))
@app.route("/v1/global/results/freedom-runs")
def GetFreedomRunsGlobally():
global cache
if f"global/results/freedom-runs" not in cache:
cache[f"global/results/freedom-runs"] = parkrun.FreedomRun.GetFreedomRunsGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/results/freedom-runs"]))
# Global Stats Data
@app.route("/v1/global/stats/largest-clubs")
def GetLargestClubsGlobally():
global cache
if f"global/stats/largest-clubs" not in cache:
cache[f"global/stats/largest-clubs"] = parkrun.Club.GetLargestClubsGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/stats/largest-clubs"]))
@app.route("/v1/global/stats/attendance-records")
def GetAttendanceRecordsGlobally():
global cache
if f"global/stats/attendance-records" not in cache:
cache[f"global/stats/attendance-records"] = parkrun.AttendanceRecord.GetAttendanceRecordsGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/stats/attendance-records"]))
@app.route("/v1/global/stats/most-events")
def GetMostEventsGlobally():
global cache
if f"global/stats/most-events" not in cache:
cache[f"global/stats/most-events"] = parkrun.MostEvent.GetMostEventsGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/stats/most-events"]))
@app.route("/v1/global/stats/most-first-finishes")
def GetMostFirstFinishesGlobally():
global cache
if f"global/stats/most-first-finishes" not in cache:
cache[f"global/stats/most-first-finishes"] = parkrun.MostFirstFinish.GetMostFirstFinishesGlobally()
return json.dumps(ObjectListToDictList(cache[f"global/stats/most-first-finishes"]))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)