diff --git a/api.php b/api.php index 53b010c..147a167 100644 --- a/api.php +++ b/api.php @@ -120,7 +120,7 @@ function extractYear($dateStr) { return ''; } -// ── FONCTION POUR RÉCUPÉRER LES IMAGES DEPUIS CINEMAPASSION.COM ── +// ── FONCTION DE RÉCUPÉRATION (Mise à jour TMDB / OMDb) ── function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { $defaultPoster = 'assets/img/default_physical_media.jpg'; @@ -129,87 +129,42 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { } $cleanTitle = cleanTitle($title); - $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'; - - // ÉTAPE 1 : Recherche sur cinemapassion.com - $searchUrl = "https://www.cinemapassion.com/recherche.php?recherche=" . urlencode($cleanTitle); - - $searchRes = httpGet($searchUrl, 10, $userAgent); - - if ($searchRes) { - // Extraire le lien vers la page du film - // Pattern typique : ou similaire - if (preg_match('/]*>' . preg_quote($cleanTitle, '/') . '/i', $searchRes, $matches)) { - $filmUrl = $matches[1]; - if (strpos($filmUrl, 'http') !== 0) { - $filmUrl = 'https://www.cinemapassion.com' . (strpos($filmUrl, '/') === 0 ? '' : '/') . $filmUrl; - } - - $filmRes = httpGet($filmUrl, 10, $userAgent); - - if ($filmRes) { - $posterUrl = null; - - // Méthode 1 : Chercher l'image d'affiche principale - if (preg_match('/]*class=[\'"](?:affiche|poster|movie-poster)[^"]*"[^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/i', $filmRes, $imgMatches)) { - $posterUrl = $imgMatches[1]; - } - // Méthode 2 : Chercher dans une div dédiée - elseif (preg_match('/]*class=[\'"](?:affiche|poster|movie-poster)[^"]*"[^>]*>.*?]*src=[\'"]([^\'"]+)[\'"][^>]*>/is', $filmRes, $imgMatches)) { - $posterUrl = $imgMatches[1]; - } - // Méthode 3 : Chercher toute image de film - elseif (preg_match('/]*src=[\'"](https?:\/\/www\.cinemapassion\.com\/[^\'"]*\/?(?:affiches|posters|images|films)[^\'"]*\.(?:jpg|jpeg|png|webp))[^>]*>/i', $filmRes, $imgMatches)) { - $posterUrl = $imgMatches[1]; - } - - if (!empty($posterUrl)) { - // S'assurer que l'URL est absolue - if (strpos($posterUrl, 'http') !== 0) { - $posterUrl = 'https://www.cinemapassion.com' . (strpos($posterUrl, '/') === 0 ? '' : '/') . $posterUrl; - } - - return [ - 'poster' => $posterUrl, - 'title' => $cleanTitle, - 'format' => 'Blu-ray' - ]; - } - } + $posterUrl = null; + + // 1. Recherche ultra-rapide via TMDB (Déjà configuré et prioritaire) + $tmdbKey = getTmdbApiKey($pdo); + if ($tmdbKey) { + $tmdbData = fetchTMDBFull($cleanTitle, $year, $tmdbKey, $pdo); + if ($tmdbData && !empty($tmdbData['poster'])) { + $posterUrl = $tmdbData['poster']; } + } + + // 2. Fallback sur OMDb (si l'API est configurée en BDD) + if (empty($posterUrl)) { + $stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = 'omdb_api_key'"); + $stmt->execute(); + $row = $stmt->fetch(); + $omdbKey = $row ? decryptData($row['key_value']) : null; - // ÉTAPE 2 : Recherche alternative par EAN si disponible - if (!empty($ean)) { - $cleanEan = preg_replace('/[^0-9]/', '', $ean); - $searchUrl = "https://www.cinemapassion.com/recherche.php?recherche=" . urlencode($cleanEan); + if ($omdbKey) { + $omdbUrl = "http://www.omdbapi.com/?apikey=" . urlencode($omdbKey) . "&t=" . urlencode($cleanTitle); + if (!empty($year)) $omdbUrl .= "&y=" . urlencode($year); - $searchRes = httpGet($searchUrl, 10, $userAgent); + $omdbRes = httpGet($omdbUrl, 5); + $omdbData = $omdbRes ? json_decode($omdbRes, true) : null; - if ($searchRes && preg_match('/]*>/i', $searchRes, $matches)) { - $filmUrl = $matches[1]; - if (strpos($filmUrl, 'http') !== 0) { - $filmUrl = 'https://www.cinemapassion.com' . (strpos($filmUrl, '/') === 0 ? '' : '/') . $filmUrl; - } - - $filmRes = httpGet($filmUrl, 10, $userAgent); - - if ($filmRes && preg_match('/]*src=[\'"](https?:\/\/www\.cinemapassion\.com\/[^\'"]*\/?(?:affiches|posters|images|films)[^\'"]*\.(?:jpg|jpeg|png|webp))[^>]*>/i', $filmRes, $imgMatches)) { - $posterUrl = $imgMatches[1]; - if (strpos($posterUrl, 'http') !== 0) { - $posterUrl = 'https://www.cinemapassion.com' . (strpos($posterUrl, '/') === 0 ? '' : '/') . $posterUrl; - } - - return [ - 'poster' => $posterUrl, - 'title' => $cleanTitle, - 'format' => 'Blu-ray' - ]; - } + if ($omdbData && isset($omdbData['Poster']) && $omdbData['Poster'] !== 'N/A') { + $posterUrl = $omdbData['Poster']; } } } - - return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; + + return [ + 'poster' => $posterUrl ?: $defaultPoster, + 'title' => $cleanTitle, + 'format' => 'Blu-ray' // Le format réel est affiné par detectFormat() plus tard + ]; } // ── ROUTEUR PRINCIPAL ──