Actualiser api.php

This commit is contained in:
2026-06-26 13:23:49 +02:00
parent 48597b5ef9
commit 85d2c06d6f
+31 -50
View File
@@ -69,33 +69,31 @@ function getFanartApiKey($pdo) {
return $row ? decryptData($row['key_value']) : null; return $row ? decryptData($row['key_value']) : null;
} }
function httpGet($url, $timeout = 3, $ua = null) { 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/120.0.0.0 Safari/537.36'; 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); $ch = curl_init($url);
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $timeout, CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => 3, CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => $ua, CURLOPT_USERAGENT => $ua,
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_REFERER => 'https://www.cinemapassion.com/', // Crucial pour éviter le blocage
CURLOPT_HTTPHEADER => [ CURLOPT_HTTPHEADER => [
'Accept: application/json', '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.9,en;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); $res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); 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) { function cleanTitle($title) {
@@ -120,52 +118,35 @@ function extractYear($dateStr) {
return ''; return '';
} }
// ── FONCTION DE RÉCUPÉRATION (Mise à jour TMDB / OMDb) ──
function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) {
$defaultPoster = 'assets/img/default_physical_media.jpg'; $defaultPoster = 'assets/img/default_physical_media.jpg';
if (empty($title)) {
return ['poster' => $defaultPoster, 'title' => '', 'format' => 'Blu-ray'];
}
$cleanTitle = cleanTitle($title); $cleanTitle = cleanTitle($title);
$posterUrl = null;
// 1. Recherche ultra-rapide via TMDB (Déjà configuré et prioritaire) // ÉTAPE 1 : URL de recherche standardisée
$tmdbKey = getTmdbApiKey($pdo); $searchUrl = "https://www.cinemapassion.com/recherche.php?recherche=" . urlencode($cleanTitle);
if ($tmdbKey) { $searchRes = httpGet($searchUrl);
$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 ($searchRes) {
if (empty($posterUrl)) { // Extraction du lien du premier résultat
$stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = 'omdb_api_key'"); if (preg_match('/<a href="(film-[^"]+\.html)"/i', $searchRes, $matches)) {
$stmt->execute(); $filmUrl = 'https://www.cinemapassion.com/' . $matches[1];
$row = $stmt->fetch(); $filmRes = httpGet($filmUrl);
$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);
$omdbRes = httpGet($omdbUrl, 5);
$omdbData = $omdbRes ? json_decode($omdbRes, true) : null;
if ($omdbData && isset($omdbData['Poster']) && $omdbData['Poster'] !== 'N/A') {
$posterUrl = $omdbData['Poster'];
}
}
}
if ($filmRes) {
// Regex générique pour trouver l'image principale (souvent dans une balise img unique)
// Cherche le dossier "affiches" ou "posters"
if (preg_match('/src="(https?:\/\/[^"]+\/(?:affiches|posters)\/[^"]+\.(?:jpg|png))"/i', $filmRes, $imgMatches)) {
return [ return [
'poster' => $posterUrl ?: $defaultPoster, 'poster' => $imgMatches[1],
'title' => $cleanTitle, 'title' => $cleanTitle,
'format' => 'Blu-ray' // Le format réel est affiné par detectFormat() plus tard 'format' => 'Blu-ray'
]; ];
} }
}
}
}
return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray'];
}
// ── ROUTEUR PRINCIPAL ── // ── ROUTEUR PRINCIPAL ──