diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-08-29 16:07:03 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-08-29 16:07:03 +0200 |
| commit | 78eef92bfa3513a81ac64d4377f8300c533aaa6c (patch) | |
| tree | e056e6b4fceca76ab67586548f0aa6ed80f925bc /packages/meshbay-node/src/meshbay_node/transport | |
| parent | 236e5e811355212945b89c4f7a99df5c837e97c7 (diff) | |
| parent | 4d167e9f958d26c1db920274974ae446426928e3 (diff) | |
| download | meshbay-78eef92bfa3513a81ac64d4377f8300c533aaa6c.tar.gz | |
Merge branch 'feat/videos-matching-v8-v13'
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/transport')
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/transport/webrtc_server.py | 165 |
1 files changed, 111 insertions, 54 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/transport/webrtc_server.py b/packages/meshbay-node/src/meshbay_node/transport/webrtc_server.py index 788a8f1..af6bf08 100644 --- a/packages/meshbay-node/src/meshbay_node/transport/webrtc_server.py +++ b/packages/meshbay-node/src/meshbay_node/transport/webrtc_server.py @@ -70,6 +70,7 @@ from meshbay_common.adminop import ( OP_TMDB_ENABLED, OP_VIDEO_ROOT, OP_TMDB_OVERRIDE, + OP_TMDB_REMATCH, OP_MUSICBRAINZ_ENABLED, OP_AUDIO_ROOT, OP_PHOTO_ROOTS, @@ -475,6 +476,8 @@ class WebRTCPeerSession: self._spawn(self._do_tmdb_search_request(msg)) elif mtype == MNP.TMDB_OVERRIDE: self._do_tmdb_override(msg) + elif mtype == MNP.TMDB_REMATCH: + self._do_tmdb_rematch(msg) elif mtype == MNP.MUSICBRAINZ_ENABLED: self._do_musicbrainz_enabled(msg) elif mtype == MNP.MUSIC_META_REQ: @@ -3171,82 +3174,133 @@ class WebRTCPeerSession: except Exception: pass + def _do_tmdb_rematch(self, msg: dict) -> None: + """ + An operator dropping one file's cached TMDB match so it re-resolves + with the current matcher (§10.1/V13) — the one-click alternative to + the full search-and-pick "Fix match" flow, and reachable without + SSH (`meshbay-node video rematch` clears a whole group). Signed like + `tmdb_override`: `media_cache` is shared node-wide. + """ + file_id = msg.get("file_id") + if not isinstance(file_id, str) or not file_id: + self._send({"type": "error", "detail": "Missing file_id"}) + return + if not self._group_ctx()["index"].get_entry(file_id): + self._send({"type": "error", "detail": "File not found"}) + return + if not self._has_admin_authority(): + self._send({"type": "error", "detail": "No authorized key for this"}) + return + self._issue_admin_challenge(OP_TMDB_REMATCH, f"file_id={file_id}") + + async def _admin_exec_tmdb_rematch( + self, pending: dict, transcript: bytes, sig: bytes, + ) -> None: + subject = pending["subject"] + if not await self._verify_admin_sig(transcript, sig): + self._send({"type": "error", "detail": "Signature verification failed"}) + self._audit("admin_auth_failed", f"tmdb_rematch:{subject}") + return + file_id = dict(part.split("=", 1) for part in subject.split(","))["file_id"] + media_cache = self._ctx.get("media_cache") + if media_cache is None: + self._send({"type": "error", "detail": "Media cache not available"}) + return + await media_cache.drop_tmdb_match(file_id) + self._audit("tmdb_rematch", subject) + + notice = {"type": MNP.TMDB_REMATCH_ACK, "v": MNP_VERSION, "file_id": file_id} + for uid, session in list(self._peer_registry().items()): + try: + session._send(notice) + except Exception: + pass + async def _tmdb_search(self, tmdb_client, entry, is_show: bool): """ - §3.3's retry ladder. TMDB's own top result is still trusted per - query (§3.3's last row — no local re-ranking of *its* list); what - changed is that the ladder now *scores every candidate query* and - keeps the best, instead of returning the first that merely clears - 0.6. + §3.3's retry ladder — same shape for movies and shows (§10.1/V8). + TMDB's own top result is still trusted per query (§3.3's last row — + no local re-ranking of *its* list); what the ladder adds is that it + *scores every candidate query* and keeps the best, instead of + returning the first that merely clears 0.6. The bare parsed title is the weakest query: guessit drops a - "Volume 2", strips a real subtitle into `alternative_title`, and - renders a sequel number where TMDB uses a Roman numeral. A wrong - film that happened to score ~0.7 against that weak query — a - same-year making-of documentary, or a franchise entry whose - localized TMDB title *is* the franchise name — used to win outright - before `alternative_title` or the Roman-numeral variant was ever - tried. Found live (2026-08-29): a numbered sequel matched a - same-year documentary; a two-volume film's second part matched the - first; several franchise entries matched one early entry. + "Volume 2", strips a real subtitle into `alternative_title`, renders + a sequel index where TMDB spells it differently, and a show's folder + name can carry a year or release-group noise. A wrong entry that + scored ~0.7 against that weak query — a same-year making-of + documentary, a franchise entry whose localized TMDB title *is* the + franchise name, a season-specific promo entry standing in for a + whole show — used to win outright before a stronger candidate was + ever tried. Found live (2026-08-29). """ from meshbay_node.indexer import title_parse if is_show: title = entry.display_title or title_parse.naive_title(entry.name) - result, ratio = await tmdb_client.search_tv(title) - if result is None or ratio < 0.6: - naive = title_parse.naive_title(entry.name) - if naive != title: - result, ratio = await tmdb_client.search_tv(naive) - return result, ratio - - def _release_year(res: dict) -> int | None: - d = str(res.get("release_date") or res.get("first_air_date") or "") - return int(d[:4]) if d[:4].isdigit() else None + name_naive = title_parse.naive_title(entry.name) + year = title_parse.year_in(title) or title_parse.year_in(entry.name) + extra = [c for c in (name_naive, title_parse.clean_query(title)) + if c and c != title] + return await self._tmdb_ladder( + tmdb_client.search_tv, title, extra, year, strong_extra=False) parsed = title_parse.parse_movie_filename(entry.name) title = entry.display_title or parsed.display_title or parsed.naive_title + strong = [c for c in (parsed.alt_title, *title_parse.sequel_variants(title)) if c] + extra = [c for c in (*strong, parsed.naive_title) if c and c != title] + return await self._tmdb_ladder( + tmdb_client.search_movie, title, extra, parsed.year, + strong_extra=bool(strong)) - # Fast path, unchanged in effect: a strong direct hit still returns - # on the first call, so the common case costs exactly one request - # and the new ladder below only engages in the ambiguous 0<ratio<0.85 - # zone where every one of the live bugs lived. - result, ratio = await tmdb_client.search_movie(title, parsed.year) - if result is not None and ratio >= 0.85: - return result, ratio - - best_result, best_score = (result, ratio) if result is not None else (None, 0.0) + @staticmethod + async def _tmdb_ladder(search_fn, primary: str, extra: list[str], + year: int | None, strong_extra: bool): + """ + `search_fn(query, year) -> (result|None, ratio)`. Try `primary`, + return at once on a confident hit (ratio >= 0.85 — the common case, + one request). Otherwise score each `extra` candidate and keep the + best. `strong_extra` says whether `extra` contains anything more + specific than a punctuation-normalised restatement of `primary` + (an alternative_title, a sequel variant); when it does not and the + primary hit is already decent, the remaining calls are skipped + (§10.1/V11 — they almost never win and cost a round trip each). + """ + def _year_of(res: dict) -> int | None: + d = str(res.get("release_date") or res.get("first_air_date") or "") + return int(d[:4]) if d[:4].isdigit() else None - def _apply_year_rescue(res: dict, r: float) -> float: - # Only for a query whose own top hit is weak on its face - # (ratio < 0.6): TMDB already year-filtered the search, so its - # top result landing exactly on the filename's year is a hard - # corroborating signal that the low string ratio is a - # localized/rearranged title, not a wrong film. Never lets year - # equality outrank a genuinely strong textual match elsewhere. - if r < 0.6 and parsed.year and _release_year(res) == parsed.year: + def _rescue(res: dict, r: float) -> float: + # A sub-0.6 hit whose result lands on the exact requested year: + # TMDB already year-filtered the search, so this is a hard + # corroboration that the low ratio is a localised/rearranged + # title, not a wrong entry. Never overrides a confident hit. + if r < 0.6 and year and _year_of(res) == year: return max(r, 0.6) return r + result, ratio = await search_fn(primary, year) + if result is not None and ratio >= 0.85: + return result, ratio + best_result, best_score = (result, ratio) if result is not None else (None, 0.0) if best_result is not None: - best_score = _apply_year_rescue(best_result, best_score) + best_score = _rescue(best_result, best_score) + if best_score >= 0.6 and not strong_extra: + return best_result, best_score - for candidate in filter(None, [parsed.alt_title, - *title_parse.sequel_variants(title), - parsed.naive_title]): - if candidate == title: + for candidate in extra: + if candidate == primary: continue - r2, ratio2 = await tmdb_client.search_movie(candidate, parsed.year) - if r2 is None and parsed.year: - # A year-filtered search that finds nothing: the filename's - # year tag may be an edition/regional year TMDB doesn't - # carry. Retry the same candidate unconstrained before - # dropping it. - r2, ratio2 = await tmdb_client.search_movie(candidate) + r2, ratio2 = await search_fn(candidate, year) + if r2 is None and year: + # A year-filtered search that finds nothing: the year tag + # may be an edition/regional year TMDB doesn't carry. Retry + # the candidate unconstrained before dropping it. + r2, ratio2 = await search_fn(candidate, None) if r2 is None: continue - score2 = _apply_year_rescue(r2, ratio2) + score2 = _rescue(r2, ratio2) if score2 > best_score: best_result, best_score = r2, score2 if best_score >= 0.85: @@ -3866,6 +3920,9 @@ class WebRTCPeerSession: elif pending["op"] == OP_TMDB_OVERRIDE: self._spawn( self._admin_exec_tmdb_override(pending, transcript, sig_bytes)) + elif pending["op"] == OP_TMDB_REMATCH: + self._spawn( + self._admin_exec_tmdb_rematch(pending, transcript, sig_bytes)) elif pending["op"] == OP_MUSICBRAINZ_ENABLED: self._spawn( self._admin_exec_musicbrainz_enabled(pending, transcript, sig_bytes)) |