-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (43 loc) · 1.47 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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.encoders import jsonable_encoder
import asyncio
from datetime import datetime
from async_utils import periodic
from cbr_data import get_locations, get_arrivals, get_data, get_live_data, Data, Status, Location, Stop
app = FastAPI()
app.data = get_data()
app.last_request = datetime.now()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
CUSTOM_ENCODERS = {
Stop: lambda obj: obj.name,
Status: lambda obj: obj.name
}
async def update_data(app):
if (datetime.now() - app.last_request).total_seconds() <= 30:
app.data.feed_data = get_live_data()
elif app.data.feed_data is not None and len(app.data.feed_data) > 0:
app.data.feed_data = None
@app.on_event("startup")
def init_data():
task = asyncio.create_task(periodic(15.0, update_data, app))
@app.get("/live")
async def live():
app.last_request = datetime.now()
if app.data.feed_data is None:
app.data.feed_data = get_live_data()
ret = get_locations(app.data)
return jsonable_encoder(ret, custom_encoder=CUSTOM_ENCODERS)
@app.get("/arrivals/{seq}")
async def arrival(seq:int):
app.last_request = datetime.now()
if app.data.feed_data is None:
app.data.feed_data = get_live_data()
ret = get_arrivals(app.data, seq)
return jsonable_encoder(ret, custom_encoder=CUSTOM_ENCODERS)