Actualiser api.php

This commit is contained in:
2026-06-30 16:36:30 +02:00
parent ed7c2454b4
commit 6a49a166dd
+157 -102
View File
@@ -541,6 +541,7 @@ function fetchFromBlurayCom($ean) {
return $empty;
}
// ── FONCTION POUR RÉCUPÉRER LES DONNÉES DEPUIS MOVIECOVERS.COM ──
function fetchFromMovieCovers($title, $year = '') {
$empty = [
'title' => '', 'year' => '', 'director' => '', 'actors' => '',
@@ -551,55 +552,106 @@ function fetchFromMovieCovers($title, $year = '') {
if (empty($title)) return $empty;
// Nettoyer le titre pour l'URL
$urlTitle = strtoupper(str_replace(' ', '+', $title));
$url = "https://moviecovers.com/film/titre_{$urlTitle}.html";
// Nettoyer le titre pour la recherche
$searchTitle = trim($title);
$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 (!$html) return $empty;
if (empty($searchTitle)) return $empty;
// Extraire le titre
if (preg_match('/<TITLE>([^<]+)<\/TITLE>/i', $html, $m)) {
error_log("MovieCovers: 🔍 Recherche de '$searchTitle'");
// É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]);
} else {
$empty['title'] = $searchTitle;
}
// Extraire l'affiche - plusieurs patterns
if (preg_match('/<img[^>]*src="(https:\/\/moviecovers\.com\/DATA\/thumbs\/[^"]+)"[^>]*title="Recto/i', $html, $m)) {
$empty['poster'] = $m[1];
} elseif (preg_match('/<meta[^>]*property="og:image"[^>]*content="([^"]+)"/i', $html, $m)) {
$empty['poster'] = $m[1];
// IDMC (via og:image ou PRE)
$idmc = null;
if (preg_match('/<meta property="og:image" content="[^"]*\/getjpg\.html\/([^"\'\/]+)\.jpg"/i', $filmHtml, $m)) {
$idmc = urldecode($m[1]);
} elseif (preg_match('/IDMC.*?<PRE>([^<]+)<\/PRE>/is', $filmHtml, $m)) {
$idmc = trim($m[1]);
}
// Format haute qualité de l'affiche
if ($empty['poster']) {
$empty['poster'] = str_replace('/thumbs/', '/films-l/', $empty['poster']);
if (!$idmc) {
error_log("MovieCovers: ❌ IDMC non trouvé");
return $empty;
}
// Extraire le réalisateur
if (preg_match('/R&eacute;alisateur<\/TH>\s*<TD[^>]*>.*?<a[^>]*>([^<]+)<\/a>/is', $html, $m)) {
$empty['director'] = trim($m[1]);
}
// URL de la cover (haute qualité)
$idmcEncoded = urlencode($idmc);
$empty['poster'] = "https://moviecovers.com/DATA/zipcache/{$idmcEncoded}.jpg";
// Extraire l'année
if (preg_match('/Ann&eacute;e<\/TH>\s*<TD[^>]*>.*?<a[^>]*>(\d{4})<\/a>/is', $html, $m)) {
// Année
if (preg_match('/<TH[^>]*>Ann[^<]*e<\/TH>\s*<TD[^>]*>.*?(\d{4})/is', $filmHtml, $m)) {
$empty['year'] = $m[1];
}
// Extraire la durée
if (preg_match('/Dur&eacute;e<\/TH>\s*<TD[^>]*>([\dH]+[\dmin]*)/is', $html, $m)) {
$empty['length'] = trim($m[1]);
// Durée
if (preg_match('/<TH[^>]*>Dur[^<]*e<\/TH>\s*<TD[^>]*>([^<]+)/is', $filmHtml, $m)) {
$empty['length'] = trim(strip_tags($m[1]));
}
// Extraire les acteurs
if (preg_match_all('/<a href="\/multicrit\.html\?acteur=[^"]*">([^<]+)<\/a>/i', $html, $matches)) {
$empty['actors'] = implode(', ', array_slice($matches[1], 0, 5));
// Réalisateur
if (preg_match('/<TH[^>]*>R[^<]*alisateur<\/TH>\s*<TD[^>]*>.*?<a[^>]*>([^<]+)<\/a>/is', $filmHtml, $m)) {
$empty['director'] = trim($m[1]);
}
// Extraire le synopsis
if (preg_match('/<meta[^>]*property="og:description"[^>]*content="([^"]+)"/i', $html, $m)) {
$empty['description'] = html_entity_decode($m[1]);
// Acteurs (extraire tous les liens dans la section acteurs)
if (preg_match('/<TH[^>]*>Acteurs[^<]*<\/TH>\s*<TD[^>]*>(.*?)<\/TD>/is', $filmHtml, $m)) {
$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;
}
@@ -747,80 +799,83 @@ case 'import_batch':
$imported = 0; $skipped = 0;
try {
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)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE
title=VALUES(title), year=VALUES(year), format=VALUES(format),
poster=IF(VALUES(poster)!='assets/img/default_physical_media.jpg', VALUES(poster), poster),
ean_isbn13=IF(VALUES(ean_isbn13)!='', VALUES(ean_isbn13), ean_isbn13),
description=IF(VALUES(description)!='', VALUES(description), description),
length=IF(VALUES(length)!='', VALUES(length), length),
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),
actors=IF(VALUES(actors)!='', VALUES(actors), actors),
publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher),
director=IF(VALUES(director)!='', VALUES(director), director)");
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)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE
title=VALUES(title), year=VALUES(year), format=VALUES(format),
poster=IF(VALUES(poster)!='assets/img/default_physical_media.jpg', VALUES(poster), poster),
ean_isbn13=IF(VALUES(ean_isbn13)!='', VALUES(ean_isbn13), ean_isbn13),
description=IF(VALUES(description)!='', VALUES(description), description),
length=IF(VALUES(length)!='', VALUES(length), length),
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),
actors=IF(VALUES(actors)!='', VALUES(actors), actors),
publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher),
director=IF(VALUES(director)!='', VALUES(director), director)");
foreach ($items as $item) {
$ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? ''));
$csvTitle = trim($item['title'] ?? '');
if (strlen($ean) < 8 && empty($csvTitle)) {
$skipped++;
continue;
}
error_log("Import: 🎬 Traitement de '$csvTitle' (EAN: $ean)");
// 1. Essayer Blu-ray.com (prioritaire pour les données physiques)
$blurayData = !empty($ean) ? fetchFromBlurayCom($ean) : [];
// 2. Si Blu-ray.com échoue, essayer MovieCovers avec le titre CSV
$movieCoversData = [];
if ((empty($blurayData['title']) || empty($blurayData['poster'])) && !empty($csvTitle)) {
error_log("Import: 🔄 Blu-ray.com incomplet, essai MovieCovers pour '$csvTitle'");
$movieCoversData = fetchFromMovieCovers($csvTitle);
}
// 3. Fusionner les données (Blu-ray.com prioritaire, MovieCovers en complément)
$title = !empty($blurayData['title']) ? $blurayData['title'] : (!empty($movieCoversData['title']) ? $movieCoversData['title'] : $csvTitle);
if (empty($title)) {
error_log("Import: ❌ Pas de titre pour EAN $ean - ignoré");
$skipped++;
continue;
}
$year = !empty($blurayData['year']) ? $blurayData['year'] : (!empty($movieCoversData['year']) ? $movieCoversData['year'] : '');
$format = !empty($blurayData['format']) ? $blurayData['format'] : detectFormat($title);
$publisher = !empty($blurayData['publisher']) ? $blurayData['publisher'] : (!empty($movieCoversData['publisher']) ? $movieCoversData['publisher'] : '');
$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'] : '');
// 4. Fallback TMDB si données manquantes
if (empty($poster) || empty($director) || empty($actors) || empty($desc) || empty($year)) {
error_log("Import: 🔄 Fallback TMDB pour '$title'");
$tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
foreach ($items as $item) {
$ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? ''));
$csvTitle = trim($item['title'] ?? '');
if (strlen($ean) < 8 && empty($csvTitle)) {
$skipped++;
continue;
}
// 1. Essayer Blu-ray.com (avec throttling)
$blurayData = !empty($ean) ? fetchFromBlurayCom($ean) : [];
// 2. Si Blu-ray.com échoue, essayer MovieCovers avec le titre CSV
if (empty($blurayData['title']) && !empty($csvTitle)) {
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
$title = !empty($blurayData['title']) ? $blurayData['title'] : $csvTitle;
if (empty($title)) {
error_log("Import: ❌ Pas de titre pour EAN $ean - ignoré");
$skipped++;
continue;
}
$year = $blurayData['year'] ?? '';
$format = $blurayData['format'] ?: detectFormat($title);
$publisher = $blurayData['publisher'] ?? '';
$discs = $blurayData['number_of_discs'] ?: 1;
$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
if (empty($poster) || empty($director) || empty($actors) || empty($desc)) {
error_log("Import: 🔄 Fallback TMDB pour '$title'");
$tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
if (empty($poster) || $poster === 'assets/img/default_physical_media.jpg') {
$poster = $tmdb['poster'];
}
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) - Disques: $discs, Format: $format");
if (empty($poster) || $poster === 'assets/img/default_physical_media.jpg') {
$poster = $tmdb['poster'];
}
} 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é)
$stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)