Skip to content

feat(catalogs): add optional IMDb rating filter - #209

Open
yusuftunga1 wants to merge 2 commits into
TimilsinaBimal:mainfrom
yusuftunga1:feature/imdb-rating-filter
Open

yusuftunga1 wants to merge 2 commits into
TimilsinaBimal:mainfrom
yusuftunga1:feature/imdb-rating-filter

Conversation

@yusuftunga1

Copy link
Copy Markdown

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.
  • Black 24.4.0, isort 5.13.2, flake8 7.0.0, JavaScript syntax checks, and git diff --check passed for the changed files.
  • Live Cinemeta checks and a separate local demo exercised inclusive boundaries, filter reset, and empty results. The account-free demo is not included in this PR.
  • Automated coverage includes settings validation/persistence, movies and series, missing/malformed ratings, disabled behavior, cached catalogs, cold builds, and lookup failures. Full history-account-to-Stremio installation has not been tested end to end.

@vercel

vercel Bot commented Nov 6, 2026

Copy link
Copy Markdown

@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.

@TimilsinaBimal TimilsinaBimal left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. the feature is a good idea and the settings plumbing (model fields, round trip, configure page, dashboard) is clean. The catalog request path needs some changes before this can merge.

Comment on lines +83 to +86
data = {
**data,
"metas": await filter_by_imdb_rating(data["metas"], content_type, user_settings.imdb_rating_min),
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +43 to +47
# 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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 asyncio.gather.

Comment thread app/static/js/modules/form.js Outdated
rows: catalog.rows
})),
language: languageSelect?.value || 'english',
imdb_rating_min: document.getElementById('imdbRatingMin')?.value ? Number(document.getElementById('imdbRatingMin').value) : null,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread README.md
<img src="https://contrib.rocks/image?repo=TimilsinaBimal/watchly" />
</a>

### Minimum IMDb rating

Copy link
Copy Markdown
Owner

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.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)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add rating filter to list results (e.g., IMDb)

3 participants