diff --git a/api.php b/api.php index 7d02780..9dc8ceb 100644 --- a/api.php +++ b/api.php @@ -120,81 +120,82 @@ function extractYear($dateStr) { return ''; } -// ── 1. Fonction Fanart.tv EXCLUSIVE (Aucune jaquette TMDB ou OpenLibrary) ── -function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) { - // Image locale par défaut si aucun résultat +// ── FONCTION POUR RÉCUPÉRER LES IMAGES DEPUIS BLU-RAY.COM ── +function fetchBluRayCom($ean, $title = '', $year = '', $pdo = null) { $defaultPoster = 'assets/img/default_physical_media.jpg'; - $cleanTitle = cleanTitle($title); - if (empty($cleanTitle)) return ['poster' => $defaultPoster, 'title' => $title, 'format' => $format]; - - // 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']); + if (empty($ean)) { + return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray']; } - $tmdbKey = getTmdbApiKey($pdo); - if (!$tmdbKey) return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => $format]; + // Nettoyer l'EAN (supprimer les espaces, tirets, etc.) + $cleanEan = preg_replace('/[^0-9]/', '', $ean); - // Étape A : Obtenir l'ID TMDB (Recherche silencieuse) - $searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query=" . urlencode($cleanTitle); - 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]; + if (empty($cleanEan)) { + return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray']; } - $tmdbId = $searchData['results'][0]['id']; - $poster = null; + // Recherche sur blu-ray.com par EAN/UPC + $searchUrl = "https://www.blu-ray.com/search/?quicksearch=1&searchtype=products&q=" . urlencode($cleanEan); - // Étape B : Interrogation stricte de Fanart.tv - $fanartUrl = "https://webservice.fanart.tv/v3/movies/{$tmdbId}?api_key={$apiKey}"; - $fanartRes = httpGet($fanartUrl, 5); - $fanartData = $fanartRes ? json_decode($fanartRes, true) : []; + $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 (!isset($fanartData['error']) && !empty($fanartData['movieposter'])) { - $posters = $fanartData['movieposter']; + 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; - // 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']; - } + // 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'); - // Priorité 2 : Langue Anglaise - if (empty($poster)) { - $englishPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'en'; }); - if (!empty($englishPosters)) { - usort($englishPosters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); }); - $poster = $englishPosters[0]['url']; + 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' + ]; } } + } + + // 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); - // Priorité 3 : N'importe quelle jaquette dispo - if (empty($poster)) { - usort($posters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); }); - $poster = $posters[0]['url']; + $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('/]*>/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('/]*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)) { - $poster = $defaultPoster; - } - - return [ - 'poster' => $poster, - 'title' => $cleanTitle, - 'format' => $format, - ]; + return ['poster' => $defaultPoster, 'title' => $title, 'format' => 'Blu-ray']; } // 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é."]); } break; - case 'import_batch': - checkAuth($pdo); - $data = json_decode(file_get_contents("php://input"), true); - $type = $data['type'] ?? 'critique'; - $items = $data['items'] ?? []; + 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)"); + + foreach ($items as $item) { + $title = $item['title'] ?? ''; + if (empty($title)) continue; - $pdo->beginTransaction(); - $imported = 0; + $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'] ?? ''; - 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 = ?"); - - foreach ($items as $item) { - $title = $item['title'] ?? ''; - if (empty($title)) continue; - - $year = $item['year'] ?? ''; - $ean = $item['ean'] ?? ''; - $desc = $item['description'] ?? ''; - - $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]); - $imported++; - } - } else { + $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++; + } + } else { $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $review = $rowData['Review'] ?? $rowData['review'] ?? '';