Actualiser api.php

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