From f7f46cd9062fc13c046e24e2ac7cbba090aec2e7 Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 23 Jul 2026 14:26:56 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/api.php b/api.php index 266a1ec..4aa1e6e 100644 --- a/api.php +++ b/api.php @@ -468,7 +468,6 @@ function fetchFromBlurayComByEan($ean) { return $empty; } -// ── FONCTION TMDB COMPLÈTE ── function fetchTmdbPosterAndSynopsis($title, $year = '', $pdo = null) { $default = ['poster'=>'assets/img/default_physical_media.jpg','title'=>'','description'=>'','director'=>'','actors'=>'','length'=>'','year'=>'']; if (empty($title)) return $default; @@ -476,17 +475,24 @@ function fetchTmdbPosterAndSynopsis($title, $year = '', $pdo = null) { $tmdbKey = getTmdbApiKey($pdo); if (!$tmdbKey) return $default; + // Nettoyage et encodage correct pour l'URL $clean = cleanTitle($title); - $searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query=" . urlencode($clean) . "&language=fr-FR"; - if ($year) $searchUrl .= "&year={$year}"; + $queryUrl = urlencode($clean); - $res = httpGet($searchUrl, 5); + $searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query={$queryUrl}&language=fr-FR"; + if (!empty($year)) { + $searchUrl .= "&year={$year}"; + } + + $res = httpGet($searchUrl, 8); $data = $res ? json_decode($res, true) : []; - if (empty($data['results']) && $year) { - $res = httpGet("https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query=" . urlencode($clean) . "&language=fr-FR", 5); + // Fallback : Si l'année exacte ne donne rien, on cherche le titre seul immédiatement + if (empty($data['results']) && !empty($year)) { + $res = httpGet("https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query={$queryUrl}&language=fr-FR", 8); $data = $res ? json_decode($res, true) : []; } + if (empty($data['results'])) return $default; $movie = $data['results'][0]; @@ -496,8 +502,9 @@ function fetchTmdbPosterAndSynopsis($title, $year = '', $pdo = null) { $default['description'] = $movie['overview'] ?? ''; $default['title'] = $clean; + // Deuxième requête pour les détails et le casting (réalisation) $detailsUrl = "https://api.themoviedb.org/3/movie/{$movieId}?api_key={$tmdbKey}&append_to_response=credits&language=fr-FR"; - $detRes = httpGet($detailsUrl, 5); + $detRes = httpGet($detailsUrl, 8); if ($detRes) { $det = json_decode($detRes, true); $default['length'] = !empty($det['runtime']) ? "{$det['runtime']} min" : ''; @@ -817,21 +824,36 @@ case 'import_batch': } else { $stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), poster=VALUES(poster), rating=VALUES(rating), review=VALUES(review), streaming=VALUES(streaming)"); foreach ($items as $rowData) { - $title = $rowData['Title'] ?? $rowData['title'] ?? ''; + // Supporte "Name" (Letterboxd standard) ou "Title" + $title = $rowData['Title'] ?? $rowData['title'] ?? $rowData['Name'] ?? ''; if (empty($title)) continue; - $year = !empty($rowData['publish_date']) ? substr($rowData['publish_date'], 0, 4) : ($rowData['Year'] ?? $rowData['year'] ?? ''); + + // PRIORITY FIX : On cherche la vraie année du film, on ignore publish_date pour la recherche TMDB + $year = $rowData['Year'] ?? $rowData['year'] ?? ''; + if (empty($year) && !empty($rowData['Release Date'])) { + $year = substr($rowData['Release Date'], 0, 4); + } + $id = makeStableId('critique', $title, $year); $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $review = $rowData['Review'] ?? $rowData['review'] ?? $rowData['description'] ?? ''; - $director = ''; $poster = 'assets/img/default_physical_media.jpg'; $streaming = 'Support physique / Cinéma'; + $director = ''; + $poster = 'assets/img/default_physical_media.jpg'; + $streaming = 'Support physique / Cinéma'; + + // Appel TMDB $tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo); if ($tmdbData) { if (!empty($tmdbData['director'])) $director = $tmdbData['director']; if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') $poster = $tmdbData['poster']; if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year']; } + $stmtCritiques->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]); + + // PAUSE ANTI-BLOCAGE : 250 millisecondes entre chaque film pour respecter le quota TMDB + usleep(250000); $imported++; } }