-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (31 loc) · 1.24 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
import asyncio
import math
from aiocache import Cache
from starlette.applications import Starlette
from starlette.routing import Route, Mount
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from letterboxd import get_letterboxd_most_recently_watched_details
from sites import SITES
from work import get_current_work_place
templates = Jinja2Templates(directory="templates")
cache = Cache(Cache.MEMORY)
async def cache_letterboxd_result():
letterboxd_result = await get_letterboxd_most_recently_watched_details()
await cache.set("letterboxd_result", letterboxd_result, math.inf)
return letterboxd_result
async def index(request):
letterboxd_result = await cache.get("letterboxd_result")
asyncio.create_task(cache_letterboxd_result())
work_result = get_current_work_place()
ctx = {
"letterboxd_result": letterboxd_result,
"sites": SITES,
"work_result": work_result,
"request": request,
}
return templates.TemplateResponse("index.html", ctx)
routes = [Route("/", index), Mount("/static", StaticFiles(directory="/app/static"))]
async def fill_cache():
await cache_letterboxd_result()
app = Starlette(routes=routes, on_startup=[fill_cache])