Deco
deploy / deploy (push) Successful in 32s

This commit is contained in:
Romain committed 2026-10-04 12:34:08 +02:00
1 parent 080279e456
commit b15207aa88
22 files changed
+800 -222

No files matched your search

+8 -4
View File
@@ -3,7 +3,7 @@ import type { MediaItem } from '../types'
import { CLOUD_TMDB, isPlausibleTmdbKey } from './catalogApi'
import { tmdbViaCloud } from './cloud/api'
import { TYPE_BY_VALUE } from './constants'
import { isPlainObject, isSafeExternalId, isSafeId, readStorage, safeDay, safeIso, safeSeasons } from './security'
import { isPlainObject, isSafeExternalId, isSafeId, isSafeTmdbPath, readStorage, remoteImage, safeDay, safeIso, safeSeasons } from './security'
/**
* Suivi des nouveaux épisodes.
@@ -21,6 +21,8 @@ export interface AiringInfo {
seasons?: number
/** Épisodes de chaque saison (TMDB) */
seasonSizes?: number[]
/** Grande image (TMDB) */
backdrop?: string
checkedAt: string
}
@@ -30,7 +32,7 @@ const KEY = 'azuucine:airing'
const RECHECK_MS = 12 * 3600 * 1000
const RECHECK_ENDED_MS = 30 * 24 * 3600 * 1000
/** Date d'arrivée du suivi par saison (les vérifications plus anciennes n'ont pas le découpage) */
const SEASONS_SINCE = '2026-10-04T00:00:00.000Z'
const SEASONS_SINCE = '2026-10-04T10:00:00.000Z'
const n0 = (v: unknown, max = 100000) => (typeof v === 'number' && Number.isFinite(v) && v >= 0 ? Math.min(max, Math.round(v)) : 0)
@@ -44,7 +46,7 @@ export function loadAiring(): AiringCache {
const checkedAt = safeIso(v.checkedAt)
if (!checkedAt) continue
const next = isPlainObject(v.next) && safeDay(v.next.date) ? { episode: n0(v.next.episode), season: n0(v.next.season) || undefined, date: v.next.date as string } : undefined
out[id] = { aired: n0(v.aired), ended: v.ended === true, seasons: n0(v.seasons) || undefined, seasonSizes: safeSeasons(v.seasonSizes), next, checkedAt }
out[id] = { aired: n0(v.aired), ended: v.ended === true, seasons: n0(v.seasons) || undefined, seasonSizes: safeSeasons(v.seasonSizes), backdrop: remoteImage(v.backdrop), next, checkedAt }
}
return out
}
@@ -76,7 +78,7 @@ export function needsCheck(item: MediaItem, cache: AiringCache, force = false):
const info = cache[item.id]
if (!info || force) return true
// Fiche TMDB vérifiée avant le suivi par saison : on récupère le découpage une fois
if (item.externalId?.startsWith('tmdb:tv:') && !info.seasonSizes && (info.seasons ?? 0) > 1 && info.checkedAt < SEASONS_SINCE) return true
if (item.externalId?.startsWith('tmdb:tv:') && (!info.backdrop || (!info.seasonSizes && (info.seasons ?? 0) > 1)) && info.checkedAt < SEASONS_SINCE) return true
const age = Date.now() - new Date(info.checkedAt).getTime()
return age > (info.ended ? RECHECK_ENDED_MS : RECHECK_MS)
}
@@ -90,6 +92,7 @@ interface TmdbTv {
last_episode_to_air?: { season_number: number; episode_number: number } | null
next_episode_to_air?: { season_number: number; episode_number: number; air_date?: string } | null
seasons?: { season_number: number; episode_count: number }[]
backdrop_path?: string | null
}
async function checkTmdb(tvId: string, key: string): Promise<Omit<AiringInfo, 'checkedAt'>> {
@@ -128,6 +131,7 @@ async function checkTmdb(tvId: string, key: string): Promise<Omit<AiringInfo, 'c
.sort((a, b) => a.season_number - b.season_number)
.map((s) => n0(s.episode_count)),
),
backdrop: isSafeTmdbPath(d.backdrop_path) ? `https://image.tmdb.org/t/p/w780${d.backdrop_path}` : undefined,
ended: d.status === 'Ended' || d.status === 'Canceled',
next: nx && nextDate ? { episode: n0(nx.episode_number), season: n0(nx.season_number) || undefined, date: nextDate } : undefined,
}
+2
View File
@@ -12,6 +12,7 @@ import {
safeCountries,
safeDay,
safeIso,
remoteImage,
safePosterUrl,
safeSeasons,
safeStringList,
@@ -104,6 +105,7 @@ export function normalizeItem(raw: unknown): MediaItem | null {
notes: cleanText(r.notes, LIMITS.notes),
notesPublic: r.notesPublic === true || undefined,
poster: safePosterUrl(r.poster),
backdrop: remoteImage(r.backdrop),
overview: cleanText(r.overview, LIMITS.overview),
externalId: isSafeExternalId(r.externalId) ? r.externalId : undefined,
top: normalizeTop(r.top),
+51 -7
View File
@@ -4,7 +4,7 @@ import { tmdbViaCloud } from './cloud/api'
import { TYPE_BY_VALUE } from './constants'
import { subtypeLabel, tmdbGenres } from './genres'
import { franchiseKey } from './franchise'
import { cleanText, isSafeExternalId, isSafeTmdbPath, LIMITS, safeCountries, safePosterUrl, safeSeasons, safeStringList } from './security'
import { cleanText, isSafeExternalId, isSafeTmdbPath, LIMITS, remoteImage, safeCountries, safeDay, safePosterUrl, safeSeasons, safeStringList } from './security'
/**
* Recherche dans les bases publiques :
@@ -58,6 +58,7 @@ export function sanitizeMeta(d: Partial<MediaInput>): Partial<MediaInput> {
externalId: isSafeExternalId(d.externalId) ? d.externalId : undefined,
countries: safeCountries(d.countries),
poster: safePosterUrl(d.poster),
backdrop: remoteImage(d.backdrop),
publicRating:
typeof d.publicRating === 'number' && d.publicRating > 0 && d.publicRating <= 10 ? Math.round(d.publicRating * 10) / 10 : undefined,
}
@@ -212,6 +213,7 @@ interface TmdbDetails {
release_date?: string
first_air_date?: string
poster_path?: string | null
backdrop_path?: string | null
last_episode_to_air?: { runtime?: number } | null
seasons?: { season_number?: number; episode_count?: number }[]
vote_average?: number
@@ -287,6 +289,7 @@ export async function getTmdbDetails(result: SearchResult, key: string): Promise
duration: kind === 'movie' ? d.runtime || undefined : undefined,
episodesTotal: kind === 'tv' ? Math.max(d.number_of_episodes || 0, sumSeasons(tmdbSeasons(d))) || undefined : undefined,
seasons: kind === 'tv' ? tmdbSeasons(d) : undefined,
backdrop: tmdbBackdrop(d.backdrop_path),
episodeDuration: kind === 'tv' ? epRuntime : undefined,
externalId: result.externalId,
countries: d.origin_country?.length ? d.origin_country : d.production_countries?.map((c) => c.iso_3166_1).slice(0, 3),
@@ -304,12 +307,53 @@ function tmdbSeasons(d: TmdbDetails): number[] | undefined {
}
const sumSeasons = (s?: number[]) => (s ?? []).reduce((a, b) => a + b, 0)
/** Saisons d'une série déjà dans la bibliothèque (pour les fiches ajoutées avant le suivi par saison). */
export async function getTmdbSeasons(externalId: string, key: string): Promise<{ seasons?: number[]; total?: number }> {
if (!isSafeExternalId(externalId) || !externalId.startsWith('tmdb:tv:')) return {}
const d = await tmdbFetch<TmdbDetails>(`/tv/${externalId.split(':')[2]}`, key)
const seasons = tmdbSeasons(d)
return { seasons, total: Math.max(d.number_of_episodes || 0, sumSeasons(seasons)) || undefined }
const tmdbBackdrop = (path?: string | null) => (isSafeTmdbPath(path) ? `${TMDB_IMG}/w780${path}` : undefined)
/** Grande image et saisons d'un titre déjà dans la bibliothèque (fiches ajoutées avant ces nouveautés). */
export async function getTmdbExtras(externalId: string, key: string): Promise<{ seasons?: number[]; total?: number; backdrop?: string }> {
if (!isSafeExternalId(externalId) || !externalId.startsWith('tmdb:')) return {}
const [, kind, id] = externalId.split(':') as ['tmdb', 'movie' | 'tv', string]
const d = await tmdbFetch<TmdbDetails>(`/${kind}/${id}`, key)
const seasons = kind === 'tv' ? tmdbSeasons(d) : undefined
return {
seasons,
total: kind === 'tv' ? Math.max(d.number_of_episodes || 0, sumSeasons(seasons)) || undefined : undefined,
backdrop: tmdbBackdrop(d.backdrop_path),
}
}
export interface EpisodeInfo {
number: number
name?: string
still?: string
date?: string
runtime?: number
}
const seasonCache = new Map<string, EpisodeInfo[]>()
/** Épisodes d'une saison (titre, image, date de diffusion). Gardés en mémoire le temps de la session. */
export async function getTmdbSeasonEpisodes(externalId: string, season: number, key: string): Promise<EpisodeInfo[]> {
if (!isSafeExternalId(externalId) || !externalId.startsWith('tmdb:tv:') || !Number.isInteger(season) || season < 1 || season > 999) return []
const cacheKey = `${externalId}:${season}:${tmdbLanguage()}`
const cached = seasonCache.get(cacheKey)
if (cached) return cached
const d = await tmdbFetch<{ episodes?: { episode_number?: number; name?: string; still_path?: string | null; air_date?: string; runtime?: number }[] }>(
`/tv/${externalId.split(':')[2]}/season/${season}`,
key,
)
const list = (Array.isArray(d.episodes) ? d.episodes : [])
.filter((e) => Number.isInteger(e.episode_number) && e.episode_number! > 0)
.slice(0, 1000)
.map((e) => ({
number: e.episode_number!,
name: cleanText(e.name, LIMITS.title),
still: isSafeTmdbPath(e.still_path) ? `${TMDB_IMG}/w300${e.still_path}` : undefined,
date: safeDay(e.air_date),
runtime: typeof e.runtime === 'number' && e.runtime > 0 && e.runtime < 1000 ? Math.round(e.runtime) : undefined,
}))
seasonCache.set(cacheKey, list)
return list
}
/** Vérifie qu'une clé TMDB fonctionne. */
+6
View File
@@ -76,3 +76,9 @@ export function seasonPosition(item: Pick<MediaItem, 'episodesWatched' | 'season
/** Nombre d'épisodes vus au début de la saison n (1 = première). */
export const episodesBeforeSeason = (seasons: number[], n: number) => seasons.slice(0, Math.max(0, n - 1)).reduce((a, b) => a + b, 0)
/** Nombre maximum d'épisodes : toutes les saisons connues, sinon le total enregistré. */
export function episodeCap(item: Pick<MediaItem, 'episodesTotal' | 'seasons'>): number | undefined {
const sum = item.seasons?.reduce((a, b) => a + b, 0) ?? 0
return Math.max(sum, item.episodesTotal ?? 0) || undefined
}
+6
View File
@@ -132,3 +132,9 @@ export function safeSeasons(v: unknown): number[] | undefined {
const out = v.slice(0, 100).map((n) => (typeof n === 'number' && Number.isInteger(n) && n > 0 && n <= 5000 ? n : 0))
return out.length > 1 && out.every((n) => n > 0) ? out : undefined
}
/** Image distante autorisée (jamais d'image intégrée : la grande image n'est qu'un décor). */
export function remoteImage(value: unknown): string | undefined {
const url = safePosterUrl(value)
return url && !url.startsWith('data:') && url.length <= 300 ? url : undefined
}