Actualiser api.php

This commit is contained in:
2026-06-25 10:03:30 +02:00
parent 2953d9e4fc
commit d5025f9618
+79 -100
View File
@@ -117,22 +117,33 @@ function extractYear($dateStr) {
return ''; return '';
} }
// ── MovieCovers.com (jaquettes HD pour la vidéothèque) ── // ── CoverCentury.com (jaquettes HD pour vidéothèque) ──
function fetchMovieCovers($title, $year = '', $format = 'Blu-ray') { function fetchCoverCentury($title, $year = '', $format = 'bluray') {
if (empty($title)) return null; if (empty($title)) return null;
$ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'; $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
$cleanTitle = cleanTitle($title); $cleanTitle = cleanTitle($title);
// MovieCovers utilise des URLs de recherche // Mapping des formats pour CoverCentury
$searchUrl = "https://moviecovers.com/search?q=" . urlencode($cleanTitle); $formatMap = [
'4k ultra hd' => '4k',
'4k' => '4k',
'blu-ray' => 'bluray',
'bluray' => 'bluray',
'dvd' => 'dvd',
];
$ccFormat = $formatMap[strtolower($format)] ?? 'bluray';
// URL de recherche CoverCentury
$searchUrl = "https://www.covercentury.com/search?q=" . urlencode($cleanTitle);
if (!empty($year)) { if (!empty($year)) {
$searchUrl .= "+(" . $year . ")"; $searchUrl .= "+(" . $year . ")";
} }
$html = httpGet($searchUrl, 8, $ua); $html = httpGet($searchUrl, 8, $ua);
if (!$html) { if (!$html) {
error_log("MovieCovers: Échec recherche pour '$title'"); error_log("CoverCentury: Échec recherche pour '$title'");
return null; return null;
} }
@@ -142,78 +153,47 @@ function fetchMovieCovers($title, $year = '', $format = 'Blu-ray') {
'format' => $format, 'format' => $format,
]; ];
// Extraction des liens d'images depuis la page de résultats // Extraction des liens d'images - CoverCentury utilise des structures spécifiques
// MovieCovers utilise des liens vers des images en haute résolution // Chercher les liens vers les pages de covers
preg_match_all('/href=["\']([^"\']*\/covers?\/[^"\']+\.jpg)["\']/i', $html, $matches); preg_match_all('/href=["\']([^"\']*\/cover\/[^"\']+)["\']/i', $html, $coverLinks);
if (!empty($matches[1])) { if (!empty($coverLinks[1])) {
// Prendre la première image trouvée (généralement la plus pertinente) // Prendre le premier résultat pertinent
$result['poster'] = $matches[1][0]; $coverPage = $coverLinks[1][0];
if (strpos($coverPage, 'http') !== 0) {
$coverPage = 'https://www.covercentury.com' . $coverPage;
}
// Essayer de trouver une version HD si disponible // Récupérer la page du cover pour avoir l'image en haute qualité
foreach ($matches[1] as $imgUrl) { $coverHtml = httpGet($coverPage, 8, $ua);
if (strpos($imgUrl, 'hd') !== false || strpos($imgUrl, 'large') !== false) { if ($coverHtml) {
$result['poster'] = $imgUrl; // Chercher l'image principale en haute résolution
if (preg_match('/<img[^>]+src=["\']([^"\']+\.jpg)["\'][^>]*class=["\'][^"\']*main[^"\']*["\']/i', $coverHtml, $m)) {
$result['poster'] = $m[1];
} elseif (preg_match('/<meta[^>]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']/i', $coverHtml, $m)) {
$result['poster'] = $m[1];
} elseif (preg_match('/href=["\']([^"\']*\/images\/[^"\']+\.jpg)["\']/i', $coverHtml, $m)) {
$result['poster'] = $m[1];
}
// Extraire le titre
if (preg_match('/<title[^>]*>([^<]+) - CoverCentury/i', $coverHtml, $titleMatch)) {
$result['title'] = trim($titleMatch[1]);
}
}
}
// Fallback : chercher directement les images dans la page de résultats
if (empty($result['poster'])) {
preg_match_all('/<img[^>]+src=["\']([^"\']+\.jpg)["\'][^>]*>/i', $html, $imgMatches);
if (!empty($imgMatches[1])) {
foreach ($imgMatches[1] as $img) {
if (strpos($img, 'covercentury') !== false || strpos($img, '/covers/') !== false) {
$result['poster'] = $img;
break; break;
} }
} }
} }
// Si pas trouvé avec le pattern précédent, chercher dans les balises img
if (empty($result['poster'])) {
preg_match_all('/<img[^>]+src=["\']([^"\']*\/covers?\/[^"\']+\.jpg)["\'][^>]*>/i', $html, $imgMatches);
if (!empty($imgMatches[1])) {
$result['poster'] = $imgMatches[1][0];
}
}
// Extraire le titre depuis la page si disponible
if (preg_match('/<title[^>]*>([^<]+) - MovieCovers/i', $html, $titleMatch)) {
$result['title'] = trim($titleMatch[1]);
}
return (!empty($result['poster'])) ? $result : null;
}
// Fonction alternative pour recherche directe par URL si on connaît le format
function fetchMovieCoversDirect($title, $format = 'bluray') {
if (empty($title)) return null;
$ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
$cleanTitle = strtolower(trim(preg_replace('/[^a-zA-Z0-9\s]/', '', $title)));
$cleanTitle = preg_replace('/\s+/', '-', $cleanTitle);
// Formats supportés par MovieCovers
$formatMap = [
'4k ultra hd' => '4k',
'4k' => '4k',
'blu-ray' => 'bluray',
'bluray' => 'bluray',
'dvd' => 'dvd',
];
$mcFormat = $formatMap[strtolower($format)] ?? 'bluray';
// URL directe vers la page du cover
$directUrl = "https://moviecovers.com/{$mcFormat}/{$cleanTitle}";
$html = httpGet($directUrl, 8, $ua);
if (!$html) {
// Fallback sur la recherche
return fetchMovieCovers($title, '', $format);
}
$result = [
'poster' => '',
'title' => '',
'format' => $format,
];
// Chercher l'image principale en haute qualité
if (preg_match('/<img[^>]+src=["\']([^"\']+\.jpg)["\'][^>]*class=["\'][^"\']*cover[^"\']*["\']/i', $html, $m)) {
$result['poster'] = $m[1];
} elseif (preg_match('/<meta[^>]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']/i', $html, $m)) {
$result['poster'] = $m[1];
} }
return (!empty($result['poster'])) ? $result : null; return (!empty($result['poster'])) ? $result : null;
@@ -366,13 +346,12 @@ switch ($action) {
'length' => '', 'number_of_discs' => 1, 'aspect_ratio' => '', 'actors' => '' 'length' => '', 'number_of_discs' => 1, 'aspect_ratio' => '', 'actors' => ''
]; ];
// Pour MovieCovers, on a besoin d'un titre // Pour CoverCentury, on a besoin d'un titre
// On essaie d'abord de trouver le titre via TMDB (pour les critiques) ou on utilise l'EAN comme base
$tmdbKey = getTmdbApiKey($pdo); $tmdbKey = getTmdbApiKey($pdo);
$titleForSearch = ''; $titleForSearch = '';
if ($type === 'critique' && $tmdbKey) { if ($tmdbKey) {
// Pour les critiques, on cherche d'abord sur TMDB // Essayer de trouver via TMDB d'abord
$searchUrl = "https://api.themoviedb.org/3/find/{$ean}?api_key={$tmdbKey}&external_source=imdb_id"; $searchUrl = "https://api.themoviedb.org/3/find/{$ean}?api_key={$tmdbKey}&external_source=imdb_id";
$searchRes = httpGet($searchUrl, 5); $searchRes = httpGet($searchUrl, 5);
$searchData = $searchRes ? json_decode($searchRes, true) : []; $searchData = $searchRes ? json_decode($searchRes, true) : [];
@@ -386,23 +365,23 @@ switch ($action) {
} }
} }
// Si pas de titre trouvé, on utilise l'EAN comme référence (peu probable de trouver sur MovieCovers)
if (empty($titleForSearch)) { if (empty($titleForSearch)) {
echo json_encode(['success' => true, 'data' => $result, 'warning' => 'Titre non trouvé pour cet EAN']); echo json_encode(['success' => true, 'data' => $result, 'warning' => 'Titre non trouvé']);
exit; exit;
} }
// Récupération jaquette via MovieCovers (pour vidéothèque) ou TMDB (pour critiques) // Récupération jaquette
if ($type === 'videotheque') { if ($type === 'videotheque') {
// CoverCentury pour vidéothèque
$format = $result['format'] ?: 'Blu-ray'; $format = $result['format'] ?: 'Blu-ray';
$mcData = fetchMovieCovers($titleForSearch, $result['year'], $format); $ccData = fetchCoverCentury($titleForSearch, $result['year'], $format);
if (!empty($mcData)) { if (!empty($ccData)) {
if (!empty($mcData['poster'])) $result['poster'] = $mcData['poster']; if (!empty($ccData['poster'])) $result['poster'] = $ccData['poster'];
if (!empty($mcData['title'])) $result['title'] = $mcData['title']; if (!empty($ccData['title'])) $result['title'] = $ccData['title'];
$result['format'] = $format; $result['format'] = $format;
} }
} else { } else {
// Critiques : TMDB complet // TMDB pour critiques
if ($tmdbKey) { if ($tmdbKey) {
$tmdbData = fetchTMDBFull($titleForSearch, $result['year'], $tmdbKey, $pdo); $tmdbData = fetchTMDBFull($titleForSearch, $result['year'], $tmdbKey, $pdo);
if ($tmdbData) { if ($tmdbData) {
@@ -441,12 +420,12 @@ switch ($action) {
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$stmt->execute([$id, $data['title'] ?? '', $data['year'] ?? '', $data['director'] ?? '', $data['poster'] ?? '', $data['rating'] ?? 3.0, $data['review'] ?? '', $streaming]); $stmt->execute([$id, $data['title'] ?? '', $data['year'] ?? '', $data['director'] ?? '', $data['poster'] ?? '', $data['rating'] ?? 3.0, $data['review'] ?? '', $streaming]);
} else { } else {
// Vidéothèque : MovieCovers pour la jaquette // Vidéothèque : CoverCentury pour la jaquette
if (empty($data['poster']) && !empty($data['title'])) { if (empty($data['poster']) && !empty($data['title'])) {
$format = $data['format'] ?: 'Blu-ray'; $format = $data['format'] ?: 'Blu-ray';
$mcData = fetchMovieCovers($data['title'], $data['year'] ?? '', $format); $ccData = fetchCoverCentury($data['title'], $data['year'] ?? '', $format);
if (!empty($mcData['poster'])) { if (!empty($ccData['poster'])) {
$data['poster'] = $mcData['poster']; $data['poster'] = $ccData['poster'];
} }
} }
@@ -491,7 +470,7 @@ switch ($action) {
$id = makeStableId($type, $title, $year); $id = makeStableId($type, $title, $year);
if ($type === 'videotheque') { if ($type === 'videotheque') {
// ── VIDÉOTHÈQUE : MovieCovers pour jaquette HD, PAS de TMDB ── // ── VIDÉOTHÈQUE : CoverCentury pour jaquette HD ──
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
$actors = ''; $actors = '';
if (!empty($csvActors)) { if (!empty($csvActors)) {
@@ -523,21 +502,21 @@ switch ($action) {
$poster = $rowData['poster'] ?? ''; $poster = $rowData['poster'] ?? '';
$director = ''; $director = '';
// Récupération jaquette via MovieCovers // Récupération jaquette via CoverCentury
$cleanTitleForMC = cleanTitle($title); $cleanTitleForCC = cleanTitle($title);
if (!empty($cleanTitleForMC)) { if (!empty($cleanTitleForCC)) {
$mcData = fetchMovieCovers($cleanTitleForMC, $year, $format); $ccData = fetchCoverCentury($cleanTitleForCC, $year, $format);
if (!empty($mcData)) { if (!empty($ccData)) {
if (!empty($mcData['poster'])) { if (!empty($ccData['poster'])) {
$poster = $mcData['poster']; $poster = $ccData['poster'];
} }
if (!empty($mcData['title']) && ($title === 'Sans titre' || empty($title))) { if (!empty($ccData['title']) && ($title === 'Sans titre' || empty($title))) {
$title = $mcData['title']; $title = $ccData['title'];
} }
} }
} }
// PAS D'APPEL TMDB POUR LA VIDÉOTHÈQUE // PAS DE TMDB POUR VIDÉOTHÈQUE
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) $sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
@@ -602,7 +581,7 @@ switch ($action) {
} }
break; break;
case 'debug_moviecovers': case 'debug_covercentury':
$title = $_GET['title'] ?? ''; $title = $_GET['title'] ?? '';
$year = $_GET['year'] ?? ''; $year = $_GET['year'] ?? '';
$format = $_GET['format'] ?? 'Blu-ray'; $format = $_GET['format'] ?? 'Blu-ray';
@@ -610,7 +589,7 @@ switch ($action) {
if (!$title) { echo json_encode(['error' => 'Titre manquant']); exit; } if (!$title) { echo json_encode(['error' => 'Titre manquant']); exit; }
$result = ['title' => $title, 'year' => $year, 'format' => $format]; $result = ['title' => $title, 'year' => $year, 'format' => $format];
$data = fetchMovieCovers($title, $year, $format); $data = fetchCoverCentury($title, $year, $format);
$result['data'] = $data; $result['data'] = $data;
$result['status'] = $data ? 'OK' : 'AUCUN_RÉSULTAT'; $result['status'] = $data ? 'OK' : 'AUCUN_RÉSULTAT';