diff --git a/api.php b/api.php index b31e9f0..d225b79 100644 --- a/api.php +++ b/api.php @@ -158,11 +158,9 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { if ($html) { // ── ÉTAPE 2 : Extraire le lien vers la page du film ── - // Pattern : href='../film/Saw-3814.php' if (preg_match('/href=["\']([^"\']*film\/[^"\'\/]+-\d+\.php)["\']/i', $html, $matches)) { $filmPath = $matches[1]; - // Normaliser l'URL $filmPath = str_replace('../', '', $filmPath); if (strpos($filmPath, '/') !== 0) { $filmUrl = 'https://www.cinemapassion.com/' . $filmPath; @@ -170,8 +168,6 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { $filmUrl = 'https://www.cinemapassion.com' . $filmPath; } - // Extraire le nom-slug et l'ID numérique - // Ex: film/Saw-3814.php → filmName=Saw, filmId=3814 if (preg_match('/film\/([^\/]+)-(\d+)\.php/i', $filmUrl, $filmMatches)) { $filmName = $filmMatches[1]; $filmId = $filmMatches[2]; @@ -188,13 +184,9 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { $pageRes = httpGet($pageUrl, 10, $userAgent); if ($pageRes) { - // Pattern : src='http://www.cinemapassion.com/covers_temp/covers3/Saw-13005811062007.jpg' - $imgPattern = '/src=["\']?(https?:\/\/www\.cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s>]+\.jpg)["\']?/i'; - - if (preg_match($imgPattern, $pageRes, $imgMatches)) { + // Pattern plus flexible pour capturer l'image + if (preg_match('/src=["\']?(https?:\/\/[^"\'\s>]*cinemapassion\.com[^"\'\s>]*covers[^"\'\s>]*\.jpg)["\']?/i', $pageRes, $imgMatches)) { $posterUrl = $imgMatches[1]; - - // Forcer HTTPS $posterUrl = str_replace('http://', 'https://', $posterUrl); error_log("CinemaPassion: Image trouvée pour '{$cleanTitle}' → {$posterUrl}"); @@ -211,8 +203,7 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { } // ── FALLBACK : Chercher directement une image dans les résultats ── - $imgPattern = '/src=["\']?(https?:\/\/www\.cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s>]+\.jpg)["\']?/i'; - if (preg_match($imgPattern, $html, $imgMatches)) { + if (preg_match('/src=["\']?(https?:\/\/[^"\'\s>]*cinemapassion\.com[^"\'\s>]*covers[^"\'\s>]*\.jpg)["\']?/i', $html, $imgMatches)) { $posterUrl = str_replace('http://', 'https://', $imgMatches[1]); error_log("CinemaPassion: Image trouvée (fallback) pour '{$cleanTitle}' → {$posterUrl}"); return [ @@ -614,14 +605,158 @@ case 'get_config_keys': echo json_encode(["success" => true, "imported" => $imported]); break; - case 'debug_cinemapassion': - $title = $_GET['title'] ?? ''; - $year = $_GET['year'] ?? ''; - $ean = $_GET['ean'] ?? ''; +// ── ENDPOINT DE DEBUG DÉTAILLÉ ── +case 'debug_cinemapassion_detailed': + $title = $_GET['title'] ?? 'Saw'; + $year = $_GET['year'] ?? ''; + $ean = $_GET['ean'] ?? ''; + + $debugInfo = [ + 'title' => $title, + 'year' => $year, + 'ean' => $ean, + 'cleanTitle' => cleanTitle($title), + 'steps' => [] + ]; + + $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'; + + // ÉTAPE 1 : Test de la recherche POST + $searchUrl = "https://www.cinemapassion.com/moteur2.php"; + $ch = curl_init($searchUrl); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 15, + CURLOPT_CONNECTTIMEOUT => 5, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_USERAGENT => $userAgent, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query(['recherche' => cleanTitle($title)]), + CURLOPT_REFERER => 'https://www.cinemapassion.com/', + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/x-www-form-urlencoded', + 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'Accept-Language: fr-FR,fr;q=0.8', + 'Origin: https://www.cinemapassion.com' + ], + ]); + $searchRes = curl_exec($ch); + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $debugInfo['steps']['search'] = [ + 'url' => $searchUrl, + 'http_code' => $code, + 'response_length' => strlen($searchRes), + 'success' => ($code === 200 && !empty($searchRes)) + ]; + + if ($code === 200 && $searchRes) { + // ÉTAPE 2 : Extraction du lien film + if (preg_match('/href=["\']([^"\']*film\/[^"\'\/]+-\d+\.php)["\']/i', $searchRes, $matches)) { + $filmPath = $matches[1]; + $filmPath = str_replace('../', '', $filmPath); + $filmUrl = 'https://www.cinemapassion.com/' . $filmPath; + + $debugInfo['steps']['film_link'] = [ + 'found' => true, + 'filmPath' => $filmPath, + 'filmUrl' => $filmUrl + ]; + + // ÉTAPE 3 : Extraction du nom et ID + if (preg_match('/film\/([^\/]+)-(\d+)\.php/i', $filmUrl, $filmMatches)) { + $filmName = $filmMatches[1]; + $filmId = $filmMatches[2]; + + $debugInfo['steps']['film_details'] = [ + 'filmName' => $filmName, + 'filmId' => $filmId + ]; + + // ÉTAPE 4 : Test des pages de jaquettes + $pagesToTry = [ + "https://www.cinemapassion.com/jaquette-dvd-{$filmName}-{$filmId}.php", + "https://www.cinemapassion.com/jaquette-blu-ray-{$filmName}-{$filmId}.php", + $filmUrl, + "https://www.cinemapassion.com/sticker-dvd-{$filmName}-{$filmId}.php", + ]; + + foreach ($pagesToTry as $index => $pageUrl) { + $pageRes = httpGet($pageUrl, 10, $userAgent); + + $pageDebug = [ + 'url' => $pageUrl, + 'success' => !empty($pageRes), + 'response_length' => strlen($pageRes) + ]; + + if ($pageRes) { + // Test du pattern d'image + if (preg_match('/src=["\']?(https?:\/\/[^"\'\s>]*cinemapassion\.com[^"\'\s>]*covers[^"\'\s>]*\.jpg)["\']?/i', $pageRes, $imgMatches)) { + $posterUrl = str_replace('http://', 'https://', $imgMatches[1]); + $pageDebug['image_found'] = true; + $pageDebug['image_url'] = $posterUrl; + + $debugInfo['steps']['pages_tested'][$index] = $pageDebug; + $debugInfo['final_result'] = [ + 'success' => true, + 'poster' => $posterUrl, + 'title' => cleanTitle($title), + 'format' => 'Blu-ray' + ]; + + echo json_encode($debugInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + exit; + } else { + $pageDebug['image_found'] = false; + // Extraire un extrait du HTML pour debug + $pageDebug['html_snippet'] = substr($pageRes, 0, 500); + } + } + + $debugInfo['steps']['pages_tested'][$index] = $pageDebug; + } + } + } else { + $debugInfo['steps']['film_link'] = [ + 'found' => false, + 'html_snippet' => substr($searchRes, 0, 1000) + ]; + } - $result = fetchCinemaPassion($title, $year, $ean, $pdo); - echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); - exit; - break; + // Test du fallback + if (preg_match('/src=["\']?(https?:\/\/[^"\'\s>]*cinemapassion\.com[^"\'\s>]*covers[^"\'\s>]*\.jpg)["\']?/i', $searchRes, $imgMatches)) { + $posterUrl = str_replace('http://', 'https://', $imgMatches[1]); + $debugInfo['steps']['fallback'] = [ + 'success' => true, + 'image_url' => $posterUrl + ]; + $debugInfo['final_result'] = [ + 'success' => true, + 'poster' => $posterUrl, + 'title' => cleanTitle($title), + 'format' => 'Blu-ray' + ]; + } else { + $debugInfo['steps']['fallback'] = [ + 'success' => false + ]; + } + } + + if (!isset($debugInfo['final_result'])) { + $debugInfo['final_result'] = [ + 'success' => false, + 'poster' => 'assets/img/default_physical_media.jpg', + 'title' => cleanTitle($title), + 'format' => 'Blu-ray' + ]; + } + + echo json_encode($debugInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + exit; + break; } ?> \ No newline at end of file