diff --git a/unwind/db.py b/unwind/db.py index e32d4c0..4a4fc68 100644 --- a/unwind/db.py +++ b/unwind/db.py @@ -119,7 +119,6 @@ async def apply_db_patches(db: Database): raise RuntimeError("No statement found.") async with db.transaction(): - for query in queries: await db.execute(query) diff --git a/unwind/imdb.py b/unwind/imdb.py index 9288e6f..5b56b3f 100644 --- a/unwind/imdb.py +++ b/unwind/imdb.py @@ -35,12 +35,10 @@ log = logging.getLogger(__name__) async def refresh_user_ratings_from_imdb(stop_on_dupe=True): - with session() as s: s.headers["Accept-Language"] = "en-US, en;q=0.5" for user in await db.get_all(User): - log.info("⚡️ Loading data for %s ...", user.name) try: @@ -97,7 +95,6 @@ find_movie_id = re.compile(r"/title/(?Ptt\d+)/").search def movie_and_rating_from_item(item) -> tuple[Movie, Rating]: - genres = (genre := item.find("span", "genre")) and genre.string or "" movie = Movie( title=item.h3.a.string.strip(), @@ -169,7 +166,6 @@ async def parse_page(url) -> tuple[list[Rating], str | None]: items = soup.find_all("div", "lister-item-content") for i, item in enumerate(items): - try: movie, rating = movie_and_rating_from_item(item) except Exception as err: @@ -199,7 +195,6 @@ async def load_ratings(user_id): next_url = user_ratings_url(user_id) while next_url: - ratings, next_url = await parse_page(next_url) for i, rating in enumerate(ratings): diff --git a/unwind/imdb_import.py b/unwind/imdb_import.py index 3557993..3991a78 100644 --- a/unwind/imdb_import.py +++ b/unwind/imdb_import.py @@ -111,11 +111,10 @@ def gz_mtime(path: Path) -> datetime: def count_lines(path: Path) -> int: i = 0 - one_mb = 2 ** 20 + one_mb = 2**20 buf_size = 8 * one_mb # 8 MiB seems to give a good read/process performance. with gzip.open(path, "rt") as f: - while buf := f.read(buf_size): i += buf.count("\n") @@ -138,7 +137,6 @@ def read_imdb_tsv( def read_imdb_tsv(path: Path, row_type, *, unpack=True): with gzip.open(path, "rt", newline="") as f: - rows = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE) # skip header line @@ -205,7 +203,6 @@ async def import_from_file(*, basics_path: Path, ratings_path: Path): chunk = [] for i, m in enumerate(read_basics(basics_path)): - perc = 100 * i / total if perc >= perc_next_report: await db.set_import_progress(perc) diff --git a/unwind/models.py b/unwind/models.py index 70ffe26..8922e5b 100644 --- a/unwind/models.py +++ b/unwind/models.py @@ -41,7 +41,6 @@ def fields(class_or_instance): # XXX this might be a little slow (not sure), if so, memoize for f in _fields(class_or_instance): - if f.name == "_is_lazy": continue @@ -108,7 +107,6 @@ def asplain( d: JSONObject = {} for f in fields(o): - if filter_fields is not None and f.name not in filter_fields: continue @@ -156,7 +154,6 @@ def fromplain(cls: Type[T], d: dict[str, Any], *, serialized: bool = False) -> T dd: JSONObject = {} for f in fields(cls): - target = f.type otype = optional_type(f.type) is_opt = otype is not None diff --git a/unwind/request.py b/unwind/request.py index 3b78872..c4866ea 100644 --- a/unwind/request.py +++ b/unwind/request.py @@ -77,7 +77,6 @@ def Session() -> requests.Session: def throttle( times: int, per_seconds: float, jitter: Callable[[], float] | None = None ) -> Callable[[Callable], Callable]: - calls: deque[float] = deque(maxlen=times) if jitter is None: @@ -86,7 +85,6 @@ def throttle( def decorator(func: Callable) -> Callable: @wraps(func) def inner(*args, **kwds): - # clean up while calls: if calls[0] + per_seconds > time(): @@ -151,7 +149,6 @@ def cache_path(req) -> Path | None: @throttle(1, 1, random) def http_get(s: requests.Session, url: str, *args, **kwds) -> requests.Response: - req = s.prepare_request(requests.Request("GET", url, *args, **kwds)) cachefile = cache_path(req) if config.debug else None @@ -244,7 +241,6 @@ def download( raise FileExistsError(23, "Would replace existing file", str(file_path)) with session() as s: - headers = {} if file_exists and only_if_newer: assert file_path @@ -300,7 +296,7 @@ def download( tempfd, tempfile_path = tempfile.mkstemp( dir=tempdir, prefix=f".download-{file_path.name}." ) - one_mb = 2 ** 20 + one_mb = 2**20 chunk_size = 8 * one_mb try: log.debug("💾 Writing to temp file %s ...", tempfile_path) diff --git a/unwind/utils.py b/unwind/utils.py index efe9f17..f253bde 100644 --- a/unwind/utils.py +++ b/unwind/utils.py @@ -33,7 +33,7 @@ def phc_scrypt( if salt is None: salt = secrets.token_bytes(16) - n = params.get("n", 2 ** 14) # CPU/Memory cost factor + n = params.get("n", 2**14) # CPU/Memory cost factor r = params.get("r", 8) # block size p = params.get("p", 1) # parallelization factor # maxmem = 2 * 128 * n * r * p diff --git a/unwind/web.py b/unwind/web.py index dbe39bc..cab9a9e 100644 --- a/unwind/web.py +++ b/unwind/web.py @@ -195,7 +195,6 @@ route.registered = _routes @route("/groups/{group_id}/ratings") async def get_ratings_for_group(request): - group_id = as_ulid(request.path_params["group_id"]) group = await db.get(Group, id=str(group_id)) @@ -256,7 +255,6 @@ def not_implemented(): @route("/movies") @requires(["authenticated"]) async def list_movies(request): - params = request.query_params user = await auth_user(request) @@ -324,7 +322,6 @@ async def list_movies(request): @route("/movies", methods=["POST"]) @requires(["authenticated", "admin"]) async def add_movie(request): - not_implemented() @@ -366,7 +363,6 @@ _import_lock = asyncio.Lock() @route("/movies/_reload_imdb", methods=["POST"]) @requires(["authenticated", "admin"]) async def load_imdb_movies(request): - params = request.query_params force = truthy(params.get("force")) @@ -389,7 +385,6 @@ async def load_imdb_movies(request): @route("/users") @requires(["authenticated", "admin"]) async def list_users(request): - users = await db.get_all(User) return JSONResponse([asplain(u) for u in users]) @@ -398,7 +393,6 @@ async def list_users(request): @route("/users", methods=["POST"]) @requires(["authenticated", "admin"]) async def add_user(request): - name, imdb_id = await json_from_body(request, ["name", "imdb_id"]) # XXX restrict name @@ -420,7 +414,6 @@ async def add_user(request): @route("/users/{user_id}") @requires(["authenticated"]) async def show_user(request): - user_id = as_ulid(request.path_params["user_id"]) if is_admin(request): @@ -449,7 +442,6 @@ async def show_user(request): @route("/users/{user_id}", methods=["DELETE"]) @requires(["authenticated", "admin"]) async def remove_user(request): - user_id = as_ulid(request.path_params["user_id"]) user = await db.get(User, id=str(user_id)) @@ -467,7 +459,6 @@ async def remove_user(request): @route("/users/{user_id}", methods=["PATCH"]) @requires(["authenticated"]) async def modify_user(request): - user_id = as_ulid(request.path_params["user_id"]) if is_admin(request): @@ -515,7 +506,6 @@ async def modify_user(request): @route("/users/{user_id}/groups", methods=["POST"]) @requires(["authenticated", "admin"]) async def add_group_to_user(request): - user_id = as_ulid(request.path_params["user_id"]) user = await db.get(User, id=str(user_id)) @@ -540,21 +530,18 @@ async def add_group_to_user(request): @route("/users/{user_id}/ratings") @requires(["private"]) async def ratings_for_user(request): - not_implemented() @route("/users/{user_id}/ratings", methods=["PUT"]) @requires("authenticated") async def set_rating_for_user(request): - not_implemented() @route("/users/_reload_ratings", methods=["POST"]) @requires(["authenticated", "admin"]) async def load_imdb_user_ratings(request): - ratings = [rating async for rating in imdb.refresh_user_ratings_from_imdb()] return JSONResponse({"new_ratings": [asplain(r) for r in ratings]}) @@ -563,7 +550,6 @@ async def load_imdb_user_ratings(request): @route("/groups") @requires(["authenticated", "admin"]) async def list_groups(request): - groups = await db.get_all(Group) return JSONResponse([asplain(g) for g in groups]) @@ -572,7 +558,6 @@ async def list_groups(request): @route("/groups", methods=["POST"]) @requires(["authenticated", "admin"]) async def add_group(request): - (name,) = await json_from_body(request, ["name"]) # XXX restrict name @@ -586,7 +571,6 @@ async def add_group(request): @route("/groups/{group_id}/users", methods=["POST"]) @requires(["authenticated"]) async def add_user_to_group(request): - group_id = as_ulid(request.path_params["group_id"]) group = await db.get(Group, id=str(group_id))