diff --git a/.gitignore b/.gitignore index 30a091a..0eacced 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.pyc /.cache +/.pytest_cache /data/* /requirements.txt diff --git a/unwind/db.py b/unwind/db.py index ae660c4..f609ca2 100644 --- a/unwind/db.py +++ b/unwind/db.py @@ -2,7 +2,7 @@ import logging import re from dataclasses import fields from pathlib import Path -from typing import Optional, Type, TypeVar +from typing import Literal, Optional, Type, TypeVar import sqlalchemy from databases import Database @@ -155,7 +155,7 @@ async def find_ratings( media_type: str = None, ignore_tv_episodes: bool = False, include_unrated: bool = False, - year: int = None, + yearcomp: tuple[Literal["<", "=", ">"], int] = None, limit_rows=10, ): values = { @@ -176,9 +176,11 @@ async def find_ratings( """ ) - if year: + if yearcomp: + op, year = yearcomp + assert op in "<=>" values["year"] = year - conditions.append(f"{Movie._table}.release_year=:year") + conditions.append(f"{Movie._table}.release_year{op}:year") if media_type: values["media_type"] = media_type diff --git a/unwind/web.py b/unwind/web.py index 8b18f0d..23d8128 100644 --- a/unwind/web.py +++ b/unwind/web.py @@ -1,6 +1,7 @@ import base64 import binascii import logging +from typing import Literal from starlette.applications import Starlette from starlette.authentication import ( @@ -50,6 +51,18 @@ def truthy(s: str): return bool(s) and s.lower() in {"1", "yes", "true"} +def yearcomp(s: str): + if not s: + return + + comp: Literal["<", "=", ">"] = "=" + if (prefix := s[0]) in "<=>": + comp = prefix # type: ignore + s = s[len(prefix) :] + + return comp, int(s) + + async def ratings(request): params = request.query_params rows = await find_ratings( @@ -57,7 +70,7 @@ async def ratings(request): media_type=params.get("media_type"), ignore_tv_episodes=truthy(params.get("ignore_tv_episodes")), include_unrated=truthy(params.get("include_unrated")), - year=int(params["year"]) if "year" in params else None, + yearcomp=yearcomp(params["year"]) if "year" in params else None, ) aggr = {}