init with some kind of working prototype
This commit is contained in:
commit
b5cb22822e
22 changed files with 1292 additions and 0 deletions
53
unwind/web.py
Normal file
53
unwind/web.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
from collections import defaultdict
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.routing import Route
|
||||
|
||||
from . import config
|
||||
from .db import close_connection_pool, find_ratings, open_connection_pool
|
||||
|
||||
|
||||
def imdb_url(imdb_id: str):
|
||||
return f"https://www.imdb.com/title/{imdb_id}/"
|
||||
|
||||
|
||||
def truthy(s: str):
|
||||
return bool(s) and s.lower() in {"1", "yes", "true"}
|
||||
|
||||
|
||||
async def ratings(request):
|
||||
title = request.query_params.get("title")
|
||||
media_type = request.query_params.get("media_type")
|
||||
ignore_tv_episodes = truthy(request.query_params.get("ignore_tv_episodes"))
|
||||
rows = await find_ratings(
|
||||
title=title, media_type=media_type, ignore_tv_episodes=ignore_tv_episodes
|
||||
)
|
||||
|
||||
aggr = {}
|
||||
for r in rows:
|
||||
mov = aggr.setdefault(
|
||||
r["movie_imdb_id"],
|
||||
{
|
||||
"title": r["movie_title"],
|
||||
"year": r["release_year"],
|
||||
"link": imdb_url(r["movie_imdb_id"]),
|
||||
"user_scores": [],
|
||||
"imdb_score": r["imdb_score"],
|
||||
"media_type": r["media_type"],
|
||||
},
|
||||
)
|
||||
mov["user_scores"].append(r["user_score"])
|
||||
|
||||
resp = tuple(aggr.values())
|
||||
|
||||
return JSONResponse(resp)
|
||||
|
||||
|
||||
app = Starlette(
|
||||
on_startup=[open_connection_pool],
|
||||
on_shutdown=[close_connection_pool],
|
||||
routes=[
|
||||
Route("/ratings", ratings),
|
||||
],
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue