diff --git a/api.php b/api.php index 8516fce..a485b5f 100644 --- a/api.php +++ b/api.php @@ -25,8 +25,6 @@ try { $pdo->exec("CREATE TABLE IF NOT EXISTS videotheque (id BIGINT PRIMARY KEY, title VARCHAR(255) NOT NULL, year VARCHAR(10), director VARCHAR(255), poster TEXT, format VARCHAR(50), length VARCHAR(50), publisher VARCHAR(255), ean_isbn13 VARCHAR(50), number_of_discs INT DEFAULT 1, aspect_ratio VARCHAR(50), description TEXT, actors TEXT)"); try { $pdo->exec("ALTER TABLE videotheque ADD COLUMN actors TEXT AFTER description"); } catch (\Exception $e) {} - try { $pdo->exec("DROP TABLE IF EXISTS cache_api"); } catch (\Exception $e) {} - } catch (\PDOException $e) { echo json_encode(["error" => "Erreur BDD : " . $e->getMessage()]); exit; } // ── FONCTIONS UTILITAIRES ── @@ -117,33 +115,44 @@ function extractYear($dateStr) { return ''; } -// ── CoverCentury.com (jaquettes HD pour vidéothèque) ── -function fetchCoverCentury($title, $year = '', $format = 'bluray') { +// ── DVDcover.com (version française) ── +function fetchDVDCover($title, $year = '', $format = 'bluray') { if (empty($title)) return null; $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'; $cleanTitle = cleanTitle($title); - // Mapping des formats pour CoverCentury + // Mapping des formats pour DVDcover $formatMap = [ - '4k ultra hd' => '4k', - '4k' => '4k', - 'blu-ray' => 'bluray', - 'bluray' => 'bluray', + '4k ultra hd' => '4k-ultra-hd', + '4k' => '4k-ultra-hd', + 'blu-ray' => 'blu-ray', + 'bluray' => 'blu-ray', 'dvd' => 'dvd', ]; - $ccFormat = $formatMap[strtolower($format)] ?? 'bluray'; + $dcFormat = $formatMap[strtolower($format)] ?? 'blu-ray'; - // URL de recherche CoverCentury - $searchUrl = "https://www.covercentury.com/search?q=" . urlencode($cleanTitle); + // Construction de l'URL de recherche + // DVDcover utilise des URLs comme : https://www.dvdcover.com/search/TITLE/year/FORMAT + $searchPath = str_replace(' ', '-', strtolower($cleanTitle)); + $searchPath = preg_replace('/[^a-z0-9-]/', '', $searchPath); + + $searchUrl = "https://www.dvdcover.com/{$dcFormat}/search/{$searchPath}"; if (!empty($year)) { - $searchUrl .= "+(" . $year . ")"; + $searchUrl .= "/{$year}"; } $html = httpGet($searchUrl, 8, $ua); + + // Fallback : essayer sans l'année if (!$html) { - error_log("CoverCentury: Échec recherche pour '$title'"); + $searchUrl = "https://www.dvdcover.com/{$dcFormat}/search/{$searchPath}"; + $html = httpGet($searchUrl, 8, $ua); + } + + if (!$html) { + error_log("DVDCover: Échec recherche pour '$title' sur $searchUrl"); return null; } @@ -153,42 +162,44 @@ function fetchCoverCentury($title, $year = '', $format = 'bluray') { 'format' => $format, ]; - // Extraction des liens d'images - CoverCentury utilise des structures spécifiques + // DVDcover structure les covers dans des divs spécifiques // Chercher les liens vers les pages de covers - preg_match_all('/href=["\']([^"\']*\/cover\/[^"\']+)["\']/i', $html, $coverLinks); + preg_match_all('/href=["\']([^"\']*\/covers?\/[^"\']+)["\']/i', $html, $coverLinks); if (!empty($coverLinks[1])) { - // Prendre le premier résultat pertinent + // Prendre le premier résultat $coverPage = $coverLinks[1][0]; if (strpos($coverPage, 'http') !== 0) { - $coverPage = 'https://www.covercentury.com' . $coverPage; + $coverPage = 'https://www.dvdcover.com' . $coverPage; } - // Récupérer la page du cover pour avoir l'image en haute qualité + // Récupérer la page du cover $coverHtml = httpGet($coverPage, 8, $ua); if ($coverHtml) { - // Chercher l'image principale en haute résolution - if (preg_match('/]+src=["\']([^"\']+\.jpg)["\'][^>]*class=["\'][^"\']*main[^"\']*["\']/i', $coverHtml, $m)) { + // Chercher l'image principale (cover front) + // DVDcover utilise des classes comme "cover-front", "cover-image" + if (preg_match('/]+src=["\']([^"\']+\.jpg)["\'][^>]*class=["\'][^"\']*cover[^"\']*["\']/i', $coverHtml, $m)) { $result['poster'] = $m[1]; } elseif (preg_match('/]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']/i', $coverHtml, $m)) { $result['poster'] = $m[1]; - } elseif (preg_match('/href=["\']([^"\']*\/images\/[^"\']+\.jpg)["\']/i', $coverHtml, $m)) { + } elseif (preg_match('/href=["\']([^"\']*\/images\/covers?\/[^"\']+\.jpg)["\']/i', $coverHtml, $m)) { $result['poster'] = $m[1]; } // Extraire le titre - if (preg_match('/]*>([^<]+) - CoverCentury/i', $coverHtml, $titleMatch)) { + if (preg_match('/]*>([^<]+) - DVDCover/i', $coverHtml, $titleMatch)) { $result['title'] = trim($titleMatch[1]); } } } - // Fallback : chercher directement les images dans la page de résultats + // Fallback : chercher directement dans la page de résultats if (empty($result['poster'])) { + // Chercher les images de covers dans les résultats de recherche preg_match_all('/]+src=["\']([^"\']+\.jpg)["\'][^>]*>/i', $html, $imgMatches); if (!empty($imgMatches[1])) { foreach ($imgMatches[1] as $img) { - if (strpos($img, 'covercentury') !== false || strpos($img, '/covers/') !== false) { + if (strpos($img, 'dvdcover') !== false || strpos($img, '/covers/') !== false) { $result['poster'] = $img; break; } @@ -346,7 +357,7 @@ switch ($action) { 'length' => '', 'number_of_discs' => 1, 'aspect_ratio' => '', 'actors' => '' ]; - // Pour CoverCentury, on a besoin d'un titre + // Pour DVDcover, on a besoin d'un titre $tmdbKey = getTmdbApiKey($pdo); $titleForSearch = ''; @@ -372,12 +383,12 @@ switch ($action) { // Récupération jaquette if ($type === 'videotheque') { - // CoverCentury pour vidéothèque + // DVDcover pour vidéothèque $format = $result['format'] ?: 'Blu-ray'; - $ccData = fetchCoverCentury($titleForSearch, $result['year'], $format); - if (!empty($ccData)) { - if (!empty($ccData['poster'])) $result['poster'] = $ccData['poster']; - if (!empty($ccData['title'])) $result['title'] = $ccData['title']; + $dcData = fetchDVDCover($titleForSearch, $result['year'], $format); + if (!empty($dcData)) { + if (!empty($dcData['poster'])) $result['poster'] = $dcData['poster']; + if (!empty($dcData['title'])) $result['title'] = $dcData['title']; $result['format'] = $format; } } else { @@ -420,12 +431,12 @@ switch ($action) { $stmt = $pdo->prepare($sql); $stmt->execute([$id, $data['title'] ?? '', $data['year'] ?? '', $data['director'] ?? '', $data['poster'] ?? '', $data['rating'] ?? 3.0, $data['review'] ?? '', $streaming]); } else { - // Vidéothèque : CoverCentury pour la jaquette + // Vidéothèque : DVDcover pour la jaquette if (empty($data['poster']) && !empty($data['title'])) { $format = $data['format'] ?: 'Blu-ray'; - $ccData = fetchCoverCentury($data['title'], $data['year'] ?? '', $format); - if (!empty($ccData['poster'])) { - $data['poster'] = $ccData['poster']; + $dcData = fetchDVDCover($data['title'], $data['year'] ?? '', $format); + if (!empty($dcData['poster'])) { + $data['poster'] = $dcData['poster']; } } @@ -470,7 +481,7 @@ switch ($action) { $id = makeStableId($type, $title, $year); if ($type === 'videotheque') { - // ── VIDÉOTHÈQUE : CoverCentury pour jaquette HD ── + // ── VIDÉOTHÈQUE : DVDcover pour jaquette HD ── $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; $actors = ''; if (!empty($csvActors)) { @@ -502,16 +513,16 @@ switch ($action) { $poster = $rowData['poster'] ?? ''; $director = ''; - // Récupération jaquette via CoverCentury - $cleanTitleForCC = cleanTitle($title); - if (!empty($cleanTitleForCC)) { - $ccData = fetchCoverCentury($cleanTitleForCC, $year, $format); - if (!empty($ccData)) { - if (!empty($ccData['poster'])) { - $poster = $ccData['poster']; + // Récupération jaquette via DVDcover + $cleanTitleForDC = cleanTitle($title); + if (!empty($cleanTitleForDC)) { + $dcData = fetchDVDCover($cleanTitleForDC, $year, $format); + if (!empty($dcData)) { + if (!empty($dcData['poster'])) { + $poster = $dcData['poster']; } - if (!empty($ccData['title']) && ($title === 'Sans titre' || empty($title))) { - $title = $ccData['title']; + if (!empty($dcData['title']) && ($title === 'Sans titre' || empty($title))) { + $title = $dcData['title']; } } } @@ -581,7 +592,7 @@ switch ($action) { } break; - case 'debug_covercentury': + case 'debug_dvdcover': $title = $_GET['title'] ?? ''; $year = $_GET['year'] ?? ''; $format = $_GET['format'] ?? 'Blu-ray'; @@ -589,7 +600,7 @@ switch ($action) { if (!$title) { echo json_encode(['error' => 'Titre manquant']); exit; } $result = ['title' => $title, 'year' => $year, 'format' => $format]; - $data = fetchCoverCentury($title, $year, $format); + $data = fetchDVDCover($title, $year, $format); $result['data'] = $data; $result['status'] = $data ? 'OK' : 'AUCUN_RÉSULTAT';