Actualiser api.php

This commit is contained in:
2026-06-26 13:23:49 +02:00
parent 48597b5ef9
commit 85d2c06d6f
+33 -52
View File
@@ -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('/<a href="(film-[^"]+\.html)"/i', $searchRes, $matches)) {
$filmUrl = 'https://www.cinemapassion.com/' . $matches[1];
$filmRes = httpGet($filmUrl);
$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 [
'poster' => $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 ──