Actualiser api.php

This commit is contained in:
2026-06-26 09:35:10 +02:00
parent 9eeb97a661
commit 480ccdebdf
+111 -80
View File
@@ -120,81 +120,82 @@ function extractYear($dateStr) {
return ''; return '';
} }
// ── 1. Fonction Fanart.tv EXCLUSIVE (Aucune jaquette TMDB ou OpenLibrary) ── // ── FONCTION POUR RÉCUPÉRER LES IMAGES DEPUIS BLU-RAY.COM ──
function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) { function fetchBluRayCom($ean, $title = '', $year = '', $pdo = null) {
// Image locale par défaut si aucun résultat
$defaultPoster = 'assets/img/default_physical_media.jpg'; $defaultPoster = 'assets/img/default_physical_media.jpg';
$cleanTitle = cleanTitle($title);
if (empty($cleanTitle)) return ['poster' => $defaultPoster, 'title' => $title, 'format' => $format]; if (empty($ean)) {
return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray'];
// Récupération des clés API
$apiKey = '70f9747dafe9b27f9b14ef80ee0cacc9';
if ($pdo) {
$stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = 'fanart_api_key'");
$stmt->execute();
$row = $stmt->fetch();
if ($row) $apiKey = decryptData($row['key_value']);
} }
$tmdbKey = getTmdbApiKey($pdo); // Nettoyer l'EAN (supprimer les espaces, tirets, etc.)
if (!$tmdbKey) return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => $format]; $cleanEan = preg_replace('/[^0-9]/', '', $ean);
// Étape A : Obtenir l'ID TMDB (Recherche silencieuse) if (empty($cleanEan)) {
$searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query=" . urlencode($cleanTitle); return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray'];
if (!empty($year)) $searchUrl .= "&year={$year}";
$searchUrl .= "&language=fr-FR";
$searchRes = httpGet($searchUrl, 5);
$searchData = $searchRes ? json_decode($searchRes, true) : [];
if (empty($searchData['results'])) {
return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => $format];
} }
$tmdbId = $searchData['results'][0]['id']; // Recherche sur blu-ray.com par EAN/UPC
$poster = null; $searchUrl = "https://www.blu-ray.com/search/?quicksearch=1&searchtype=products&q=" . urlencode($cleanEan);
// Étape B : Interrogation stricte de Fanart.tv $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');
$fanartUrl = "https://webservice.fanart.tv/v3/movies/{$tmdbId}?api_key={$apiKey}";
$fanartRes = httpGet($fanartUrl, 5);
$fanartData = $fanartRes ? json_decode($fanartRes, true) : [];
if (!isset($fanartData['error']) && !empty($fanartData['movieposter'])) { if (!$searchRes) {
$posters = $fanartData['movieposter']; return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray'];
}
// Priorité 1 : Langue Française // Extraire le lien vers la page du produit
$frenchPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'fr'; }); if (preg_match('/<a href="\/products\/\?id=(\d+)"[^>]*>/i', $searchRes, $matches)) {
if (!empty($frenchPosters)) { $productId = $matches[1];
usort($frenchPosters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); }); $productUrl = "https://www.blu-ray.com/products/?id=" . $productId;
$poster = $frenchPosters[0]['url'];
}
// Priorité 2 : Langue Anglaise // Récupérer la page du produit
if (empty($poster)) { $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');
$englishPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'en'; });
if (!empty($englishPosters)) { if ($productRes) {
usort($englishPosters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); }); // Extraire l'image principale
$poster = $englishPosters[0]['url']; if (preg_match('/<img[^>]*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'
];
} }
} }
}
// Priorité 3 : N'importe quelle jaquette dispo // Si pas trouvé par EAN, essayer par titre
if (empty($poster)) { if (!empty($title)) {
usort($posters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); }); $cleanTitle = cleanTitle($title);
$poster = $posters[0]['url']; $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');
if ($searchRes && preg_match('/<a href="\/movies\/\?id=(\d+)"[^>]*>/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');
if ($movieRes && preg_match('/<img[^>]*src="([^"]*\/movies\/pictures\/[^"]*)"/i', $movieRes, $imgMatches)) {
$posterUrl = $imgMatches[1];
$posterUrl = str_replace('/resized/', '/', $posterUrl);
return [
'poster' => $posterUrl,
'title' => $title,
'format' => 'Blu-ray'
];
}
} }
} }
if (empty($poster)) { return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray'];
$poster = $defaultPoster;
}
return [
'poster' => $poster,
'title' => $cleanTitle,
'format' => $format,
];
} }
// Fonction pour vérifier si une URL existe // Fonction pour vérifier si une URL existe
@@ -485,34 +486,64 @@ case 'get_config_keys':
else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); }
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(); $pdo->beginTransaction();
$imported = 0; $imported = 0;
if ($type === 'videotheque') { if ($type === 'videotheque') {
$stmtVideo = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description) VALUES (?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE ean_isbn13 = ?, poster = ?, description = ?"); $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) { foreach ($items as $item) {
$title = $item['title'] ?? ''; $title = $item['title'] ?? '';
if (empty($title)) continue; if (empty($title)) continue;
$year = $item['year'] ?? ''; $year = $item['year'] ?? '';
$ean = $item['ean'] ?? ''; $ean = $item['ean'] ?? '';
$desc = $item['description'] ?? ''; $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); $id = makeStableId('videotheque', $title, $year);
$fanartData = fetchFanartTv($title, $year, 'Blu-ray', $pdo);
$poster = $fanartData['poster'];
$stmtVideo->execute([$id, $title, $year, 'Blu-ray', $poster, $ean, $desc, $ean, $poster, $desc]); // Utiliser blu-ray.com pour récupérer l'image
$imported++; $bluRayData = fetchBluRayCom($ean, $title, $year, $pdo);
} $poster = $bluRayData['poster'];
} else {
// 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++;
}
} 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'] ?? '';