fix(explore): match sequel catalog entries to their parent library show

MAL/AniList season entries never matched the library show they belong to.
Both backends use the catalog title as a server-side filter before verifying
external ids, and a title like "Mushoku Tensei: Jobless Reincarnation Season 2"
cannot reach a show stored as "Mushoku Tensei: Jobless Reincarnation". Measured
against a 267-show Plex library, 3 of 113 mapped sequel entries matched.

The reverse lookup now takes two ordered title candidates instead of one: the
entry's own title and its season-stripped form, with typographic punctuation
normalised because both servers miss on a curly apostrophe. That matches 77 of
113. Widening it further to romaji/native/synonym variants reached only 81, so
the cap stays at two rather than spending up to five more requests per lookup
that finds nothing.

A sequel's year is its own season's, not the parent show's, so the +/-1 year
window is dropped for one - it would exclude the very show being looked for.
That also keeps a miss at the two requests the single-title lookup already
spent. A Plex Discover item additionally skips the title search entirely by
filtering on the plex:// guid its own rating key already is, which costs no
extra request and needs no cloud lookup.

A season 2+ entry only matches when the server really has that season, which
costs one children fetch on a match. Only a season both TVDB and TMDB agree on
is gated: which provider a library numbers its seasons by is a server setting
no dataset supplies and none of it is inferable from the ids an item exposes,
so a disagreeing reference is left ungated rather than gated on a guess.

The match cache is keyed per source and per entry rather than by canonical id,
which every season of a series shares: all five Mushoku Tensei entries collapse
to imdb:tt13293588, so one season-gated result would have poisoned the rest.

Entries whose Fribb row carries no provider id at all remain unmatched. That is
an upstream mapping gap, not something to guess around with extra lookups.

