From 85d2c06d6f9e5be44b06614c92361a66c9cf0d8b Mon Sep 17 00:00:00 2001 From: Cedric Date: Fri, 26 Jun 2026 13:23:49 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 85 ++++++++++++++++++++++----------------------------------- 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/api.php b/api.php index 147a167..abc7b44 100644 --- a/api.php +++ b/api.php @@ -69,33 +69,31 @@ function getFanartApiKey($pdo) { return $row ? decryptData($row['key_value']) : null; } -function httpGet($url, $timeout = 3, $ua = null) { - if (!$ua) $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'; +function httpGet($url, $timeout = 10, $ua = null) { + if (!$ua) $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'; - if (!function_exists('curl_init')) { - $ctx = stream_context_create(['http' => [ - 'timeout' => $timeout, - 'user_agent' => $ua, - 'header' => "Accept: application/json\r\nAccept-Language: fr-FR,fr;q=0.9\r\n" - ]]); - return @file_get_contents($url, false, $ctx); - } $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $timeout, - CURLOPT_CONNECTTIMEOUT => 3, + CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERAGENT => $ua, CURLOPT_FOLLOWLOCATION => true, + CURLOPT_REFERER => 'https://www.cinemapassion.com/', // Crucial pour éviter le blocage CURLOPT_HTTPHEADER => [ - 'Accept: application/json', - 'Accept-Language: fr-FR,fr;q=0.9,en;q=0.8', + 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', + 'Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.5,en;q=0.3', + 'Connection: keep-alive', + 'Upgrade-Insecure-Requests: 1' ], ]); $res = curl_exec($ch); + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - return $res ?: null; + + // Si le code est 403, le site vous a banni, essayez de ralentir les requêtes + return ($code === 200) ? $res : null; } function cleanTitle($title) { @@ -120,51 +118,34 @@ function extractYear($dateStr) { return ''; } -// ── FONCTION DE RÉCUPÉRATION (Mise à jour TMDB / OMDb) ── function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { $defaultPoster = 'assets/img/default_physical_media.jpg'; - - if (empty($title)) { - return ['poster' => $defaultPoster, 'title' => '', 'format' => 'Blu-ray']; - } - $cleanTitle = cleanTitle($title); - $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; - - if ($omdbKey) { - $omdbUrl = "http://www.omdbapi.com/?apikey=" . urlencode($omdbKey) . "&t=" . urlencode($cleanTitle); - if (!empty($year)) $omdbUrl .= "&y=" . urlencode($year); + + // ÉTAPE 1 : URL de recherche standardisée + $searchUrl = "https://www.cinemapassion.com/recherche.php?recherche=" . urlencode($cleanTitle); + $searchRes = httpGet($searchUrl); + + if ($searchRes) { + // Extraction du lien du premier résultat + if (preg_match('/ $imgMatches[1], + 'title' => $cleanTitle, + 'format' => 'Blu-ray' + ]; + } } } } - - return [ - 'poster' => $posterUrl ?: $defaultPoster, - 'title' => $cleanTitle, - 'format' => 'Blu-ray' // Le format réel est affiné par detectFormat() plus tard - ]; + return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; } // ── ROUTEUR PRINCIPAL ──