diff --git a/api.php b/api.php
index 0de4816..510a42f 100644
--- a/api.php
+++ b/api.php
@@ -137,7 +137,7 @@ function detectFormat($title, $desc = '') {
return 'Blu-ray';
}
-// ── FONCTION PRINCIPALE : CINEMAPASSION.COM ──
+// ── FONCTION POUR RÉCUPÉRER LES IMAGES DEPUIS CINEMAPASSION.COM ──
function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) {
$defaultPoster = 'assets/img/default_physical_media.jpg';
$cleanTitle = cleanTitle($title);
@@ -149,52 +149,93 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) {
// ÉTAPE 1 : Recherche POST sur /moteur2.php
$searchRes = httpPost('https://www.cinemapassion.com/moteur2.php', ['recherche' => $cleanTitle]);
- if ($searchRes) {
- // ÉTAPE 2 : Extraire le lien vers la page du film
- // Pattern : href='../film/Saw-3814.php' ou href='film/Saw-3814.php'
- if (preg_match('/href=["\']?(?:\.\.\/)?film\/([^"\'\s]+)-(\d+)\.php["\']?/i', $searchRes, $matches)) {
- $filmName = $matches[1];
- $filmId = $matches[2];
+ if (!$searchRes) {
+ error_log("CinemaPassion: Recherche échouée pour '{$cleanTitle}'");
+ return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
+ }
+
+ // ÉTAPE 2 : Trouver le lien qui correspond EXACTEMENT au titre
+ // Pattern : Saw
+ $filmUrl = null;
+ $filmName = null;
+ $filmId = null;
+
+ // Chercher tous les liens vers des films
+ if (preg_match_all('/]*>([^<]+)<\/a>/i', $searchRes, $matches, PREG_SET_ORDER)) {
+ foreach ($matches as $match) {
+ $linkTitle = trim($match[3]);
+ $linkName = $match[1];
+ $linkId = $match[2];
- // ÉTAPE 3 : Essayer plusieurs pages pour trouver l'image
- $pagesToTry = [
- "https://www.cinemapassion.com/jaquette-dvd-{$filmName}-{$filmId}.php",
- "https://www.cinemapassion.com/jaquette-blu-ray-{$filmName}-{$filmId}.php",
- "https://www.cinemapassion.com/film/{$filmName}-{$filmId}.php",
- "https://www.cinemapassion.com/sticker-dvd-{$filmName}-{$filmId}.php",
- ];
-
- foreach ($pagesToTry as $pageUrl) {
- $pageRes = httpGet($pageUrl, 10);
-
- if ($pageRes) {
- // Pattern : src='http://www.cinemapassion.com/covers_temp/covers3/Saw-13005811062007.jpg'
- if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s>]+\.jpg)["\']?/i', $pageRes, $imgMatches)) {
- $posterUrl = $imgMatches[1];
- $posterUrl = str_replace('http://', 'https://', $posterUrl);
- error_log("CinemaPassion OK: '{$cleanTitle}' → {$posterUrl}");
- return ['poster' => $posterUrl, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
- }
-
- // Fallback : image dans lesaffiches/
- if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/lesaffiches\/[^"\'\s>]+\.jpg)["\']?/i', $pageRes, $imgMatches)) {
- $posterUrl = $imgMatches[1];
- $posterUrl = str_replace('http://', 'https://', $posterUrl);
- error_log("CinemaPassion OK (affiche): '{$cleanTitle}' → {$posterUrl}");
- return ['poster' => $posterUrl, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
- }
- }
+ // Vérifier si le titre du lien correspond au titre recherché
+ if (strcasecmp($linkTitle, $cleanTitle) === 0 ||
+ stripos($linkTitle, $cleanTitle) !== false ||
+ stripos($cleanTitle, $linkTitle) !== false) {
+ $filmUrl = "https://www.cinemapassion.com/film/{$linkName}-{$linkId}.php";
+ $filmName = $linkName;
+ $filmId = $linkId;
+ break;
}
}
- // Fallback : chercher directement une image covers_temp dans les résultats
- if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s>]+\.jpg)["\']?/i', $searchRes, $imgMatches)) {
- $posterUrl = str_replace('http://', 'https://', $imgMatches[1]);
- error_log("CinemaPassion OK (direct): '{$cleanTitle}' → {$posterUrl}");
- return ['poster' => $posterUrl, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
+ // Si pas de correspondance exacte, prendre le premier résultat
+ if (!$filmUrl && !empty($matches)) {
+ $filmName = $matches[0][1];
+ $filmId = $matches[0][2];
+ $filmUrl = "https://www.cinemapassion.com/film/{$filmName}-{$filmId}.php";
}
}
+ if (!$filmUrl) {
+ error_log("CinemaPassion: Film non trouvé pour '{$cleanTitle}'");
+ return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
+ }
+
+ // ÉTAPE 3 : Aller sur la page du film
+ $filmPageRes = httpGet($filmUrl, 10);
+
+ if (!$filmPageRes) {
+ error_log("CinemaPassion: Page film inaccessible : {$filmUrl}");
+ return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
+ }
+
+ // ÉTAPE 4 : Chercher le lien vers la jaquette DVD ou Blu-ray
+ // Pattern : ou
+ $jaquetteUrl = null;
+
+ if (preg_match('/href=["\']?(?:\.\.\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s]+\.php)["\']?/i', $filmPageRes, $matches)) {
+ $jaquettePath = $matches[1];
+ $jaquetteUrl = "https://www.cinemapassion.com/" . str_replace('../', '', $jaquettePath);
+ }
+
+ // ÉTAPE 5 : Si page jaquette trouvée, aller dessus et extraire l'image covers_temp
+ if ($jaquetteUrl) {
+ $jaquettePageRes = httpGet($jaquetteUrl, 10);
+
+ if ($jaquettePageRes) {
+ // Pattern : src='http://www.cinemapassion.com/covers_temp/covers3/Saw-13005811062007.jpg'
+ if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s>]+\.jpg)["\']?/i', $jaquettePageRes, $imgMatches)) {
+ $posterUrl = str_replace('http://', 'https://', $imgMatches[1]);
+ error_log("CinemaPassion OK (jaquette): '{$cleanTitle}' → {$posterUrl}");
+ return ['poster' => $posterUrl, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
+ }
+ }
+ }
+
+ // ÉTAPE 6 : Fallback - Chercher l'image covers_temp directement sur la page du film
+ if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s>]+\.jpg)["\']?/i', $filmPageRes, $imgMatches)) {
+ $posterUrl = str_replace('http://', 'https://', $imgMatches[1]);
+ error_log("CinemaPassion OK (covers_temp direct): '{$cleanTitle}' → {$posterUrl}");
+ return ['poster' => $posterUrl, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
+ }
+
+ // ÉTAPE 7 : Fallback - Utiliser l'affiche trouvée sur la page du film
+ if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/lesaffiches\/[^"\'\s>]+\.jpg)["\']?/i', $filmPageRes, $imgMatches)) {
+ $posterUrl = str_replace('http://', 'https://', $imgMatches[1]);
+ error_log("CinemaPassion OK (affiche fallback): '{$cleanTitle}' → {$posterUrl}");
+ return ['poster' => $posterUrl, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
+ }
+
error_log("CinemaPassion KO: Image NON trouvée pour '{$cleanTitle}'");
return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
}