Repository navigation
feat(catalogs): add optional IMDb rating filter - #209
yusuftunga1 wants to merge 2 commits into
Conversation
|
@yusuftunga1 is attempting to deploy a commit to the Bimal Timilsina's projects Team on Vercel. A member of the Team first needs to authorize it. |
| data = { | ||
| **data, | ||
| "metas": await filter_by_imdb_rating(data["metas"], content_type, user_settings.imdb_rating_min), | ||
| } |
There was a problem hiding this comment.
This puts network calls on the cache-hit path. A cached row used to be a Redis read plus a shuffle; with the filter on, each cached row request now makes up to 20 Cinemeta requests, and Stremio requests every row at once.
It is also outside any try, so a Cinemeta outage turns a good cached row into a 500 after three retries per title, and a combined movies-and-series row fails both halves.
The cache is already filtered at build time, so this only matters right after the threshold changes — and it only handles raising it. Lowering it leaves the cached row over-filtered until the next refresh.
Please drop this block and call invalidate_all_catalogs in AuthService when imdb_rating_min changes, next to the existing watch_history_source check.
|
|
||
| logger.debug(f"Returning {len(recommendations)} items for {content_type}") | ||
|
|
||
| recommendations = await filter_by_imdb_rating( |
There was a problem hiding this comment.
This runs after _get_recommendations has already truncated to the row limit, so rows shrink instead of backfilling.
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 = await filter_by_imdb_rating( | ||
| recommendations, content_type, ctx.user_settings.imdb_rating_min | ||
| ) |
There was a problem hiding this comment.
A few lines down, if cleaned: skips the cache write for an empty row. With a strict threshold that filters a row down to nothing, every request for that row becomes a full cold build plus the Cinemeta calls.
| if minimum is None or not metas: | ||
| return metas | ||
|
|
||
| client = BaseClient(base_url="https://v3-cinemeta.strem.io") |
There was a problem hiding this comment.
app/services/cinemeta_service.py already has a process-wide cinemeta_service with this base URL. A BaseClient per call means a new connection pool and TLS handshake per row.
That service currently uses raw httpx and swallows errors, so it needs a method that raises (ideally by moving it onto BaseClient) — happy for that to be part of this PR.
|
|
||
| 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): |
There was a problem hiding this comment.
clean_meta already drops ids that don't start with tt, so this can't fail for anything that would be served. Same for the finite and 0–10 checks on line 37: Cinemeta doesn't return those values.
| 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)} |
There was a problem hiding this comment.
This makes imdbRating the real IMDb value only when the filter is on, so the same title shows a different rating to different users. Worth either doing it for everyone or leaving the displayed value alone.
| # 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 |
There was a problem hiding this comment.
This exists only to keep the per-call client open until every request settles. With a shared client it becomes a plain asyncio.gather.
| rows: catalog.rows | ||
| })), | ||
| language: languageSelect?.value || 'english', | ||
| imdb_rating_min: document.getElementById('imdbRatingMin')?.value ? Number(document.getElementById('imdbRatingMin').value) : null, |
There was a problem hiding this comment.
The element is looked up twice; a local variable would read better.
Also, entering 0 enables the filter, which drops every unrated title and makes all the Cinemeta calls without excluding anything by rating. Is that intended?
| <img src="https://contrib.rocks/image?repo=TimilsinaBimal/watchly" /> | ||
| </a> | ||
|
|
||
| ### Minimum IMDb rating |
There was a problem hiding this comment.
This section landed under Contributors. It fits better with the other configuration docs. Please also add a CHANGELOG.md entry.
| 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) |
There was a problem hiding this comment.
Both catalog tests replace filter_by_imdb_rating, so nothing exercises the real request path. A test for the cache-hit path during a Cinemeta failure would have caught the 500.
…b minimum changes Rows are cached after the filter has run, so filtering again on a hit only added a Cinemeta round trip per title on every request, and a Cinemeta outage turned a cached row into a 500. Serve cached rows as they are and clear the catalog cache when imdb_rating_min changes instead, otherwise lowering or disabling the minimum would never bring filtered titles back. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DWMMTUqUQHSF38vW6H8798
Closes #193.
Adds an optional Minimum IMDb rating setting to Preferences and the token API (
imdb_rating_min). A value of 7.5 keeps titles rated 7.5 or higher across movie and series catalogs, including trending fallbacks and rewatch picks. The setting is restored on reconfiguration and shown on the dashboard; existing accounts default to disabled.Ratings come from Stremio Cinemeta instead of treating TMDB vote averages as IMDb ratings. When enabled, the filter excludes missing/invalid ratings, checks cached catalogs too, and prevents partially filtered results from being cached if lookups fail. Filtering runs after recommendation selection, so strict thresholds can shorten rows. Enabled filtering adds Cinemeta requests on cache hits as well as cold builds.
Validation:
TMDB_API_KEY=test-key python -m pytest tests/ -q— 228 passed.git diff --checkpassed for the changed files.