Répartition des notes sur les fiches de films et séries (Trakt)
deploy / deploy (push) Successful in 42s
deploy / deploy (push) Successful in 42s
- Fonction serveur « ratings » : moyenne, votes et répartition 1→10 de Trakt à partir de l'identifiant TMDB (clé TRAKT_CLIENT_ID côté serveur) - Fiche : la note Trakt remplace celle de TMDB quand il y a au moins 20 votes, avec le graphique comme pour les animes ; sinon rien ne change Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
This commit is contained in:
5 files changed
+130
-5
No files matched your search
@@ -327,7 +327,7 @@ export default function TitleSheet({ item: initial, seed, onClose, onOpenPerson,
|
||||
|
||||
const heroImage = backdrop ?? extras?.backdrop
|
||||
const average = extras?.average ?? data.publicRating
|
||||
const source = externalId?.startsWith('anilist:') ? 'AniList' : externalId ? 'TMDB' : undefined
|
||||
const source = externalId?.startsWith('anilist:') ? 'AniList' : externalId ? (extras?.ratingSource ?? 'TMDB') : undefined
|
||||
const runtime = type === 'film' ? (data.duration ?? extras?.runtime) : undefined
|
||||
const metaLine = [
|
||||
data.year,
|
||||
|
||||
+16
-4
@@ -1,6 +1,6 @@
|
||||
import type { MediaInput, MediaType } from '../types'
|
||||
import { region, t, tmdbLanguage } from '../i18n'
|
||||
import { tmdbViaCloud } from './cloud/api'
|
||||
import { ratingsViaCloud, tmdbViaCloud } from './cloud/api'
|
||||
import { TYPE_BY_VALUE } from './constants'
|
||||
import { subtypeLabel, tmdbGenres } from './genres'
|
||||
import { franchiseKey } from './franchise'
|
||||
@@ -998,8 +998,10 @@ export interface TitleExtras {
|
||||
/** Note moyenne du public, sur 10 */
|
||||
average?: number
|
||||
votes?: number
|
||||
/** Répartition des notes du public (AniList) : note sur 10 → nombre de votes */
|
||||
/** Répartition des notes du public (AniList, ou Trakt pour les films et séries) : note sur 10 → nombre de votes */
|
||||
distribution?: { score: number; amount: number }[]
|
||||
/** D'où vient la note moyenne (« Trakt » quand elle remplace celle de TMDB) */
|
||||
ratingSource?: string
|
||||
cast: CastMember[]
|
||||
/** Réalisation (films) ou création (séries) */
|
||||
directors: string[]
|
||||
@@ -1037,6 +1039,8 @@ async function tmdbExtras(externalId: string, key: string): Promise<TitleExtras>
|
||||
const [, kind, id] = externalId.split(':') as ['tmdb', 'movie' | 'tv', string]
|
||||
// Une seule requête pour le casting et « Où regarder ». Si le proxy n'accepte pas encore la combinaison
|
||||
// (fonction serveur pas redéployée), on retombe sur le casting seul.
|
||||
// Répartition des notes : TMDB ne la donne pas, on la demande à Trakt (via notre serveur), en parallèle
|
||||
const trakt = key.trim() === CLOUD_TMDB ? ratingsViaCloud(kind, id).catch(() => null) : Promise.resolve(null)
|
||||
const d = await tmdbFetch<TmdbExtras>(`/${kind}/${id}`, key, { append_to_response: 'credits,watch/providers' }).catch((e: unknown) => {
|
||||
// Seul un refus du paramètre (400) justifie le second essai : hors ligne, 429, 5xx… on ne double pas la requête
|
||||
if (key.trim() !== CLOUD_TMDB || !(e instanceof Error) || e.message !== t('err.tmdbStatus', { status: 400 })) throw e
|
||||
@@ -1055,10 +1059,18 @@ async function tmdbExtras(externalId: string, key: string): Promise<TitleExtras>
|
||||
.filter((c): c is CastMember => !!c.name)
|
||||
const crew = Array.isArray(d.credits?.crew) ? d.credits!.crew! : []
|
||||
const votes = count(d.vote_count, 100_000_000)
|
||||
// Trakt : moyenne, votes et répartition ensemble (même public), dès qu'il y a assez de votes
|
||||
const r = await trakt
|
||||
const useTrakt = !!r && r.rating != null && r.votes >= 20
|
||||
const distribution = useTrakt
|
||||
? Object.entries(r!.distribution).map(([score, amount]) => ({ score: Number(score), amount })).sort((a, b) => a.score - b.score)
|
||||
: undefined
|
||||
return {
|
||||
backdrop: isSafeTmdbPath(d.backdrop_path) ? `${TMDB_IMG}/w780${d.backdrop_path}` : undefined,
|
||||
average: tmdbPublicRating(d.vote_average, d.vote_count),
|
||||
votes,
|
||||
average: useTrakt ? Math.round(r!.rating! * 10) / 10 : tmdbPublicRating(d.vote_average, d.vote_count),
|
||||
votes: useTrakt ? r!.votes : votes,
|
||||
distribution,
|
||||
ratingSource: useTrakt ? 'Trakt' : undefined,
|
||||
cast,
|
||||
directors:
|
||||
kind === 'movie'
|
||||
|
||||
@@ -47,3 +47,25 @@ export async function tmdbViaCloud<T>(path: string, params: Record<string, strin
|
||||
if (!res.ok) throw new Error(t('err.tmdbStatus', { status: res.status }))
|
||||
return res.json() as Promise<T>
|
||||
}
|
||||
|
||||
/** Notes du public d'un film ou d'une série (Trakt, via notre serveur) : moyenne sur 10, votes, répartition 1→10. */
|
||||
export async function ratingsViaCloud(kind: 'movie' | 'tv', id: string): Promise<{ rating?: number; votes: number; distribution: Record<string, number> } | null> {
|
||||
if (!/^\d{1,10}$/.test(id)) return null
|
||||
const q = new URLSearchParams({ kind, id })
|
||||
const res = (await getAccessToken())
|
||||
? await cloudFetch(`/functions/v1/ratings?${q}`)
|
||||
: await timedFetch(`${SUPABASE_URL}/functions/v1/ratings?${q}`, { headers: { apikey: SUPABASE_ANON_KEY }, credentials: 'omit', referrerPolicy: 'no-referrer' })
|
||||
if (!res.ok) return null
|
||||
const d = (await res.json().catch(() => null)) as { found?: boolean; rating?: unknown; votes?: unknown; distribution?: unknown } | null
|
||||
if (!d?.found || typeof d.distribution !== 'object' || !d.distribution) return null
|
||||
const distribution: Record<string, number> = {}
|
||||
for (let s = 1; s <= 10; s++) {
|
||||
const n = (d.distribution as Record<string, unknown>)[String(s)]
|
||||
distribution[String(s)] = typeof n === 'number' && Number.isFinite(n) && n >= 0 ? Math.round(n) : 0
|
||||
}
|
||||
return {
|
||||
rating: typeof d.rating === 'number' && d.rating > 0 && d.rating <= 10 ? d.rating : undefined,
|
||||
votes: typeof d.votes === 'number' && d.votes >= 0 ? Math.round(d.votes) : 0,
|
||||
distribution,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user