Store full TMDB data, add rate limiting and stale-entry refresh
Backend - Route all TMDB requests through a rate limiter (20 req/s, 8 concurrent) with 429/5xx retries; stop logging the API key - Fetch external ids, certification, keywords, videos, watch providers and alternative titles in the entry details request - Shared entry mapper used by create, refresh, season refresh and importer - New columns on entries, ids, ratings, seasons, episodes and people, with an idempotent startup migration (adds missing columns, widens types) - Fix TV IMDb ids, network country/logo, episode tmdb_id on refresh, and genre/network removal on refresh - Add stale-entries maintenance job (count, start, cancel) and live recommendations endpoint Frontend - Entry page: certification, tagline, creators, in-app trailer, vote count, TV/movie details, network logos, external links, keywords, where to watch, and recommendations - Episode page: directors, writers and guest stars - Settings: TMDB refresh card with live progress
This commit is contained in:
1 parent
cee6f7f961
commit
a9ff2d089a
33 files changed
+1722
-483
No files matched your search
@@ -0,0 +1,263 @@
|
||||
/**
|
||||
* Maps TMDB responses (from tmdbService.fetchEntryData / fetchSeasonData) to
|
||||
* the column values we store. Used by entry creation, the entry refresh and
|
||||
* the legacy importer so every path saves the same fields.
|
||||
*/
|
||||
|
||||
const CERTIFICATION_REGIONS = ["US", "GB"];
|
||||
const MAX_VIDEOS = 12;
|
||||
const MAX_KEYWORDS = 30;
|
||||
const MAX_EPISODE_CREW = 15;
|
||||
const MAX_GUEST_STARS = 25;
|
||||
|
||||
const pick = (obj, keys) =>
|
||||
obj ? Object.fromEntries(keys.map((k) => [k, obj[k] ?? null])) : null;
|
||||
|
||||
function isTv(data) {
|
||||
return data.first_air_date !== undefined || data.name !== undefined;
|
||||
}
|
||||
|
||||
/** First non-empty certification, preferring US, then the origin country. */
|
||||
function mapCertification(data) {
|
||||
const regions = [...CERTIFICATION_REGIONS, ...(data.origin_country ?? [])];
|
||||
|
||||
if (data.content_ratings) {
|
||||
const results = data.content_ratings.results ?? [];
|
||||
for (const region of regions) {
|
||||
const rating = results.find((r) => r.iso_3166_1 === region)?.rating;
|
||||
if (rating) return rating;
|
||||
}
|
||||
return results.find((r) => r.rating)?.rating ?? null;
|
||||
}
|
||||
|
||||
if (data.release_dates) {
|
||||
const results = data.release_dates.results ?? [];
|
||||
const certFor = (r) => {
|
||||
const dates = (r?.release_dates ?? []).filter((d) => d.certification);
|
||||
// Prefer theatrical (3), then limited (2), then anything
|
||||
const byType =
|
||||
dates.find((d) => d.type === 3) ??
|
||||
dates.find((d) => d.type === 2) ??
|
||||
dates[0];
|
||||
return byType?.certification || null;
|
||||
};
|
||||
for (const region of regions) {
|
||||
const cert = certFor(results.find((r) => r.iso_3166_1 === region));
|
||||
if (cert) return cert;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function mapVideos(videos) {
|
||||
const results = (videos?.results ?? []).filter(
|
||||
(v) => v.site === "YouTube" && v.key,
|
||||
);
|
||||
const rank = (v) =>
|
||||
(v.type === "Trailer" ? 0 : v.type === "Teaser" ? 1 : 2) +
|
||||
(v.official ? 0 : 3);
|
||||
return results
|
||||
.sort(
|
||||
(a, b) =>
|
||||
rank(a) - rank(b) ||
|
||||
new Date(b.published_at) - new Date(a.published_at),
|
||||
)
|
||||
.slice(0, MAX_VIDEOS)
|
||||
.map((v) =>
|
||||
pick(v, ["key", "name", "type", "official", "published_at", "iso_639_1"]),
|
||||
);
|
||||
}
|
||||
|
||||
/** Keeps per-country provider lists, trimmed to what the UI needs. */
|
||||
function mapWatchProviders(providers) {
|
||||
const results = providers?.results;
|
||||
if (!results) return null;
|
||||
|
||||
const trimList = (list) =>
|
||||
list?.map((p) =>
|
||||
pick(p, [
|
||||
"provider_id",
|
||||
"provider_name",
|
||||
"logo_path",
|
||||
"display_priority",
|
||||
]),
|
||||
);
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(results).map(([country, info]) => [
|
||||
country,
|
||||
{
|
||||
link: info.link ?? null,
|
||||
flatrate: trimList(info.flatrate),
|
||||
free: trimList(info.free),
|
||||
ads: trimList(info.ads),
|
||||
rent: trimList(info.rent),
|
||||
buy: trimList(info.buy),
|
||||
},
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
function mapEpisodeSummary(episode) {
|
||||
return pick(episode, [
|
||||
"id",
|
||||
"name",
|
||||
"season_number",
|
||||
"episode_number",
|
||||
"air_date",
|
||||
"episode_type",
|
||||
"runtime",
|
||||
"still_path",
|
||||
]);
|
||||
}
|
||||
|
||||
/** Entry table columns. */
|
||||
function mapEntryFields(data) {
|
||||
const tv = isTv(data);
|
||||
const keywords = (data.keywords?.keywords ?? data.keywords?.results ?? [])
|
||||
.slice(0, MAX_KEYWORDS)
|
||||
.map((k) => k.name);
|
||||
const alternativeTitles = (
|
||||
data.alternative_titles?.titles ??
|
||||
data.alternative_titles?.results ??
|
||||
[]
|
||||
).map((t) => pick(t, ["iso_3166_1", "title", "type"]));
|
||||
|
||||
return {
|
||||
title: data.title || data.name,
|
||||
original_title: data.original_title || data.original_name,
|
||||
release_date: data.release_date || data.first_air_date || null,
|
||||
release_status: data.status,
|
||||
runtime:
|
||||
data.runtime ||
|
||||
data.episode_run_time?.[0] ||
|
||||
data.last_episode_to_air?.runtime ||
|
||||
null,
|
||||
|
||||
tagline: data.tagline || null,
|
||||
storyline: data.overview,
|
||||
country: data.production_countries?.[0]?.name ?? null,
|
||||
|
||||
status: data.status,
|
||||
certification: mapCertification(data),
|
||||
network_studio:
|
||||
data.networks?.[0]?.name ?? data.production_companies?.[0]?.name ?? null,
|
||||
|
||||
adult: !!data.adult,
|
||||
tmdb_popularity: data.popularity ?? null,
|
||||
original_language: data.original_language ?? null,
|
||||
origin_country: data.origin_country ?? null,
|
||||
spoken_languages:
|
||||
data.spoken_languages?.map((l) =>
|
||||
pick(l, ["iso_639_1", "english_name", "name"]),
|
||||
) ?? null,
|
||||
production_countries: data.production_countries ?? null,
|
||||
production_companies:
|
||||
data.production_companies?.map((c) =>
|
||||
pick(c, ["id", "name", "logo_path", "origin_country"]),
|
||||
) ?? null,
|
||||
homepage: data.homepage || null,
|
||||
budget: tv ? null : data.budget || null,
|
||||
revenue: tv ? null : data.revenue || null,
|
||||
keywords,
|
||||
tmdb_videos: mapVideos(data.videos),
|
||||
watch_providers: mapWatchProviders(data["watch/providers"]),
|
||||
alternative_titles: alternativeTitles,
|
||||
|
||||
tv_type: tv ? (data.type ?? null) : null,
|
||||
created_by: tv
|
||||
? (data.created_by ?? []).map((p) =>
|
||||
pick(p, ["id", "name", "profile_path"]),
|
||||
)
|
||||
: null,
|
||||
number_of_seasons: tv ? (data.number_of_seasons ?? null) : null,
|
||||
number_of_episodes: tv ? (data.number_of_episodes ?? null) : null,
|
||||
last_air_date: tv ? (data.last_air_date ?? null) : null,
|
||||
in_production: tv ? (data.in_production ?? null) : null,
|
||||
next_episode_to_air: tv
|
||||
? mapEpisodeSummary(data.next_episode_to_air)
|
||||
: null,
|
||||
last_episode_to_air: tv
|
||||
? mapEpisodeSummary(data.last_episode_to_air)
|
||||
: null,
|
||||
|
||||
poster_path: data.poster_path,
|
||||
banner_path: data.backdrop_path,
|
||||
|
||||
last_fetch: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
/** EntryId table columns. TV details have no imdb_id; it's in external_ids. */
|
||||
function mapEntryIds(data) {
|
||||
const ext = data.external_ids ?? {};
|
||||
return {
|
||||
imdb_id: data.imdb_id || ext.imdb_id || null,
|
||||
tvdb_id: ext.tvdb_id != null ? String(ext.tvdb_id) : null,
|
||||
wikidata_id: ext.wikidata_id || null,
|
||||
facebook_id: ext.facebook_id || null,
|
||||
instagram_id: ext.instagram_id || null,
|
||||
twitter_id: ext.twitter_id || null,
|
||||
};
|
||||
}
|
||||
|
||||
/** EntryRating TMDB columns (user/imdb/rt ratings are left untouched). */
|
||||
function mapEntryRatings(data) {
|
||||
return {
|
||||
tmdb_rating: data.vote_average ?? null,
|
||||
tmdb_vote_count: data.vote_count ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Season table columns from a season details response. */
|
||||
function mapSeasonFields(seasonData) {
|
||||
return {
|
||||
tmdb_id: seasonData.id,
|
||||
tvdb_id:
|
||||
seasonData.external_ids?.tvdb_id != null
|
||||
? String(seasonData.external_ids.tvdb_id)
|
||||
: null,
|
||||
name: seasonData.name,
|
||||
overview: seasonData.overview,
|
||||
season_number: seasonData.season_number,
|
||||
episode_count: seasonData.episodes?.length ?? 0,
|
||||
air_date: seasonData.air_date,
|
||||
tmdb_rating: seasonData.vote_average,
|
||||
poster_path: seasonData.poster_path,
|
||||
};
|
||||
}
|
||||
|
||||
/** Episode table columns from an entry of seasonData.episodes. */
|
||||
function mapEpisodeFields(episode) {
|
||||
return {
|
||||
tmdb_id: episode.id,
|
||||
name: episode.name,
|
||||
overview: episode.overview,
|
||||
episode_number: episode.episode_number,
|
||||
episode_type: episode.episode_type,
|
||||
runtime: episode.runtime,
|
||||
air_date: episode.air_date,
|
||||
tmdb_rating: episode.vote_average,
|
||||
tmdb_vote_count: episode.vote_count ?? null,
|
||||
production_code: episode.production_code || null,
|
||||
crew: (episode.crew ?? [])
|
||||
.filter((c) => ["Directing", "Writing"].includes(c.department))
|
||||
.slice(0, MAX_EPISODE_CREW)
|
||||
.map((c) => pick(c, ["id", "name", "job", "department", "profile_path"])),
|
||||
guest_stars: (episode.guest_stars ?? [])
|
||||
.slice(0, MAX_GUEST_STARS)
|
||||
.map((g) =>
|
||||
pick(g, ["id", "name", "character", "profile_path", "order"]),
|
||||
),
|
||||
still_path: episode.still_path,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mapEntryFields,
|
||||
mapEntryIds,
|
||||
mapEntryRatings,
|
||||
mapSeasonFields,
|
||||
mapEpisodeFields,
|
||||
};
|
||||
Reference in new issue
Block a user