Repository navigation
feat(catalogs): add optional IMDb rating filter #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| from app.services.recommendation.all_based import AllBasedService | ||
| from app.services.recommendation.catalog_utils import clean_meta, shuffle_data_if_needed | ||
| from app.services.recommendation.creators import CreatorsService | ||
| from app.services.recommendation.imdb_ratings import filter_by_imdb_rating | ||
| from app.services.recommendation.item_based import ItemBasedService | ||
| from app.services.recommendation.rewatch import RewatchService | ||
| from app.services.recommendation.theme_based import ThemeBasedService | ||
|
|
@@ -222,6 +223,9 @@ async def _build_catalog( | |
|
|
||
| logger.debug(f"Returning {len(recommendations)} items for {content_type}") | ||
|
|
||
| recommendations = await filter_by_imdb_rating( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This runs after Filtering belongs in the shared pipeline before truncation, alongside genre exclusion and the watched filter. That means more rating lookups per build, so it needs a small shared rating cache. Ratings are not user-specific, so one Redis entry per title serves everyone. |
||
| recommendations, content_type, ctx.user_settings.imdb_rating_min | ||
| ) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few lines down, |
||
| cleaned = [m for m in (clean_meta(m) for m in recommendations) if m is not None] | ||
| cleaned = shuffle_data_if_needed(ctx.user_settings, catalog_id, cleaned) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import asyncio | ||
| import math | ||
| import re | ||
| from typing import Any | ||
|
|
||
| import httpx | ||
|
|
||
| from app.core.base_client import BaseClient | ||
| from app.core.constants import DEFAULT_CONCURRENCY_LIMIT | ||
|
|
||
|
|
||
| async def filter_by_imdb_rating( | ||
| metas: list[dict[str, Any]], content_type: str, minimum: float | None | ||
| ) -> list[dict[str, Any]]: | ||
| if minimum is None or not metas: | ||
| return metas | ||
|
|
||
| client = BaseClient(base_url="https://v3-cinemeta.strem.io") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That service currently uses raw |
||
| semaphore = asyncio.Semaphore(DEFAULT_CONCURRENCY_LIMIT) | ||
|
|
||
| async def rated_meta(meta: dict[str, Any]) -> dict[str, Any] | None: | ||
| imdb_id = meta.get("imdb_id") or meta.get("id") or "" | ||
| if not re.fullmatch(r"tt\d+", imdb_id): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return None | ||
| async with semaphore: | ||
| try: | ||
| response = await client.get(f"/meta/{content_type}/{imdb_id}.json") | ||
| except httpx.HTTPStatusError as exc: | ||
| if exc.response.status_code == 404: | ||
| return None | ||
|
Comment on lines
+28
to
+30
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the time this catches the 404, Please count the dropped titles and log one line per row instead. That also leaves a trace when a row comes back short, which is otherwise invisible. |
||
| raise | ||
| raw_rating = (response.get("meta") or {}).get("imdbRating") | ||
| try: | ||
| rating = float(raw_rating) | ||
| except (TypeError, ValueError): | ||
| return None | ||
| if not math.isfinite(rating) or not 0 <= rating <= 10 or rating < minimum: | ||
| return None | ||
| # The existing imdbRating field contains TMDB votes until replaced here. | ||
| return {**meta, "imdbRating": str(rating)} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes |
||
|
|
||
| try: | ||
| # Wait for all requests before closing their shared client, even on failure. | ||
| results = await asyncio.gather(*(rated_meta(meta) for meta in metas), return_exceptions=True) | ||
| for result in results: | ||
| if isinstance(result, BaseException): | ||
| raise result | ||
|
Comment on lines
+43
to
+47
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This exists only to keep the per-call client open until every request settles. With a shared client it becomes a plain |
||
| return [meta for meta in results if meta is not None] | ||
| finally: | ||
| await client.close() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import asyncio | ||
| import time | ||
| from types import SimpleNamespace | ||
|
|
||
| import httpx | ||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
||
| from app.api.models.tokens import TokenRequest | ||
| from app.core.settings import UserSettings | ||
| from app.services.auth import AuthService | ||
| from app.services.recommendation import catalog_service as cs | ||
| from app.services.recommendation import imdb_ratings | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [-0.1, 10.1, float("nan"), float("inf")]) | ||
| def test_invalid_minimum_is_rejected(value): | ||
| for model in (TokenRequest, UserSettings): | ||
| with pytest.raises(ValidationError): | ||
| model(catalogs=[], imdb_rating_min=value) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [None, 0, 7.5, 10]) | ||
| def test_minimum_survives_settings_round_trip(value): | ||
| payload = TokenRequest(imdb_rating_min=value) | ||
| settings = AuthService()._build_user_settings(payload) | ||
| assert UserSettings.model_validate(settings.model_dump()).imdb_rating_min == value | ||
|
|
||
|
|
||
| def test_disabled_filter_makes_no_requests(monkeypatch): | ||
| def unexpected_client(**kwargs): | ||
| pytest.fail("Disabled filter must not create a client") | ||
|
|
||
| monkeypatch.setattr(imdb_ratings, "BaseClient", unexpected_client) | ||
| metas = [{"id": "tt1", "imdbRating": "8.9"}] | ||
| assert asyncio.run(imdb_ratings.filter_by_imdb_rating(metas, "movie", None)) is metas | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("content_type", ["movie", "series"]) | ||
| def test_filter_uses_imdb_inclusive_boundary_and_preserves_order(monkeypatch, content_type): | ||
| ratings = {"tt1": "7.4", "tt2": "7.5", "tt3": "9.1", "tt4": None, "tt5": "N/A", "tt6": "nan", "tt7": "11"} | ||
| calls = [] | ||
| closed = [] | ||
|
|
||
| class Client: | ||
| def __init__(self, **kwargs): | ||
| pass | ||
|
|
||
| async def get(self, path): | ||
| calls.append(path) | ||
| return {"meta": {"imdbRating": ratings[path.split("/")[-1].split(".")[0]]}} | ||
|
|
||
| async def close(self): | ||
| closed.append(True) | ||
|
|
||
| monkeypatch.setattr(imdb_ratings, "BaseClient", Client) | ||
| metas = [{"id": key, "imdbRating": "10"} for key in ratings] + [{"id": "tmdb:99"}] | ||
| result = asyncio.run(imdb_ratings.filter_by_imdb_rating(metas, content_type, 7.5)) | ||
| assert [m["id"] for m in result] == ["tt2", "tt3"] | ||
| assert result[0]["imdbRating"] == "7.5" | ||
| assert metas[1]["imdbRating"] == "10" | ||
| assert all(path.startswith(f"/meta/{content_type}/") for path in calls) | ||
| assert closed == [True] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("status", [404, 503]) | ||
| def test_missing_metadata_excluded_but_outages_raise_and_close(monkeypatch, status): | ||
| closed = [] | ||
|
|
||
| class Client: | ||
| def __init__(self, **kwargs): | ||
| pass | ||
|
|
||
| async def get(self, path): | ||
| response = httpx.Response(status, request=httpx.Request("GET", "https://example.com")) | ||
| response.raise_for_status() | ||
|
|
||
| async def close(self): | ||
| closed.append(True) | ||
|
|
||
| monkeypatch.setattr(imdb_ratings, "BaseClient", Client) | ||
|
|
||
| def run(): | ||
| return asyncio.run(imdb_ratings.filter_by_imdb_rating([{"id": "tt1"}], "movie", 7)) | ||
|
|
||
| if status == 404: | ||
| assert run() == [] | ||
| else: | ||
| with pytest.raises(httpx.HTTPStatusError): | ||
| run() | ||
| assert closed == [True] | ||
|
|
||
|
|
||
| def test_cached_catalog_is_served_without_rating_requests(monkeypatch): | ||
| cached_body = {"metas": [{"id": "tt1"}, {"id": "tt2"}]} | ||
|
|
||
| async def alias(token): | ||
| return token | ||
|
|
||
| async def credentials(token): | ||
| return {"settings": UserSettings(catalogs=[], imdb_rating_min=7.5).model_dump()} | ||
|
|
||
| async def cached(*args): | ||
| return cached_body, int(time.time()) | ||
|
|
||
| async def filter_metas(*args): | ||
| pytest.fail("Cached rows are already filtered; a hit must not call Cinemeta") | ||
|
|
||
| monkeypatch.setattr(cs.settings, "AUTO_UPDATE_CATALOGS", False) | ||
| monkeypatch.setattr(cs.token_store, "resolve_alias", alias) | ||
| monkeypatch.setattr(cs.token_store, "get_user_data", credentials) | ||
| monkeypatch.setattr(cs.user_cache, "get_catalog", cached) | ||
| monkeypatch.setattr(cs, "filter_by_imdb_rating", filter_metas) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both catalog tests replace |
||
| data, _ = asyncio.run(cs.CatalogService().get_catalog("token", "series", "watchly.rewatch")) | ||
| assert data["metas"] == [{"id": "tt1"}, {"id": "tt2"}] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("outage", [False, True]) | ||
| def test_cold_catalog_filters_before_cache_write(monkeypatch, outage): | ||
| service = cs.CatalogService() | ||
| settings = UserSettings(catalogs=[], imdb_rating_min=7.5) | ||
| ctx = SimpleNamespace(token="token", user_settings=settings, library=None) | ||
| writes = [] | ||
|
|
||
| async def resolve(*args): | ||
| return "watchly.rec" | ||
|
|
||
| async def profile(*args): | ||
| return SimpleNamespace(source="stremio"), set(), set() | ||
|
|
||
| async def recommendations(**kwargs): | ||
| return [{"id": "tt1", "type": "movie", "name": "Low"}, {"id": "tt2", "type": "movie", "name": "High"}] | ||
|
|
||
| async def filter_metas(metas, content_type, minimum): | ||
| assert minimum == 7.5 | ||
| if outage: | ||
| raise httpx.ConnectError("Cinemeta unavailable") | ||
| return metas[1:] | ||
|
|
||
| async def write(*args): | ||
| writes.append(args[3]) | ||
|
|
||
| monkeypatch.setattr(service, "_resolve_slot", resolve) | ||
| monkeypatch.setattr(service, "_initialize_services", lambda _: {"profile": None}) | ||
| monkeypatch.setattr(service, "_get_recommendations", recommendations) | ||
| monkeypatch.setattr(cs.user_cache, "get_profile_and_watched_sets", profile) | ||
| monkeypatch.setattr(cs.user_cache, "set_catalog", write) | ||
| monkeypatch.setattr(cs, "filter_by_imdb_rating", filter_metas) | ||
| data, _ = asyncio.run(service._build_catalog(ctx, "movie", "watchly.rec", {})) | ||
| assert [m["id"] for m in data["metas"]] == ([] if outage else ["tt2"]) | ||
| assert writes == ([] if outage else [data]) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section landed under Contributors. It fits better with the other configuration docs. Please also add a
CHANGELOG.mdentry.