From ae926596329c35b2cdfc50857faede09ee89d28b Mon Sep 17 00:00:00 2001 From: Cedric Date: Fri, 26 Jun 2026 09:54:57 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 181 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 89 insertions(+), 92 deletions(-) diff --git a/api.php b/api.php index 9dc8ceb..ede052d 100644 --- a/api.php +++ b/api.php @@ -124,65 +124,60 @@ function extractYear($dateStr) { function fetchBluRayCom($ean, $title = '', $year = '', $pdo = null) { $defaultPoster = 'assets/img/default_physical_media.jpg'; - if (empty($ean)) { + if (empty($ean) && empty($title)) { return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray']; } - // Nettoyer l'EAN (supprimer les espaces, tirets, etc.) - $cleanEan = preg_replace('/[^0-9]/', '', $ean); - - if (empty($cleanEan)) { - return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray']; + // Nettoyer l'EAN + $cleanEan = ''; + if (!empty($ean)) { + $cleanEan = preg_replace('/[^0-9]/', '', $ean); } - // Recherche sur blu-ray.com par EAN/UPC - $searchUrl = "https://www.blu-ray.com/search/?quicksearch=1&searchtype=products&q=" . urlencode($cleanEan); + $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'; - $searchRes = httpGet($searchUrl, 5, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'); - - if (!$searchRes) { - return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray']; - } - - // Extraire le lien vers la page du produit - if (preg_match('/]*>/i', $searchRes, $matches)) { - $productId = $matches[1]; - $productUrl = "https://www.blu-ray.com/products/?id=" . $productId; + // Recherche par EAN/UPC d'abord + if (!empty($cleanEan)) { + $searchUrl = "https://www.blu-ray.com/search/?quicksearch=1&searchtype=products&q=" . urlencode($cleanEan); + $searchRes = httpGet($searchUrl, 5, $userAgent); - // Récupérer la page du produit - $productRes = httpGet($productUrl, 5, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'); - - if ($productRes) { - // Extraire l'image principale - if (preg_match('/]*src="([^"]*\/movies\/pictures\/[^"]*)"/i', $productRes, $imgMatches)) { - $posterUrl = $imgMatches[1]; - - // Convertir en haute résolution si possible - $posterUrl = str_replace('/resized/', '/', $posterUrl); - - return [ - 'poster' => $posterUrl, - 'title' => $title, - 'format' => 'Blu-ray' - ]; + if ($searchRes && preg_match('/]*>/i', $searchRes, $matches)) { + $productId = $matches[1]; + $productUrl = "https://www.blu-ray.com/products/?id=" . $productId; + + $productRes = httpGet($productUrl, 5, $userAgent); + + if ($productRes) { + // Extraire l'image principale + if (preg_match('/]*id="home_release_img"[^>]*src="([^"]+)"/i', $productRes, $imgMatches)) { + $posterUrl = $imgMatches[1]; + // Convertir en haute résolution + $posterUrl = str_replace('/resized/', '/', $posterUrl); + + return [ + 'poster' => $posterUrl, + 'title' => $title, + 'format' => 'Blu-ray' + ]; + } } } } - // Si pas trouvé par EAN, essayer par titre + // Fallback : recherche par titre if (!empty($title)) { $cleanTitle = cleanTitle($title); $searchUrl = "https://www.blu-ray.com/search/?quicksearch=1&searchtype=movies&q=" . urlencode($cleanTitle); - $searchRes = httpGet($searchUrl, 5, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'); + $searchRes = httpGet($searchUrl, 5, $userAgent); if ($searchRes && preg_match('/]*>/i', $searchRes, $matches)) { $movieId = $matches[1]; $movieUrl = "https://www.blu-ray.com/movies/?id=" . $movieId; - $movieRes = httpGet($movieUrl, 5, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'); + $movieRes = httpGet($movieUrl, 5, $userAgent); - if ($movieRes && preg_match('/]*src="([^"]*\/movies\/pictures\/[^"]*)"/i', $movieRes, $imgMatches)) { + if ($movieRes && preg_match('/]*id="home_release_img"[^>]*src="([^"]+)"/i', $movieRes, $imgMatches)) { $posterUrl = $imgMatches[1]; $posterUrl = str_replace('/resized/', '/', $posterUrl); @@ -198,6 +193,8 @@ function fetchBluRayCom($ean, $title = '', $year = '', $pdo = null) { return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray']; } +// ── ROUTEUR PRINCIPAL ── + // Fonction pour vérifier si une URL existe function urlExists($url, $timeout = 3) { if (!function_exists('curl_init')) { @@ -487,63 +484,63 @@ case 'get_config_keys': break; case 'import_batch': - checkAuth($pdo); - $data = json_decode(file_get_contents("php://input"), true); - $type = $data['type'] ?? 'critique'; - $items = $data['items'] ?? []; - - $pdo->beginTransaction(); - $imported = 0; - - if ($type === 'videotheque') { - $stmtVideo = $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 - ean_isbn13 = VALUES(ean_isbn13), - poster = VALUES(poster), - description = VALUES(description), - format = VALUES(format), - length = VALUES(length), - number_of_discs = VALUES(number_of_discs), - aspect_ratio = VALUES(aspect_ratio), - actors = VALUES(actors), - publisher = VALUES(publisher)"); + checkAuth($pdo); + $data = json_decode(file_get_contents("php://input"), true); + $type = $data['type'] ?? 'critique'; + $items = $data['items'] ?? []; - foreach ($items as $item) { - $title = $item['title'] ?? ''; - if (empty($title)) continue; + $pdo->beginTransaction(); + $imported = 0; + + if ($type === 'videotheque') { + $stmtVideo = $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 + ean_isbn13 = VALUES(ean_isbn13), + poster = VALUES(poster), + description = VALUES(description), + format = VALUES(format), + length = VALUES(length), + number_of_discs = VALUES(number_of_discs), + aspect_ratio = VALUES(aspect_ratio), + actors = VALUES(actors), + publisher = VALUES(publisher)"); - $year = $item['year'] ?? ''; - $ean = $item['ean'] ?? ''; - $desc = $item['description'] ?? ''; - $length = $item['length'] ?? ''; - $discs = $item['number_of_discs'] ?? 1; - $aspect = $item['aspect_ratio'] ?? ''; - $actors = $item['actors'] ?? ''; - $publisher = $item['publisher'] ?? ''; - $director = $item['director'] ?? ''; - - $id = makeStableId('videotheque', $title, $year); - - // Utiliser blu-ray.com pour récupérer l'image - $bluRayData = fetchBluRayCom($ean, $title, $year, $pdo); - $poster = $bluRayData['poster']; - - // Déterminer le format - $format = 'Blu-ray'; - if (stripos($title, '4K') !== false || stripos($title, 'UHD') !== false) { - $format = '4K Ultra HD'; - } elseif (stripos($desc, 'DVD') !== false) { - $format = 'DVD'; + foreach ($items as $item) { + $title = $item['title'] ?? ''; + if (empty($title)) continue; + + $year = $item['year'] ?? ''; + $ean = $item['ean'] ?? ''; + $desc = $item['description'] ?? ''; + $length = $item['length'] ?? ''; + $discs = $item['number_of_discs'] ?? 1; + $aspect = $item['aspect_ratio'] ?? ''; + $actors = $item['actors'] ?? ''; + $publisher = $item['publisher'] ?? ''; + $director = $item['director'] ?? ''; + + $id = makeStableId('videotheque', $title, $year); + + // Utiliser blu-ray.com pour récupérer l'image + $bluRayData = fetchBluRayCom($ean, $title, $year, $pdo); + $poster = $bluRayData['poster']; + + // Déterminer le format + $format = 'Blu-ray'; + if (stripos($title, '4K') !== false || stripos($title, 'UHD') !== false) { + $format = '4K Ultra HD'; + } elseif (stripos($desc, 'DVD') !== false) { + $format = 'DVD'; + } + + $stmtVideo->execute([ + $id, $title, $year, $format, $poster, $ean, $desc, + $length, $discs, $aspect, $actors, $publisher, $director + ]); + $imported++; } - - $stmtVideo->execute([ - $id, $title, $year, $format, $poster, $ean, $desc, - $length, $discs, $aspect, $actors, $publisher, $director - ]); - $imported++; - } - } else { + } else { $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $review = $rowData['Review'] ?? $rowData['review'] ?? '';