close #1704
This commit is contained in:
edde746
2026-07-29 01:35:35 +02:00
parent 1165998dae
commit 0eee9f688d
20 changed files with 1350 additions and 72 deletions
+38 -10
View File
@@ -505,16 +505,44 @@ abstract class MediaServerClient {
Future<ExternalIds> fetchExternalIds(String itemId);
/// Reverse lookup: find a library movie/show matching any of [ids].
/// Both backends search by [title] (narrowed by a ±1 [year] window when
/// known, with an unfiltered fallback) and verify candidates against
/// their exact external ids. Plex checks its modern `Guid` array first,
/// then recognized legacy scalar `guid` formats; its `guid=` field filter
/// only matches the primary `plex://` guid. Jellyfin checks the inline
/// `ProviderIds`. False negatives remain possible on differing titles,
/// but title alone never produces a match. Returns null when this server
/// has no match or [kind] is not movie/show. Used to match external catalog
/// items (Explore tab) back to the user's libraries.
Future<MediaItem?> findByExternalIds(ExternalIds ids, {required MediaKind kind, String? title, int? year});
///
/// Neither backend can filter by external id — Plex's `guid=` matches only
/// the primary `plex://` guid (verified on PMS 1.43) and Jellyfin dropped
/// `anyProviderIdEquals` (silently ignored on 10.11.10) — so both search by
/// title and verify candidates against their exact external ids. Title
/// alone never produces a match.
///
/// [titles] are tried in order until one yields an id-verified candidate;
/// pass the entry's own title first and broader forms after (see
/// `titleMatchCandidates`). A sequel entry's own title never matches its
/// parent show, which is why more than one is needed. [year] applies a ±1
/// window to the first attempt only — for a sequel the catalog year is the
/// season's, not the show's.
///
/// [plexGuid] is a Plex-only escape hatch: a `plex://show/…` guid the caller
/// already holds, which the local server *can* filter on exactly, skipping
/// the title search. It is never resolved over the network — only Plex
/// Discover catalog items carry one, in their own rating key. Other backends
/// ignore it.
///
/// [season] gates the result: when the entry maps to season 2+ of a longer
/// series, a match is only returned if the server actually has that season.
/// Implementations MUST gate only on [ExternalSeasonRef.agreedSeason] — the
/// provider a library numbers its seasons by is a server-side setting no
/// dataset supplies, so a disagreeing ref is left ungated rather than gated
/// on a guess.
///
/// Returns null when this server has no match or [kind] is not movie/show.
/// Used to match external catalog items (Explore tab) back to the user's
/// libraries.
Future<MediaItem?> findByExternalIds(
ExternalIds ids, {
required MediaKind kind,
List<String> titles = const [],
int? year,
String? plexGuid,
ExternalSeasonRef? season,
});
/// Chapters and intro/credits markers for [itemId]. Plex returns both in one
/// round trip; Jellyfin combines item-level chapters with best-effort native
+6
View File
@@ -11,6 +11,7 @@ class AnilistMedia {
final String? titleEnglish;
final String? titleRomaji;
final String? titleUserPreferred;
final String? titleNative;
final String? format;
final String? status;
final int? episodes;
@@ -21,6 +22,7 @@ class AnilistMedia {
final int? seasonYear;
final int? startYear;
final List<String>? genres;
final List<String>? synonyms;
final bool isAdult;
final String? coverImageExtraLarge;
final String? coverImageLarge;
@@ -35,6 +37,7 @@ class AnilistMedia {
this.titleEnglish,
this.titleRomaji,
this.titleUserPreferred,
this.titleNative,
this.format,
this.status,
this.episodes,
@@ -45,6 +48,7 @@ class AnilistMedia {
this.seasonYear,
this.startYear,
this.genres,
this.synonyms,
this.isAdult = false,
this.coverImageExtraLarge,
this.coverImageLarge,
@@ -67,6 +71,7 @@ class AnilistMedia {
titleEnglish: title is Map ? title['english'] as String? : null,
titleRomaji: title is Map ? title['romaji'] as String? : null,
titleUserPreferred: title is Map ? title['userPreferred'] as String? : null,
titleNative: title is Map ? title['native'] as String? : null,
format: json['format'] as String?,
status: json['status'] as String?,
episodes: flexibleInt(json['episodes']),
@@ -77,6 +82,7 @@ class AnilistMedia {
seasonYear: flexibleInt(json['seasonYear']),
startYear: startDate is Map ? flexibleInt(startDate['year']) : null,
genres: _stringList(json['genres']),
synonyms: _stringList(json['synonyms']),
isAdult: json['isAdult'] == true,
coverImageExtraLarge: coverImage is Map ? coverImage['extraLarge'] as String? : null,
coverImageLarge: coverImage is Map ? coverImage['large'] as String? : null,
+43
View File
@@ -65,6 +65,24 @@ class CatalogItemIds {
return null;
}
/// Identity of *this* entry, preferring provider-native entry ids over the
/// series ids it shares with its own other seasons.
///
/// [canonicalKey] deliberately prefers imdb/tmdb/tvdb so two sources
/// describing the same title agree; that is exactly wrong for anything
/// season-specific, because every MAL/AniList season of one series carries
/// the same series ids. All five Mushoku Tensei entries collapse to
/// `imdb:tt13293588` under [canonicalKey].
String? get entryKey {
if (mal != null) return 'mal:$mal';
if (anilist != null) return 'anilist:$anilist';
if (simkl != null) return 'simkl:$simkl';
if (trakt != null) return 'trakt:$trakt';
if (plex != null) return 'plex:$plex';
if (slug != null) return 'slug:$slug';
return canonicalKey;
}
/// Every id-form key. Membership checks match on any of these so that two
/// sides carrying different id subsets (e.g. Jellyfin tmdb-only vs a Trakt
/// entry keyed by imdb) still intersect.
@@ -120,6 +138,17 @@ class CatalogItem {
/// [MediaKind.movie] or [MediaKind.show].
final MediaKind kind;
final String title;
/// Other titles the same entry is known by (MAL `alternative_titles`,
/// AniList `romaji`/`native`/`synonyms`). Media servers index one localized
/// title each, so these widen the reverse lookup without weakening it —
/// candidates are still verified by exact external id.
final List<String> altTitles;
/// Season of the parent series this entry maps to, when the provider covers
/// one season of a longer show (Fribb `season`). Null for whole-series
/// entries and every non-anime source.
final ExternalSeasonRef? season;
final int? year;
final String? overview;
final int? runtimeMinutes;
@@ -149,6 +178,8 @@ class CatalogItem {
required this.source,
required this.kind,
required this.title,
this.altTitles = const [],
this.season,
this.year,
this.overview,
this.runtimeMinutes,
@@ -168,6 +199,10 @@ class CatalogItem {
/// Kind-namespaced identity key for caches and dedupe.
String get identityKey => '${kind.id}/${ids.canonicalKey}';
/// Cache key for anything whose answer is season-specific — see
/// [CatalogItemIds.entryKey], which [identityKey] deliberately does not use.
String get entryIdentityKey => '${kind.id}/${ids.entryKey}';
/// Synthesize a [MediaItem] so catalog items flow through the existing
/// shelf/grid/card stack ([MediaHub.items] is `List<MediaItem>`).
///
@@ -196,6 +231,8 @@ class CatalogItem {
'source': source.name,
'kind': kind.id,
'title': title,
if (altTitles.isNotEmpty) 'altTitles': altTitles,
if (season != null) 'season': season!.toJson(),
if (year != null) 'year': year,
if (overview != null) 'overview': overview,
if (runtimeMinutes != null) 'runtimeMinutes': runtimeMinutes,
@@ -221,6 +258,12 @@ class CatalogItem {
(throw ArgumentError('Unknown catalog source: ${json['source']}')),
kind: MediaKind.fromString(json['kind'] as String?),
title: json['title'] as String? ?? '',
altTitles: (json['altTitles'] as List?)?.cast<String>() ?? const [],
season: switch (json['season']) {
final Map<String, Object?> s => ExternalSeasonRef.fromJson(s),
final Map s => ExternalSeasonRef.fromJson(s.cast<String, Object?>()),
_ => null,
},
year: json['year'] as int?,
overview: json['overview'] as String?,
runtimeMinutes: json['runtimeMinutes'] as int?,
@@ -118,6 +118,16 @@ class AnilistCatalogSource with CatalogWatchlistMachinery implements CatalogSour
source: CatalogSourceId.anilist,
kind: anime.isMovie ? MediaKind.movie : MediaKind.show,
title: anime.displayTitle,
altTitles: [
for (final title in <String?>[
anime.titleEnglish,
anime.titleUserPreferred,
anime.titleRomaji,
anime.titleNative,
...?anime.synonyms,
])
if (title != null && title.isNotEmpty && title != anime.displayTitle) title,
],
year: anime.year,
overview: anime.description,
runtimeMinutes: anime.runtimeMinutes,
@@ -135,6 +145,9 @@ class AnilistCatalogSource with CatalogWatchlistMachinery implements CatalogSour
tmdb: row?.tmdbIds?.firstOrNull,
tvdb: row?.tvdbId,
),
season: row == null || (row.tvdbSeason == null && row.tmdbSeason == null)
? null
: ExternalSeasonRef(tvdb: row.tvdbSeason, tmdb: row.tmdbSeason),
posterUrl: anime.posterUrl,
backdropUrl: anime.backdropUrl,
);
@@ -1,6 +1,10 @@
import 'package:flutter/foundation.dart';
import '../../media/media_item.dart';
import '../../media/media_kind.dart';
import '../../models/catalog/catalog_item.dart';
import '../../providers/multi_server_provider.dart';
import '../../utils/title_match_candidates.dart';
/// Matches external catalog items back to the user's libraries.
///
@@ -14,24 +18,53 @@ class CatalogLibraryMatcher {
static const Duration negativeTtl = Duration(minutes: 10);
final MultiServerProvider _multiServer;
final DateTime Function() _now;
final Map<String, ({DateTime at, List<MediaItem> items})> _cache = {};
CatalogLibraryMatcher(this._multiServer);
CatalogLibraryMatcher(this._multiServer) : _now = DateTime.now;
@visibleForTesting
CatalogLibraryMatcher.withClock(this._multiServer, this._now);
Future<List<MediaItem>> match(CatalogItem item) async {
if (!item.ids.hasAny) return const [];
final key = item.identityKey;
// Do not use `identityKey`: its canonical series ids make every MAL/AniList
// season collide. All five Mushoku Tensei entries (`mal39535 s1`,
// `mal45576 s1`, `mal51179 s2`, `mal55888 s2`, `mal59193 s3`) collapse to
// `imdb:tt13293588`, so the first season-gated result would poison the rest.
// Namespace by source too: MAL and AniList can share a MAL id while
// contributing different localized title candidates.
final key = '${item.source.name}/${item.entryIdentityKey}';
final cached = _cache[key];
if (cached != null && (cached.items.isNotEmpty || DateTime.now().difference(cached.at) < negativeTtl)) {
if (cached != null && (cached.items.isNotEmpty || _now().difference(cached.at) < negativeTtl)) {
return cached.items;
}
// A sequel entry's year is its own season's, not the parent show's, so a
// ±1 window around it excludes the very show we are looking for. Fribb
// does not map a season for every entry, so fall back to the title: a
// strippable season suffix says "sequel" just as reliably. Dropping the
// year here also keeps the lookup at two requests, because the client no
// longer spends one on a year-filtered attempt that cannot match.
final isSequel = (item.season?.isSequel ?? false) || stripSeasonSuffix(item.title) != null;
final matches = await _multiServer.aggregationService.findByExternalIdsAcrossServers(
item.ids.toExternalIds(),
kind: item.kind,
title: item.title,
year: item.year,
titles: titleMatchCandidates([item.title, ...item.altTitles]),
year: isSequel ? null : item.year,
plexGuid: _plexGuidFor(item),
season: item.season,
);
_cache[key] = (at: DateTime.now(), items: matches);
_cache[key] = (at: _now(), items: matches);
return matches;
}
/// The exact `plex://` guid for a Plex Discover item, which its own rating
/// key already is. Free — no request, no cloud lookup; other sources get
/// null and fall back to the title candidates.
String? _plexGuidFor(CatalogItem item) {
final plexId = item.ids.plex;
if (item.source != CatalogSourceId.plex || plexId == null || plexId.isEmpty) return null;
return 'plex://${item.kind == MediaKind.movie ? 'movie' : 'show'}/$plexId';
}
}
@@ -118,6 +118,15 @@ class MalCatalogSource with CatalogWatchlistMachinery implements CatalogSource {
source: CatalogSourceId.mal,
kind: anime.isMovie ? MediaKind.movie : MediaKind.show,
title: anime.displayTitle,
altTitles: [
for (final title in <String?>[
anime.alternativeTitles?.en,
anime.title,
anime.alternativeTitles?.ja,
...?anime.alternativeTitles?.synonyms,
])
if (title != null && title.isNotEmpty && title != anime.displayTitle) title,
],
year: anime.year,
overview: anime.synopsis,
runtimeMinutes: anime.runtimeMinutes,
@@ -134,6 +143,9 @@ class MalCatalogSource with CatalogWatchlistMachinery implements CatalogSource {
tmdb: row?.tmdbIds?.firstOrNull,
tvdb: row?.tvdbId,
),
season: row == null || (row.tvdbSeason == null && row.tmdbSeason == null)
? null
: ExternalSeasonRef(tvdb: row.tvdbSeason, tmdb: row.tmdbSeason),
posterUrl: anime.mainPicture?.primary,
);
+12 -3
View File
@@ -597,16 +597,25 @@ class DataAggregationService {
Future<List<MediaItem>> findByExternalIdsAcrossServers(
ExternalIds ids, {
required MediaKind kind,
String? title,
List<String> titles = const [],
int? year,
String? plexGuid,
ExternalSeasonRef? season,
}) async {
if (!ids.hasAny) return [];
if (!ids.hasAny && plexGuid == null) return [];
final clients = _serverManager.onlineClients;
if (clients.isEmpty) return [];
final futures = clients.entries.map((entry) async {
try {
return await entry.value.findByExternalIds(ids, kind: kind, title: title, year: year);
return await entry.value.findByExternalIds(
ids,
kind: kind,
titles: titles,
year: year,
plexGuid: plexGuid,
season: season,
);
} catch (e, st) {
appLogger.w('External-id lookup failed on ${entry.key}', error: e, stackTrace: st);
return null;
+39 -21
View File
@@ -1110,47 +1110,65 @@ mixin _JellyfinBrowseMethods on _JellyfinClientInternals {
return _mapItems([...results.first, ...results[1]]);
}
/// Jellyfin removed `anyProviderIdEquals`, so the reverse lookup is a
/// title search verified against each candidate's inline `ProviderIds` —
/// exact-id verification, so localized-title misses are possible but a
/// wrong item can never match.
/// Jellyfin removed `anyProviderIdEquals` (silently ignored on 10.11.10, so
/// it returns the unfiltered page), leaving a title search verified against
/// each candidate's inline `ProviderIds`. [plexGuid] is a Plex-only hint and
/// has no meaning in Jellyfin's provider-id model, so it is ignored.
///
/// When [year] is known, a ±1 `years=` window (comma-OR, verified on JF
/// 10.11) is applied first so short/common titles keep the true match
/// inside the 20-item response; a second unfiltered attempt covers items
/// with missing or off-window year metadata.
/// Jellyfin cannot report season ordering to a non-admin: on 10.11.10,
/// `/Library/VirtualFolders` returns 403 and Series items omit
/// `DisplayOrder`. Resolve against an unknown provider rather than guessing
/// from `ProviderIds`, so only seasons on which TVDB and TMDB agree are gated.
@override
Future<MediaItem?> findByExternalIds(ExternalIds ids, {required MediaKind kind, String? title, int? year}) async {
Future<MediaItem?> findByExternalIds(
ExternalIds ids, {
required MediaKind kind,
List<String> titles = const [],
int? year,
String? plexGuid,
ExternalSeasonRef? season,
}) async {
final itemType = switch (kind) {
MediaKind.movie => 'Movie',
MediaKind.show => 'Series',
_ => null,
};
if (itemType == null || !ids.hasAny || title == null || title.isEmpty) return null;
if (itemType == null || !ids.hasAny || titles.isEmpty) return null;
Future<MediaItem?> attempt(String? years) async {
final seasonIndex = season?.agreedSeason;
final shouldGateSeason = kind == MediaKind.show && seasonIndex != null && seasonIndex > 1;
// Not `seasonIndex`: when the two providers disagree the season number is
// unusable but the entry is still a sequel, and the ±1 window around a
// sequel's own year excludes the parent show (its year is season one's).
final skipYearWindow = season?.isSequel ?? false;
for (var index = 0; index < titles.length; index++) {
final isFirstCandidate = index == 0;
final years = isFirstCandidate && year != null && !skipYearWindow ? '${year - 1},$year,${year + 1}' : null;
final candidates = await _fetchItemsArray('/Items', {
'userId': connection.userId,
'SearchTerm': title,
'SearchTerm': titles[index],
'Recursive': 'true',
'Limit': '20',
'Limit': isFirstCandidate ? '20' : '50',
'IncludeItemTypes': itemType,
'Fields': 'ProviderIds,$_browseFields',
'years': ?years,
...jellyfinImageQueryParameters,
});
final match = ExternalIds.jellyfinCandidateMatching(candidates, ids);
if (match == null) return null;
if (match == null) continue;
final item = _mapItems([match]).firstOrNull;
if (item == null) return null;
if (item == null) continue;
if (shouldGateSeason) {
final children = await fetchChildren(item.id);
if (!children.any((child) => child.kind == MediaKind.season && child.index == seasonIndex)) {
return null;
}
}
return _withLibraryFromAncestors(item);
}
if (year != null) {
final match = await attempt('${year - 1},$year,${year + 1}');
if (match != null) return match;
}
return attempt(null);
return null;
}
/// Best-effort library stamp for items found outside a library context
+76 -25
View File
@@ -3761,37 +3761,77 @@ class PlexClient
return ExternalIds.fromGuids(guids);
}
/// Drop a sequel match when the server does not actually have that season.
///
/// Only an [ExternalSeasonRef.agreedSeason] is gated on. Which provider a
/// library numbers its seasons by is a server-side setting no dataset can
/// supply, and reading it costs two extra requests per lookup, so a
/// disagreeing ref is left ungated rather than gated on a guess.
Future<MediaItem?> _applyExternalIdSeasonGate(
Map<String, dynamic> metadata, {
required MediaKind kind,
required ExternalSeasonRef? season,
}) async {
final item = PlexMappers.mediaItem(_createTaggedMetadataWithLibrary(metadata));
if (kind != MediaKind.show) return item;
final seasonIndex = season?.agreedSeason;
if (seasonIndex == null || seasonIndex <= 1) return item;
final ratingKey = metadata['ratingKey']?.toString();
// Cannot ask the question => do not gate.
if (ratingKey == null || ratingKey.isEmpty) return item;
final children = await fetchChildren(ratingKey);
return children.any((child) => child.kind == MediaKind.season && child.index == seasonIndex) ? item : null;
}
/// Server-wide title filter + client-side external-ID verification. Plex's
/// `guid=` field filter matches only the item's primary `plex://` guid
/// (verified against PMS 1.43) — never the external `imdb://`/`tmdb://`
/// ids in the modern `Guid` array — so this mirrors the Jellyfin
/// title-search-and-verify approach. Recognized legacy scalar `guid` values
/// are checked only after modern verification fails.
///
/// When [year] is known, a ±1 window is applied server-side first
/// (`year=` takes comma-separated values as OR, verified on PMS 1.43) so
/// short/common titles keep the true match inside the 20-item response;
/// the year filter drops items with no year metadata, hence the
/// unfiltered second attempt.
/// (verified against PMS 1.43), so external ids in modern `Guid` arrays are
/// verified after title search. A resolved primary [plexGuid] can use the
/// exact server-side filter directly.
@override
Future<MediaItem?> findByExternalIds(ExternalIds ids, {required MediaKind kind, String? title, int? year}) async {
Future<MediaItem?> findByExternalIds(
ExternalIds ids, {
required MediaKind kind,
List<String> titles = const [],
int? year,
String? plexGuid,
ExternalSeasonRef? season,
}) async {
final plexType = switch (kind) {
MediaKind.movie => 1,
MediaKind.show => 2,
_ => null,
};
if (plexType == null || !ids.hasAny || title == null || title.isEmpty) {
return null;
if (plexType == null) return null;
if (!ids.hasAny && plexGuid == null) return null;
if (titles.isEmpty && plexGuid == null) return null;
if (plexGuid != null) {
final response = await _getWithFailover(
'/library/all',
queryParameters: {'guid': plexGuid, 'type': plexType, 'includeGuids': 1},
);
final metadata = _getFirstMetadataJson(response);
if (metadata != null) {
return _applyExternalIdSeasonGate(metadata, kind: kind, season: season);
}
}
Future<({Map<String, dynamic>? modern, Map<String, dynamic>? legacy})> attempt(String? years) async {
Future<({Map<String, dynamic>? modern, Map<String, dynamic>? legacy})> attempt(
String title, {
required int size,
String? years,
}) async {
final response = await _getWithFailover(
'/library/all',
queryParameters: {
'title': title,
'type': plexType,
'includeGuids': 1,
'X-Plex-Container-Size': 20,
'X-Plex-Container-Size': size,
'year': ?years,
},
);
@@ -3813,18 +3853,29 @@ class PlexClient
return (modern: null, legacy: legacy);
}
({Map<String, dynamic>? modern, Map<String, dynamic>? legacy})? filtered;
if (year != null) {
filtered = await attempt('${year - 1},$year,${year + 1}');
final modern = filtered.modern;
if (modern != null) {
return PlexMappers.mediaItem(_createTaggedMetadataWithLibrary(modern));
// Not `resolve(null)`: when the two providers disagree the season number is
// unresolvable but the entry is still a sequel, and the ±1 window around a
// sequel's own year excludes the parent show (its year is season one's).
final skipYearWindow = season?.isSequel ?? false;
for (var index = 0; index < titles.length; index++) {
final title = titles[index];
final size = index == 0 ? 20 : 50;
({Map<String, dynamic>? modern, Map<String, dynamic>? legacy})? filtered;
if (index == 0 && year != null && !skipYearWindow) {
filtered = await attempt(title, size: size, years: '${year - 1},$year,${year + 1}');
final modern = filtered.modern;
if (modern != null) {
return _applyExternalIdSeasonGate(modern, kind: kind, season: season);
}
}
final unfiltered = await attempt(title, size: size);
final match = unfiltered.modern ?? filtered?.legacy ?? unfiltered.legacy;
if (match != null) {
return _applyExternalIdSeasonGate(match, kind: kind, season: season);
}
}
final unfiltered = await attempt(null);
final match = unfiltered.modern ?? filtered?.legacy ?? unfiltered.legacy;
return match == null ? null : PlexMappers.mediaItem(_createTaggedMetadataWithLibrary(match));
return null;
}
@override
@@ -27,6 +27,7 @@ class AnilistClient implements DisposableTrackerClient {
english
romaji
userPreferred
native
}
format
status
@@ -40,6 +41,7 @@ class AnilistClient implements DisposableTrackerClient {
year
}
genres
synonyms
isAdult
coverImage {
extraLarge
+46
View File
@@ -1,3 +1,49 @@
/// Season of a parent series as numbered by each external provider.
///
/// Populated from the Fribb mapping's `season: {tvdb: N, tmdb: M}`.
class ExternalSeasonRef {
final int? tvdb;
final int? tmdb;
const ExternalSeasonRef({this.tvdb, this.tmdb});
bool get hasAny => tvdb != null || tmdb != null;
/// True when this entry covers a later season under either provider.
///
/// Independent of [agreedSeason]: it answers "is this a sequel at all",
/// which stays knowable when the two providers disagree. Callers use it to
/// drop the ±1 year window, because a sequel's catalog year is its own
/// season's, not the parent show's.
bool get isSequel => (tvdb ?? 0) > 1 || (tmdb ?? 0) > 1;
/// The season number both providers mapped and agree on, else null.
///
/// TVDB and TMDB disagree on split-cour / continuation seasons — a season
/// the former numbers `2` the latter often folds into `1` at an episode
/// offset. Which one a library follows is a server-side setting, not
/// anything a dataset can tell us, and it is NOT inferable from which ids an
/// item exposes (a Plex show carries all three regardless). So when the two
/// disagree the honest answer is "cannot tell" and the caller must not gate.
///
/// A missing number is not agreement either: `tvdb: 2, tmdb: null` means
/// Fribb has no TMDB season mapping, and a TMDB-ordered server would number
/// that season by the mapping we do not have. Holds for 1133 of the 1185
/// gate-eligible Fribb rows; the other 52 simply go ungated.
int? get agreedSeason => tvdb != null && tvdb == tmdb ? tvdb : null;
Map<String, Object?> toJson() => {if (tvdb != null) 'tvdb': tvdb, if (tmdb != null) 'tmdb': tmdb};
factory ExternalSeasonRef.fromJson(Map<String, Object?> json) =>
ExternalSeasonRef(tvdb: json['tvdb'] as int?, tmdb: json['tmdb'] as int?);
@override
bool operator ==(Object other) => other is ExternalSeasonRef && other.tvdb == tvdb && other.tmdb == tmdb;
@override
int get hashCode => Object.hash(tvdb, tmdb);
}
/// External IDs (IMDb / TMDB / TVDB) extracted from a media server's
/// metadata. Shared by the Trakt and tracker resolvers.
///
+96
View File
@@ -0,0 +1,96 @@
/// Season markers that begin a sequel suffix. Everything from the first match
/// to the end of the string is dropped, which collapses stacked suffixes
/// (`… Season 2 Part 2`, `… Season 3: The Culling Game Part 1`,
/// `… Season 2 -Arise from the Shadow-`) in one pass.
final List<RegExp> _seasonSuffixes = [
RegExp(r'\s+season\s+\d+\b.*$', caseSensitive: false),
RegExp(r'\s+\d+(?:st|nd|rd|th)\s+season\b.*$', caseSensitive: false),
RegExp(r'\s+final\s+season\b.*$', caseSensitive: false),
RegExp(r'\s+(?:part|cour|act)\.?\s*(?:\d+|i{1,3}v?|vi{0,3}|ix|x)\b.*$', caseSensitive: false),
RegExp(r'\s+(?:ii|iii|iv|v|vi|vii|viii|ix|x)$', caseSensitive: false),
];
/// A bare trailing number usually marks a sequel (`Isekai Quartet 3`), so it is
/// stripped last — but only when the token before it does not expect a number.
/// `Kaiju No. 8` and `Vol. 3` are titles, not season two of anything.
final RegExp _bareTrailingNumber = RegExp(r'\s+\d+$');
final RegExp _numberedNoun = RegExp(r'\b(?:no|vol|pt|ep|episode|chapter)\.?\s+\d+$', caseSensitive: false);
/// Typographic variants media servers index differently from what catalog
/// providers emit. Plex tokenizes on these, Jellyfin substring-matches them,
/// and both miss `Journeys` against a stored `Journey's`.
const Map<String, String> _punctuation = {
'\u2019': "'", // right single quote
'\u2018': "'",
'\u201C': '"',
'\u201D': '"',
'\u2013': '-', // en dash
'\u2014': '-', // em dash
'\u30FB': ' ', // katakana middle dot
'\uFF1A': ':',
'\uFF01': '!',
'\uFF1F': '?',
};
/// Drop the sequel suffix from [title], or return null when it has none.
String? stripSeasonSuffix(String title) {
var out = title;
for (final marker in _seasonSuffixes) {
out = out.replaceFirst(marker, '');
}
if (!_numberedNoun.hasMatch(out)) {
out = out.replaceFirst(_bareTrailingNumber, '');
}
out = out.trim();
return out.isEmpty || out == title.trim() ? null : out;
}
String _normalize(String title) {
var out = title;
for (final entry in _punctuation.entries) {
out = out.replaceAll(entry.key, entry.value);
}
return out.trim();
}
/// Ordered, deduplicated title candidates for a media-server reverse lookup.
///
/// Neither backend can filter by external id (Plex's `guid=` matches only the
/// primary `plex://` guid; Jellyfin dropped `anyProviderIdEquals`), so the
/// title is the only candidate filter available and a sequel entry's own
/// title — `You and I Are Polar Opposites Season 2` — never matches the parent
/// show. Each input contributes itself plus its season-stripped form; the
/// caller tries them in order and stops at the first candidate whose external
/// ids verify, so a broader title can never widen what actually matches.
///
/// [limit] bounds the request fan-out, and 2 is deliberate: the entry's own
/// title plus its season-stripped form matched 77 of 113 real sequel entries
/// against a 267-show Plex library, where the unexpanded title alone matched
/// 3. Raising it to 6 (adding romaji/native/synonym variants) reached only 81
/// — four more entries for up to five more requests per lookup that finds
/// nothing, which is the common case on a discovery tab. Two candidates cost
/// the same two requests the single-title lookup already spent.
///
/// Each title is emitted immediately followed by its stripped form rather than
/// in two passes, so the cap can never spend every slot on unstripped titles
/// and never try the one candidate that actually reaches the parent show.
List<String> titleMatchCandidates(Iterable<String?> titles, {int limit = 2}) {
final out = <String>[];
final seen = <String>{};
bool add(String? raw) {
if (raw == null || out.length >= limit) return false;
final title = _normalize(raw);
if (title.isEmpty || !seen.add(title.toLowerCase())) return false;
out.add(title);
return true;
}
for (final title in titles) {
if (out.length >= limit) break;
final normalized = title == null ? null : _normalize(title);
add(normalized);
if (normalized != null) add(stripSeasonSuffix(normalized));
}
return out;
}