Actualiser api.php

This commit is contained in:
2026-06-30 16:36:30 +02:00
parent ed7c2454b4
commit 6a49a166dd
+148 -93
View File
@@ -541,6 +541,7 @@ function fetchFromBlurayCom($ean) {
return $empty; return $empty;
} }
// ── FONCTION POUR RÉCUPÉRER LES DONNÉES DEPUIS MOVIECOVERS.COM ──
function fetchFromMovieCovers($title, $year = '') { function fetchFromMovieCovers($title, $year = '') {
$empty = [ $empty = [
'title' => '', 'year' => '', 'director' => '', 'actors' => '', 'title' => '', 'year' => '', 'director' => '', 'actors' => '',
@@ -551,55 +552,106 @@ function fetchFromMovieCovers($title, $year = '') {
if (empty($title)) return $empty; if (empty($title)) return $empty;
// Nettoyer le titre pour l'URL // Nettoyer le titre pour la recherche
$urlTitle = strtoupper(str_replace(' ', '+', $title)); $searchTitle = trim($title);
$url = "https://moviecovers.com/film/titre_{$urlTitle}.html"; $searchTitle = preg_replace('/\s*[\[\(].*?[\]\)]\s*/', '', $searchTitle); // Enlever [Blu-ray], (2015), etc.
$searchTitle = preg_replace('/\s*-\s*(Édition|Edition|Collector|Simple|Spéciale|Digibook|Ultimate|Intégrale|Combo|SteelBook|Boîtier).*$/i', '', $searchTitle);
$searchTitle = preg_replace('/(blu-ray|bluray|dvd|4k|ultra hd|combo|vhs|bdrip).*$/i', '', $searchTitle);
$searchTitle = trim(preg_replace('/\s{2,}/', ' ', $searchTitle));
$html = httpGet($url, 10); if (empty($searchTitle)) return $empty;
if (!$html) return $empty;
// Extraire le titre error_log("MovieCovers: 🔍 Recherche de '$searchTitle'");
if (preg_match('/<TITLE>([^<]+)<\/TITLE>/i', $html, $m)) {
// ÉTAPE 1 : Recherche du film via l'URL de recherche
$searchUrl = "https://moviecovers.com/multicrit.html?titre=" . urlencode($searchTitle) . "&slow=1&listes=1";
$html = httpGet($searchUrl, 10);
if (!$html) {
error_log("MovieCovers: ❌ Échec de la recherche");
return $empty;
}
// Extraire le lien vers la page du film
// Format : <a href="/film/titre_SAW.html">SAW</a>
if (!preg_match('/href=["\']?\/film\/titre_([^"\'\s]+)\.html["\']?/i', $html, $matches)) {
error_log("MovieCovers: ❌ Film non trouvé pour '$searchTitle'");
return $empty;
}
$filmId = $matches[1];
$filmUrl = "https://moviecovers.com/film/titre_{$filmId}.html";
error_log("MovieCovers: ✅ Film trouvé → $filmUrl");
// ÉTAPE 2 : Récupérer la page du film
$filmHtml = httpGet($filmUrl, 10);
if (!$filmHtml) {
error_log("MovieCovers: ❌ Impossible de charger la page du film");
return $empty;
}
// ÉTAPE 3 : Extraire les informations
// Titre (via og:title)
if (preg_match('/<meta property="og:title" content="([^"]+)"/i', $filmHtml, $m)) {
$empty['title'] = trim($m[1]); $empty['title'] = trim($m[1]);
} else {
$empty['title'] = $searchTitle;
} }
// Extraire l'affiche - plusieurs patterns // IDMC (via og:image ou PRE)
if (preg_match('/<img[^>]*src="(https:\/\/moviecovers\.com\/DATA\/thumbs\/[^"]+)"[^>]*title="Recto/i', $html, $m)) { $idmc = null;
$empty['poster'] = $m[1]; if (preg_match('/<meta property="og:image" content="[^"]*\/getjpg\.html\/([^"\'\/]+)\.jpg"/i', $filmHtml, $m)) {
} elseif (preg_match('/<meta[^>]*property="og:image"[^>]*content="([^"]+)"/i', $html, $m)) { $idmc = urldecode($m[1]);
$empty['poster'] = $m[1]; } elseif (preg_match('/IDMC.*?<PRE>([^<]+)<\/PRE>/is', $filmHtml, $m)) {
$idmc = trim($m[1]);
} }
// Format haute qualité de l'affiche if (!$idmc) {
if ($empty['poster']) { error_log("MovieCovers: ❌ IDMC non trouvé");
$empty['poster'] = str_replace('/thumbs/', '/films-l/', $empty['poster']); return $empty;
} }
// Extraire le réalisateur // URL de la cover (haute qualité)
if (preg_match('/R&eacute;alisateur<\/TH>\s*<TD[^>]*>.*?<a[^>]*>([^<]+)<\/a>/is', $html, $m)) { $idmcEncoded = urlencode($idmc);
$empty['director'] = trim($m[1]); $empty['poster'] = "https://moviecovers.com/DATA/zipcache/{$idmcEncoded}.jpg";
}
// Extraire l'année // Année
if (preg_match('/Ann&eacute;e<\/TH>\s*<TD[^>]*>.*?<a[^>]*>(\d{4})<\/a>/is', $html, $m)) { if (preg_match('/<TH[^>]*>Ann[^<]*e<\/TH>\s*<TD[^>]*>.*?(\d{4})/is', $filmHtml, $m)) {
$empty['year'] = $m[1]; $empty['year'] = $m[1];
} }
// Extraire la durée // Durée
if (preg_match('/Dur&eacute;e<\/TH>\s*<TD[^>]*>([\dH]+[\dmin]*)/is', $html, $m)) { if (preg_match('/<TH[^>]*>Dur[^<]*e<\/TH>\s*<TD[^>]*>([^<]+)/is', $filmHtml, $m)) {
$empty['length'] = trim($m[1]); $empty['length'] = trim(strip_tags($m[1]));
} }
// Extraire les acteurs // Réalisateur
if (preg_match_all('/<a href="\/multicrit\.html\?acteur=[^"]*">([^<]+)<\/a>/i', $html, $matches)) { if (preg_match('/<TH[^>]*>R[^<]*alisateur<\/TH>\s*<TD[^>]*>.*?<a[^>]*>([^<]+)<\/a>/is', $filmHtml, $m)) {
$empty['actors'] = implode(', ', array_slice($matches[1], 0, 5)); $empty['director'] = trim($m[1]);
} }
// Extraire le synopsis // Acteurs (extraire tous les liens dans la section acteurs)
if (preg_match('/<meta[^>]*property="og:description"[^>]*content="([^"]+)"/i', $html, $m)) { if (preg_match('/<TH[^>]*>Acteurs[^<]*<\/TH>\s*<TD[^>]*>(.*?)<\/TD>/is', $filmHtml, $m)) {
$empty['description'] = html_entity_decode($m[1]); $actorsHtml = $m[1];
preg_match_all('/<a[^>]*>([^<]+)<\/a>/i', $actorsHtml, $actorMatches);
if (!empty($actorMatches[1])) {
$empty['actors'] = implode(', ', array_map('trim', array_slice($actorMatches[1], 0, 5)));
}
} }
// Synopsis (via og:description)
if (preg_match('/<meta property="og:description" content="([^"]+)"/i', $filmHtml, $m)) {
$empty['description'] = html_entity_decode($m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
// Distribution (éditeur)
if (preg_match('/<TH[^>]*>Distribution<\/TH>\s*<TD[^>]*>.*?<a[^>]*>([^<]+)<\/a>/is', $filmHtml, $m)) {
$empty['publisher'] = trim($m[1]);
}
error_log("MovieCovers: ✅ Données récupérées → " . $empty['title'] . " (" . $empty['year'] . ")");
return $empty; return $empty;
} }
@@ -747,80 +799,83 @@ case 'import_batch':
$imported = 0; $skipped = 0; $imported = 0; $skipped = 0;
try { try {
if ($type === 'videotheque') { if ($type === 'videotheque') {
$stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director) $stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE
title=VALUES(title), year=VALUES(year), format=VALUES(format), title=VALUES(title), year=VALUES(year), format=VALUES(format),
poster=IF(VALUES(poster)!='assets/img/default_physical_media.jpg', VALUES(poster), poster), poster=IF(VALUES(poster)!='assets/img/default_physical_media.jpg', VALUES(poster), poster),
ean_isbn13=IF(VALUES(ean_isbn13)!='', VALUES(ean_isbn13), ean_isbn13), ean_isbn13=IF(VALUES(ean_isbn13)!='', VALUES(ean_isbn13), ean_isbn13),
description=IF(VALUES(description)!='', VALUES(description), description), description=IF(VALUES(description)!='', VALUES(description), description),
length=IF(VALUES(length)!='', VALUES(length), length), length=IF(VALUES(length)!='', VALUES(length), length),
number_of_discs=IF(VALUES(number_of_discs)!=1, VALUES(number_of_discs), number_of_discs), number_of_discs=IF(VALUES(number_of_discs)!=1, VALUES(number_of_discs), number_of_discs),
aspect_ratio=IF(VALUES(aspect_ratio)!='', VALUES(aspect_ratio), aspect_ratio), aspect_ratio=IF(VALUES(aspect_ratio)!='', VALUES(aspect_ratio), aspect_ratio),
actors=IF(VALUES(actors)!='', VALUES(actors), actors), actors=IF(VALUES(actors)!='', VALUES(actors), actors),
publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher), publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher),
director=IF(VALUES(director)!='', VALUES(director), director)"); director=IF(VALUES(director)!='', VALUES(director), director)");
foreach ($items as $item) { foreach ($items as $item) {
$ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? '')); $ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? ''));
$csvTitle = trim($item['title'] ?? ''); $csvTitle = trim($item['title'] ?? '');
if (strlen($ean) < 8 && empty($csvTitle)) { if (strlen($ean) < 8 && empty($csvTitle)) {
$skipped++; $skipped++;
continue; continue;
} }
// 1. Essayer Blu-ray.com (avec throttling) error_log("Import: 🎬 Traitement de '$csvTitle' (EAN: $ean)");
$blurayData = !empty($ean) ? fetchFromBlurayCom($ean) : [];
// 2. Si Blu-ray.com échoue, essayer MovieCovers avec le titre CSV // 1. Essayer Blu-ray.com (prioritaire pour les données physiques)
if (empty($blurayData['title']) && !empty($csvTitle)) { $blurayData = !empty($ean) ? fetchFromBlurayCom($ean) : [];
error_log("Import: 🔄 Blu-ray.com échoué, essai MovieCovers pour '$csvTitle'");
$blurayData = array_merge($blurayData, fetchFromMovieCovers($csvTitle, ''));
}
// 3. Utiliser le titre trouvé ou le titre CSV // 2. Si Blu-ray.com échoue, essayer MovieCovers avec le titre CSV
$title = !empty($blurayData['title']) ? $blurayData['title'] : $csvTitle; $movieCoversData = [];
if ((empty($blurayData['title']) || empty($blurayData['poster'])) && !empty($csvTitle)) {
error_log("Import: 🔄 Blu-ray.com incomplet, essai MovieCovers pour '$csvTitle'");
$movieCoversData = fetchFromMovieCovers($csvTitle);
}
if (empty($title)) { // 3. Fusionner les données (Blu-ray.com prioritaire, MovieCovers en complément)
error_log("Import: ❌ Pas de titre pour EAN $ean - ignoré"); $title = !empty($blurayData['title']) ? $blurayData['title'] : (!empty($movieCoversData['title']) ? $movieCoversData['title'] : $csvTitle);
$skipped++;
continue;
}
$year = $blurayData['year'] ?? ''; if (empty($title)) {
$format = $blurayData['format'] ?: detectFormat($title); error_log("Import: ❌ Pas de titre pour EAN $ean - ignoré");
$publisher = $blurayData['publisher'] ?? ''; $skipped++;
$discs = $blurayData['number_of_discs'] ?: 1; continue;
$aspect = $blurayData['aspect_ratio'] ?? ''; }
$length = $blurayData['length'] ?? '';
$poster = $blurayData['poster'] ?? '';
$desc = $blurayData['description'] ?? '';
$director = $blurayData['director'] ?? '';
$actors = $blurayData['actors'] ?? '';
// 4. Fallback TMDB si données manquantes $year = !empty($blurayData['year']) ? $blurayData['year'] : (!empty($movieCoversData['year']) ? $movieCoversData['year'] : '');
if (empty($poster) || empty($director) || empty($actors) || empty($desc)) { $format = !empty($blurayData['format']) ? $blurayData['format'] : detectFormat($title);
error_log("Import: 🔄 Fallback TMDB pour '$title'"); $publisher = !empty($blurayData['publisher']) ? $blurayData['publisher'] : (!empty($movieCoversData['publisher']) ? $movieCoversData['publisher'] : '');
$tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo); $discs = !empty($blurayData['number_of_discs']) ? $blurayData['number_of_discs'] : 1;
$aspect = !empty($blurayData['aspect_ratio']) ? $blurayData['aspect_ratio'] : '';
$length = !empty($blurayData['length']) ? $blurayData['length'] : (!empty($movieCoversData['length']) ? $movieCoversData['length'] : '');
$poster = !empty($blurayData['poster']) ? $blurayData['poster'] : (!empty($movieCoversData['poster']) ? $movieCoversData['poster'] : '');
$desc = !empty($blurayData['description']) ? $blurayData['description'] : (!empty($movieCoversData['description']) ? $movieCoversData['description'] : '');
$director = !empty($blurayData['director']) ? $blurayData['director'] : (!empty($movieCoversData['director']) ? $movieCoversData['director'] : '');
$actors = !empty($blurayData['actors']) ? $blurayData['actors'] : (!empty($movieCoversData['actors']) ? $movieCoversData['actors'] : '');
if (empty($poster) || $poster === 'assets/img/default_physical_media.jpg') { // 4. Fallback TMDB si données manquantes
$poster = $tmdb['poster']; if (empty($poster) || empty($director) || empty($actors) || empty($desc) || empty($year)) {
} error_log("Import: 🔄 Fallback TMDB pour '$title'");
if (empty($director)) $director = $tmdb['director'] ?? ''; $tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
if (empty($actors)) $actors = $tmdb['actors'] ?? '';
if (empty($desc)) $desc = $tmdb['description'] ?? '';
if (empty($length) && !empty($tmdb['length'])) $length = $tmdb['length'];
if (empty($year) && !empty($tmdb['year'])) $year = $tmdb['year'];
}
$id = makeStableId('videotheque', $title, $year); if (empty($poster) || $poster === 'assets/img/default_physical_media.jpg') {
$stmt->execute([$id, $title, $year, $format, $poster, $ean, $desc, $length, $discs, $aspect, $actors, $publisher, $director]); $poster = $tmdb['poster'];
$imported++;
error_log("Import: ✅ Importé '$title' ($ean) - Disques: $discs, Format: $format");
} }
} else { if (empty($director)) $director = $tmdb['director'] ?? '';
if (empty($actors)) $actors = $tmdb['actors'] ?? '';
if (empty($desc)) $desc = $tmdb['description'] ?? '';
if (empty($length) && !empty($tmdb['length'])) $length = $tmdb['length'];
if (empty($year) && !empty($tmdb['year'])) $year = $tmdb['year'];
}
$id = makeStableId('videotheque', $title, $year);
$stmt->execute([$id, $title, $year, $format, $poster, $ean, $desc, $length, $discs, $aspect, $actors, $publisher, $director]);
$imported++;
error_log("Import: ✅ Importé '$title' ($ean) - Poster: " . ($poster ? 'OK' : 'MANQUANT'));
}
} else {
// Import critiques (inchangé) // Import critiques (inchangé)
$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 (?, ?, ?, ?, ?, ?, ?, ?)