Actualiser api.php

This commit is contained in:
2026-06-26 09:35:10 +02:00
parent 9eeb97a661
commit 480ccdebdf
+94 -63
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
$frenchPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'fr'; });
if (!empty($frenchPosters)) {
usort($frenchPosters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); });
$poster = $frenchPosters[0]['url'];
} }
// Priorité 2 : Langue Anglaise // Extraire le lien vers la page du produit
if (empty($poster)) { if (preg_match('/<a href="\/products\/\?id=(\d+)"[^>]*>/i', $searchRes, $matches)) {
$englishPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'en'; }); $productId = $matches[1];
if (!empty($englishPosters)) { $productUrl = "https://www.blu-ray.com/products/?id=" . $productId;
usort($englishPosters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); });
$poster = $englishPosters[0]['url'];
}
}
// Priorité 3 : N'importe quelle jaquette dispo // 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');
usort($posters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); });
$poster = $posters[0]['url'];
}
}
if (empty($poster)) { if ($productRes) {
$poster = $defaultPoster; // Extraire l'image principale
} 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 [ return [
'poster' => $poster, 'poster' => $posterUrl,
'title' => $cleanTitle, 'title' => $title,
'format' => $format, 'format' => 'Blu-ray'
]; ];
}
}
}
// Si pas trouvé par EAN, essayer 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');
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'
];
}
}
}
return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray'];
} }
// Fonction pour vérifier si une URL existe // Fonction pour vérifier si une URL existe
@@ -495,7 +496,18 @@ case 'get_config_keys':
$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'] ?? '';
@@ -504,12 +516,31 @@ case 'get_config_keys':
$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
$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++; $imported++;
} }
} else { } else {