From 5eacc2d880eea18fddab37970a6a3504c91adb58 Mon Sep 17 00:00:00 2001 From: Cedric Date: Fri, 26 Jun 2026 16:11:31 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 145 +++++++++++++++++++++----------------------------------- 1 file changed, 53 insertions(+), 92 deletions(-) diff --git a/api.php b/api.php index 4e22acd..d0bdd15 100644 --- a/api.php +++ b/api.php @@ -100,97 +100,57 @@ function detectFormat($title, $desc = '') { return 'Blu-ray'; } -function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { +// ── FONCTION POUR RÉCUPÉRER LES AFFICHES DEPUIS TMDB ── +function fetchPosterTMDB($title, $year = '', $pdo = null) { $defaultPoster = 'assets/img/default_physical_media.jpg'; $cleanTitle = cleanTitle($title); - if (empty($cleanTitle)) return ['poster' => $defaultPoster, 'title' => '', 'format' => 'Blu-ray']; - - $baseUrl = 'https://www.cinemapassion.com'; - - $curlGet = function(string $url) use ($baseUrl): ?string { - $ch = curl_init($url); - curl_setopt_array($ch, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_MAXREDIRS => 5, - CURLOPT_TIMEOUT => 15, - CURLOPT_CONNECTTIMEOUT => 5, - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', - CURLOPT_REFERER => $baseUrl . '/', - CURLOPT_HTTPHEADER => ['Accept-Language: fr-FR,fr;q=0.8'], - ]); - $res = curl_exec($ch); - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - return ($code === 200 && is_string($res) && strlen($res) > 0) ? $res : null; - }; - - $extractImage = function(?string $html): ?string { - if (!$html) return null; - if (preg_match( - '/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', - $html, $m - )) { - $img = str_replace('http://', 'https://', $m[1]); - if (strpos($img, 'miniature') === false && strpos($img, 'vign') === false) { - return $img; - } - } - return null; - }; - - // ── ÉTAPE 1 : recherche ── - $searchHtml = $curlGet($baseUrl . '/recherche.php?recherche=' . urlencode($cleanTitle)); - - if (!$searchHtml) { - // Fallback POST - $ch = curl_init($baseUrl . '/moteur2.php'); - curl_setopt_array($ch, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_MAXREDIRS => 5, - CURLOPT_TIMEOUT => 15, - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => http_build_query(['recherche' => $cleanTitle]), - CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', - CURLOPT_REFERER => $baseUrl . '/', - ]); - $searchHtml = curl_exec($ch) ?: null; - curl_close($ch); + + if (empty($cleanTitle)) { + return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; } - if (!$searchHtml) return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; - - // ── ÉTAPE 2 : trouver et charger la fiche film ── - // Regex corrigée : gère /film/, ../film/, et URL absolue complète - $regexFilm = '/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com)?\/? (?:\.\./)?(film[\/-][^"\'\s>]+)["\']?/i'; - - $filmHtml = null; - if ($extractImage($searchHtml)) { - // Redirection directe sur la page jaquette - $filmHtml = $searchHtml; - } elseif (preg_match($regexFilm, $searchHtml, $m)) { - $filmHtml = $curlGet($baseUrl . '/' . ltrim($m[1], '/')); + + $tmdbKey = getTmdbApiKey($pdo); + if (!$tmdbKey) { + error_log("TMDB: ❌ Clé API non configurée"); + return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; } - - if (!$filmHtml) return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; - - // ── ÉTAPE 3 : page jaquette depuis la fiche film ── - // Regex corrigée : gère /jaquette-, ../jaquette-, et URL absolue complète - $regexJaq = '/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com)?\/? (?:\.\./)?(jaquette-(?:dvd|blu-ray)-[^"\'\s>]+)["\']?/i'; - - if (preg_match($regexJaq, $filmHtml, $m)) { - $jaqHtml = $curlGet($baseUrl . '/' . ltrim($m[1], '/')); - // curl suit automatiquement la redirection vers /dump/generation_page_covers3.php - $img = $extractImage($jaqHtml); - if ($img) return ['poster' => $img, 'title' => $cleanTitle, 'format' => detectFormat($cleanTitle)]; + + // ÉTAPE 1 : Recherche du film + $searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query=" . urlencode($cleanTitle); + if (!empty($year)) { + $searchUrl .= "&year={$year}"; } - - // ── Fallback : image directement sur la fiche film ── - $img = $extractImage($filmHtml); - if ($img) return ['poster' => $img, 'title' => $cleanTitle, 'format' => detectFormat($cleanTitle)]; - + $searchUrl .= "&language=fr-FR"; + + $searchRes = httpGet($searchUrl, 5); + $searchData = $searchRes ? json_decode($searchRes, true) : []; + + // Si pas de résultat avec l'année, on réessaie sans + if (empty($searchData['results']) && !empty($year)) { + $searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query=" . urlencode($cleanTitle) . "&language=fr-FR"; + $searchRes = httpGet($searchUrl, 5); + $searchData = $searchRes ? json_decode($searchRes, true) : []; + } + + if (empty($searchData['results'])) { + error_log("TMDB: ❌ Film non trouvé pour '{$cleanTitle}'"); + return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; + } + + // ÉTAPE 2 : Récupérer le poster du premier résultat + $posterPath = $searchData['results'][0]['poster_path'] ?? ''; + + if (!empty($posterPath)) { + $posterUrl = "https://image.tmdb.org/t/p/w500" . $posterPath; + error_log("TMDB: ✅ Affiche trouvée pour '{$cleanTitle}' → {$posterUrl}"); + return [ + 'poster' => $posterUrl, + 'title' => $cleanTitle, + 'format' => 'Blu-ray' + ]; + } + + error_log("TMDB: ❌ Film trouvé mais pas d'affiche pour '{$cleanTitle}'"); return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; } @@ -261,7 +221,7 @@ switch ($action) { ORDER BY id DESC "; $result = $pdo->query($sql)->fetchAll(); - foreach ($result as &$row) { + foreach ($result as $row) { if ($row['rating'] !== null) { $ratingVal = (float)$row['rating']; $row['rating'] = ($ratingVal == floor($ratingVal)) ? (int)$ratingVal : $ratingVal; @@ -291,10 +251,11 @@ switch ($action) { $stmt = $pdo->prepare($sql); $stmt->execute([$id, $data['title'] ?? '', $data['year'] ?? '', $data['director'] ?? '', $data['poster'] ?? '', $data['rating'] ?? 3.0, $data['review'] ?? '', $streaming]); } else { + // ✅ NOUVEAU : Utiliser TMDB if (empty($data['poster']) && !empty($data['title'])) { - $cpData = fetchCinemaPassion($data['title'], $data['year'] ?? '', $data['ean_isbn13'] ?? '', $pdo); - if (!empty($cpData['poster']) && $cpData['poster'] !== 'assets/img/default_physical_media.jpg') { - $data['poster'] = $cpData['poster']; + $tmdbData = fetchPosterTMDB($data['title'], $data['year'] ?? '', $pdo); + if (!empty($tmdbData['poster']) && $tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') { + $data['poster'] = $tmdbData['poster']; } } $sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=IF(VALUES(director)!='', VALUES(director), director), poster=IF(VALUES(poster)!='', VALUES(poster), poster), format=IF(VALUES(format)!='', VALUES(format), format), length=IF(VALUES(length)!='', VALUES(length), length), publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher), ean_isbn13=IF(VALUES(ean_isbn13)!='', VALUES(ean_isbn13), ean_isbn13), number_of_discs=IF(VALUES(number_of_discs)!=1, VALUES(number_of_discs), number_of_discs), aspect_ratio=IF(VALUES(aspect_ratio)!='', VALUES(aspect_ratio), aspect_ratio), description=IF(VALUES(description)!='', VALUES(description), description), actors=IF(VALUES(actors)!='', VALUES(actors), actors)"; @@ -361,8 +322,8 @@ switch ($action) { $id = makeStableId('videotheque', $title, $year); - $cpData = fetchCinemaPassion($title, $year, $ean, $pdo); - $poster = $cpData['poster']; + $tmdbData = fetchPosterTMDB($title, $year, $pdo); + $poster = $tmdbData['poster']; $format = detectFormat($title, $desc); $stmtVideo->execute([