diff --git a/api.php b/api.php index 749d56a..1bc1270 100644 --- a/api.php +++ b/api.php @@ -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>/is', $filmHtml, $m)) {
+ $idmc = trim($m[1]);
+ } elseif (preg_match('/ ['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) ?? [];