Actualiser api.php

This commit is contained in:
2026-07-01 11:35:07 +02:00
parent f539f0664c
commit 7edb9ae438
+74
View File
@@ -669,6 +669,80 @@ function fetchFromMovieCovers($title, $year = '') {
return $empty;
}
function removeAccents($string) {
$string = htmlentities($string, ENT_NOQUOTES, 'utf-8');
$string = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $string);
$string = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $string);
$string = preg_replace('#&[^;]+;#', '', $string);
return $string;
}
function fetchAndDownloadMovieCovers($title, $ean) {
if (empty($title) || empty($ean)) return null;
// Création du dossier d'images s'il n'existe pas
$dir = __DIR__ . '/../assets/img/covers';
if (!is_dir($dir)) mkdir($dir, 0777, true);
$localPath = "assets/img/covers/{$ean}.jpg";
$fullPath = __DIR__ . '/../' . $localPath;
// Si la jaquette a déjà été téléchargée lors d'un précédent import, on la réutilise direct
if (file_exists($fullPath)) return $localPath;
// Nettoyage du titre pour la recherche MovieCovers
$cleanTitle = removeAccents(trim(preg_replace('/(blu-ray|bluray|dvd|4k|ultra hd).*$/i', '', $title)));
$searchUrl = "https://www.moviecovers.com/multicrit.html?titre=" . urlencode(utf8_decode($cleanTitle));
$html = file_get_contents($searchUrl, false, stream_context_create([
'http' => ['method' => 'GET', 'header' => "Referer: https://www.moviecovers.com/\r\nUser-Agent: Mozilla/5.0\r\n", 'timeout' => 10]
]));
if (!$html) return null;
$filmHtml = null;
if (preg_match('/href=["\']?\/?(film\/titre_[^"\']+)\.html["\']?/i', $html, $m)) {
$filmUrl = "https://www.moviecovers.com/" . $m[1] . ".html";
usleep(500000); // Pause de 0.5s pour ne pas spammer le serveur
$filmHtml = file_get_contents($filmUrl, false, stream_context_create([
'http' => ['method' => 'GET', 'header' => "Referer: https://www.moviecovers.com/\r\nUser-Agent: Mozilla/5.0\r\n", 'timeout' => 10]
]));
} else if (stripos($html, 'IDMC') !== false) {
$filmHtml = $html; // Accès direct
}
if (!$filmHtml) return null;
// Extraction de l'identifiant IDMC
$idmc = null;
if (preg_match('/IDMC.*?<PRE>([^<]+)<\/PRE>/is', $filmHtml, $m)) {
$idmc = trim($m[1]);
} elseif (preg_match('/<meta property="og:image" content="[^"]*\/getjpg\.html\/([^"\']+)\.jpg"/i', $filmHtml, $m)) {
$idmc = urldecode($m[1]);
}
if ($idmc) {
$idmc_encoded = rawurlencode($idmc);
$coverUrls = [
"https://www.moviecovers.com/DATA/zipcache/{$idmc_encoded}.jpg",
"https://www.moviecovers.com/getjpg.html/{$idmc_encoded}.jpg"
];
foreach ($coverUrls as $imgUrl) {
usleep(1000000); // Pause de 1s avant de télécharger l'image (comme dans le script Python)
$imgData = @file_get_contents($imgUrl, false, stream_context_create([
'http' => ['method' => 'GET', 'header' => "Referer: https://www.moviecovers.com/\r\nUser-Agent: Mozilla/5.0\r\n", 'timeout' => 15]
]));
// Si l'image est valide et fait plus de 5Ko
if ($imgData && strlen($imgData) > 5000) {
file_put_contents($fullPath, $imgData);
return $localPath;
}
}
}
return null;
}
// ── ROUTEUR PRINCIPAL ──
$action = $_GET['action'] ?? '';
$data = json_decode(file_get_contents('php://input'), true) ?? [];