Actualiser api.php

This commit is contained in:
2026-06-26 15:24:16 +02:00
parent df2554d4be
commit 6300728c93
+134 -167
View File
@@ -59,13 +59,6 @@ function getTmdbApiKey($pdo) {
return $row ? decryptData($row['key_value']) : null; return $row ? decryptData($row['key_value']) : null;
} }
function getFanartApiKey($pdo) {
$stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = 'fanart_api_key'");
$stmt->execute();
$row = $stmt->fetch();
return $row ? decryptData($row['key_value']) : null;
}
function httpGet($url, $timeout = 5, $ua = null) { function httpGet($url, $timeout = 5, $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 (!$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')) { if (!function_exists('curl_init')) {
@@ -82,37 +75,7 @@ function httpGet($url, $timeout = 5, $ua = null) {
CURLOPT_USERAGENT => $ua, CURLOPT_USERAGENT => $ua,
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_REFERER => 'https://www.cinemapassion.com/', CURLOPT_REFERER => 'https://www.cinemapassion.com/',
CURLOPT_HTTPHEADER => [ CURLOPT_HTTPHEADER => ['Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: fr-FR,fr;q=0.8'],
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: fr-FR,fr;q=0.8',
],
]);
$res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($code === 200 && is_string($res) && strlen($res) > 0) ? $res : null;
}
function httpPost($url, $postData, $timeout = 10) {
$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')) return null;
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => $ua,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => is_array($postData) ? http_build_query($postData) : $postData,
CURLOPT_REFERER => 'https://www.cinemapassion.com/',
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: fr-FR,fr;q=0.8',
'Origin: https://www.cinemapassion.com'
],
]); ]);
$res = curl_exec($ch); $res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
@@ -142,76 +105,74 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) {
$defaultPoster = 'assets/img/default_physical_media.jpg'; $defaultPoster = 'assets/img/default_physical_media.jpg';
$cleanTitle = cleanTitle($title); $cleanTitle = cleanTitle($title);
if (empty($cleanTitle)) { if (empty($cleanTitle)) return ['poster' => $defaultPoster, 'title' => '', 'format' => 'Blu-ray'];
return ['poster' => $defaultPoster, 'title' => '', 'format' => 'Blu-ray'];
}
$baseUrl = 'http://www.cinemapassion.com'; $baseUrl = 'https://www.cinemapassion.com';
// 1. Initialisation d'une session cURL UNIQUE (Partage des cookies)
// C'est vital car moteur2.php utilise des sessions PHP pour la recherche
$ch = curl_init(); $ch = curl_init();
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true, // Indispensable si le site redirige directement vers le film
CURLOPT_MAXREDIRS => 5, CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 10, CURLOPT_TIMEOUT => 15,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
CURLOPT_COOKIEFILE => '', // Active le stockage des cookies en RAM entre les requêtes CURLOPT_COOKIEFILE => '', // Garde les cookies en RAM
CURLOPT_REFERER => $baseUrl . '/', CURLOPT_REFERER => 'https://www.cinemapassion.com/',
CURLOPT_SSL_VERIFYPEER => false CURLOPT_SSL_VERIFYPEER => false
]); ]);
// 2. Recherche via POST // 1. Recherche initiale
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/moteur2.php'); curl_setopt($ch, CURLOPT_URL, $baseUrl . '/recherche.php?recherche=' . urlencode($cleanTitle));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['recherche' => $cleanTitle]));
$html = curl_exec($ch); $html = curl_exec($ch);
$finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
// 3. Extraction du lien du film $filmHtml = null;
$filmUrl = null; $regexFilm = '/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com\/)?(?:\.\.\/)?(film[\/-][^"\'\s>]+)["\']?/i';
if ($html && preg_match('/href=["\']?(?:.*?\/)?(film\/[^"\'\s>]+)["\']?/i', $html, $matches)) {
$filmUrl = $baseUrl . '/' . $matches[1]; // Si la recherche a redirigé directement sur la fiche du film (1 seul résultat)
if (strpos($finalUrl, 'film-') !== false || strpos($finalUrl, '/film/') !== false || preg_match('/jaquette-(?:dvd|blu-ray)-/i', $html)) {
$filmHtml = $html;
} else { } else {
// Plan B : Recherche via GET si le POST échoue // Chercher le lien dans les résultats de recherche GET
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/recherche.php?recherche=' . urlencode($cleanTitle)); if ($html && preg_match($regexFilm, $html, $matches)) {
curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_URL, $baseUrl . '/' . ltrim($matches[1], './'));
$html = curl_exec($ch); $filmHtml = curl_exec($ch);
if ($html && preg_match('/href=["\']?(?:.*?\/)?(film\/[^"\'\s>]+)["\']?/i', $html, $matches)) { } else {
$filmUrl = $baseUrl . '/' . $matches[1]; // Tentative via POST si le GET échoue
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/moteur2.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['recherche' => $cleanTitle]));
$htmlPost = curl_exec($ch);
curl_setopt($ch, CURLOPT_POST, false);
$finalUrlPost = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if (strpos($finalUrlPost, 'film-') !== false || strpos($finalUrlPost, '/film/') !== false) {
$filmHtml = $htmlPost;
} elseif ($htmlPost && preg_match($regexFilm, $htmlPost, $matches)) {
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/' . ltrim($matches[1], './'));
$filmHtml = curl_exec($ch);
}
} }
} }
// 4. Si on a trouvé le film, on charge sa page // Si on a récupéré le code de la fiche film
if ($filmUrl) { if ($filmHtml) {
curl_setopt($ch, CURLOPT_URL, $filmUrl); // Chercher la sous-page contenant la jaquette
curl_setopt($ch, CURLOPT_POST, false); if (preg_match('/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com\/)?(?:\.\.\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s>]+)["\']?/i', $filmHtml, $jaqMatches)) {
$filmHtml = curl_exec($ch); curl_setopt($ch, CURLOPT_URL, $baseUrl . '/' . ltrim($jaqMatches[1], './'));
$jaqHtml = curl_exec($ch);
if ($filmHtml) {
// 5. Chercher le lien de la page contenant la jaquette
if (preg_match('/href=["\']?(?:.*?\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s>]+)["\']?/i', $filmHtml, $jaqMatches)) {
$jaqUrl = $baseUrl . '/' . $jaqMatches[1];
// Charger la page de la jaquette
curl_setopt($ch, CURLOPT_URL, $jaqUrl);
$jaqHtml = curl_exec($ch);
// 6. Extraire l'image haute définition (covers_temp/covers...)
if ($jaqHtml && preg_match('/src=["\']?(http:\/\/www\.cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $jaqHtml, $imgMatches)) {
curl_close($ch);
// On convertit l'URL en HTTPS pour éviter les erreurs de contenu mixte sur votre site web
return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray'];
}
}
// Fallback : Si aucune sous-page jaquette n'existe, on cherche l'image directement sur la page du film // Extraire l'image haute définition
if (preg_match('/src=["\']?(http:\/\/www\.cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $filmHtml, $imgMatches)) { if ($jaqHtml && preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $jaqHtml, $imgMatches)) {
// On s'assure qu'on ne prend pas les miniatures curl_close($ch);
if (strpos($imgMatches[1], 'miniature') === false) { return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray'];
curl_close($ch); }
return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray']; }
}
// Fallback direct sur la fiche du film si la sous-page jaquette n'existe pas
if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $filmHtml, $imgMatches)) {
if (strpos($imgMatches[1], 'miniature') === false && strpos($imgMatches[1], 'vign') === false) {
curl_close($ch);
return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray'];
} }
} }
} }
@@ -348,7 +309,7 @@ switch ($action) {
ORDER BY id DESC ORDER BY id DESC
"; ";
$result = $pdo->query($sql)->fetchAll(); $result = $pdo->query($sql)->fetchAll();
foreach ($result as $row) { foreach ($result as &$row) {
if ($row['rating'] !== null) { if ($row['rating'] !== null) {
$ratingVal = (float)$row['rating']; $ratingVal = (float)$row['rating'];
$row['rating'] = ($ratingVal == floor($ratingVal)) ? (int)$ratingVal : $ratingVal; $row['rating'] = ($ratingVal == floor($ratingVal)) ? (int)$ratingVal : $ratingVal;
@@ -448,10 +409,8 @@ switch ($action) {
$id = makeStableId('videotheque', $title, $year); $id = makeStableId('videotheque', $title, $year);
// ✅ UTILISER CINEMAPASSION.COM
$cpData = fetchCinemaPassion($title, $year, $ean, $pdo); $cpData = fetchCinemaPassion($title, $year, $ean, $pdo);
$poster = $cpData['poster']; $poster = $cpData['poster'];
$format = detectFormat($title, $desc); $format = detectFormat($title, $desc);
$stmtVideo->execute([ $stmtVideo->execute([
@@ -461,7 +420,6 @@ switch ($action) {
$imported++; $imported++;
} }
} else { } else {
// ✅ CORRECTION CRITIQUE : Initialisation des variables pour les critiques
$stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) $stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director),
@@ -509,94 +467,103 @@ switch ($action) {
// ── ENDPOINT DE DEBUG DÉTAILLÉ ── // ── ENDPOINT DE DEBUG DÉTAILLÉ ──
case 'debug_cinemapassion': case 'debug_cinemapassion':
$title = $_GET['title'] ?? 'Saw'; $title = $_GET['title'] ?? 'Saw';
$year = $_GET['year'] ?? '';
$ean = $_GET['ean'] ?? '';
$debugInfo = [ $debugInfo = [
'title' => $title, 'title' => $title,
'year' => $year,
'cleanTitle' => cleanTitle($title), 'cleanTitle' => cleanTitle($title),
'steps' => [] 'steps' => []
]; ];
// Test 1 : POST sur moteur2.php $baseUrl = 'https://www.cinemapassion.com';
$searchRes = httpPost('https://www.cinemapassion.com/moteur2.php', ['recherche' => cleanTitle($title)]); $ch = curl_init();
$debugInfo['steps']['search_post'] = [ curl_setopt_array($ch, [
'success' => !empty($searchRes), CURLOPT_RETURNTRANSFER => true,
'length' => $searchRes ? strlen($searchRes) : 0 CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 15,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
CURLOPT_COOKIEFILE => '',
CURLOPT_REFERER => 'https://www.cinemapassion.com/',
CURLOPT_SSL_VERIFYPEER => false
]);
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/recherche.php?recherche=' . urlencode(cleanTitle($title)));
$html = curl_exec($ch);
$finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$debugInfo['steps']['search_get'] = [
'final_url' => $finalUrl,
'success' => !empty($html)
]; ];
if ($searchRes) { $filmHtml = null;
// Test 2 : Extraction du lien film $regexFilm = '/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com\/)?(?:\.\.\/)?(film[\/-][^"\'\s>]+)["\']?/i';
if (preg_match('/href=["\']?(?:\.\.\/)?film\/([^"\'\s]+)-(\d+)\.php["\']?/i', $searchRes, $matches)) {
$debugInfo['steps']['film_found'] = [ if (strpos($finalUrl, 'film-') !== false || strpos($finalUrl, '/film/') !== false || preg_match('/jaquette-(?:dvd|blu-ray)-/i', $html)) {
'success' => true, $filmHtml = $html;
'filmName' => $matches[1], $debugInfo['steps']['film_found'] = 'Redirection directe ou page film détectée';
'filmId' => $matches[2] } else {
]; if ($html && preg_match($regexFilm, $html, $matches)) {
$filmUrl = $baseUrl . '/' . ltrim($matches[1], './');
$filmName = $matches[1]; $debugInfo['steps']['film_found'] = $filmUrl;
$filmId = $matches[2]; curl_setopt($ch, CURLOPT_URL, $filmUrl);
$filmHtml = curl_exec($ch);
// Test 3 : Pages de jaquettes
$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",
];
foreach ($pagesToTry as $i => $pageUrl) {
$pageRes = httpGet($pageUrl, 10);
$pageDebug = [
'url' => $pageUrl,
'success' => !empty($pageRes),
'length' => $pageRes ? strlen($pageRes) : 0
];
if ($pageRes) {
if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s>]+\.jpg)["\']?/i', $pageRes, $imgMatches)) {
$pageDebug['image_found'] = true;
$pageDebug['image_url'] = str_replace('http://', 'https://', $imgMatches[1]);
} elseif (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/lesaffiches\/[^"\'\s>]+\.jpg)["\']?/i', $pageRes, $imgMatches)) {
$pageDebug['affiche_found'] = true;
$pageDebug['image_url'] = str_replace('http://', 'https://', $imgMatches[1]);
} else {
$pageDebug['image_found'] = false;
// Extraire un extrait pour debug
$pageDebug['html_snippet'] = substr($pageRes, 0, 1000);
}
}
$debugInfo['steps']['pages'][$i] = $pageDebug;
if (!empty($pageDebug['image_found']) || !empty($pageDebug['affiche_found'])) {
$debugInfo['final_result'] = [
'success' => true,
'poster' => $pageDebug['image_url'],
'title' => cleanTitle($title),
'format' => 'Blu-ray'
];
echo json_encode($debugInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit;
}
}
} else { } else {
$debugInfo['steps']['film_found'] = [ curl_setopt($ch, CURLOPT_URL, $baseUrl . '/moteur2.php');
'success' => false, curl_setopt($ch, CURLOPT_POST, true);
'html_snippet' => substr($searchRes, 0, 2000) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['recherche' => cleanTitle($title)]));
$htmlPost = curl_exec($ch);
curl_setopt($ch, CURLOPT_POST, false);
$finalUrlPost = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$debugInfo['steps']['search_post'] = [
'final_url' => $finalUrlPost,
'success' => !empty($htmlPost)
]; ];
if (strpos($finalUrlPost, 'film-') !== false || strpos($finalUrlPost, '/film/') !== false) {
$filmHtml = $htmlPost;
$debugInfo['steps']['film_found'] = 'Redirection via POST';
} elseif ($htmlPost && preg_match($regexFilm, $htmlPost, $matches)) {
$filmUrl = $baseUrl . '/' . ltrim($matches[1], './');
$debugInfo['steps']['film_found'] = $filmUrl;
curl_setopt($ch, CURLOPT_URL, $filmUrl);
$filmHtml = curl_exec($ch);
} else {
$debugInfo['steps']['fail'] = 'Aucun lien de film trouvé';
$debugInfo['html_snippet'] = substr($htmlPost ?: $html, 0, 1000);
}
}
}
if ($filmHtml) {
if (preg_match('/href=["\']?(?:https?:\/\/(?:www\.)?cinemapassion\.com\/)?(?:\.\.\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s>]+)["\']?/i', $filmHtml, $jaqMatches)) {
$jaqUrl = $baseUrl . '/' . ltrim($jaqMatches[1], './');
$debugInfo['steps']['jaquette_page'] = $jaqUrl;
curl_setopt($ch, CURLOPT_URL, $jaqUrl);
$jaqHtml = curl_exec($ch);
if ($jaqHtml && preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $jaqHtml, $imgMatches)) {
$debugInfo['final_result'] = str_replace('http://', 'https://', $imgMatches[1]);
} else {
$debugInfo['steps']['jaquette_fail'] = 'Image covers_temp non trouvée sur la page jaquette';
}
}
if (!isset($debugInfo['final_result']) && preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $filmHtml, $imgMatches)) {
if (strpos($imgMatches[1], 'miniature') === false && strpos($imgMatches[1], 'vign') === false) {
$debugInfo['final_result'] = str_replace('http://', 'https://', $imgMatches[1]);
$debugInfo['steps']['fallback'] = 'Trouvé directement sur la page du film';
}
} }
} }
if (!isset($debugInfo['final_result'])) { if (!isset($debugInfo['final_result'])) {
$debugInfo['final_result'] = [ $debugInfo['final_result'] = 'assets/img/default_physical_media.jpg';
'success' => false,
'poster' => 'assets/img/default_physical_media.jpg'
];
} }
curl_close($ch);
echo json_encode($debugInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); echo json_encode($debugInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit; exit;
break;
} }
?> ?>