diff --git a/api.php b/api.php index f360c9e..7d17900 100644 --- a/api.php +++ b/api.php @@ -137,15 +137,13 @@ function fetchUPCitemdb($ean, $pdo) { return null; } -// ── 1.5 API DVDFr (Spécial Jaquettes & Synopsis FR) ── +// ── 1.5 API DVDFr (Uniquement Jaquettes FR) ── function fetchDVDFr($ean, $pdo) { if (empty($ean) || strlen($ean) < 8) return null; - - // 🔥 On change la clé de cache (v2) pour forcer le système à oublier les mauvais textes - $cacheKey = 'dvdfr_data_v2_' . md5($ean); + $cacheKey = 'dvdfr_' . md5($ean); // On revient au cache simple $cached = getCache($pdo, $cacheKey); - if ($cached) return json_decode($cached, true); + if ($cached) return $cached; $url = "https://www.dvdfr.com/api/search.php?gencode=" . urlencode($ean); $res = httpGet($url, 5); @@ -153,39 +151,12 @@ function fetchDVDFr($ean, $pdo) { try { $xml = @simplexml_load_string($res); - if ($xml && isset($xml->dvd) && isset($xml->dvd[0])) { - $dvd = $xml->dvd[0]; - $poster = isset($dvd->cover) ? (string)$dvd->cover : ''; - - $synopsis = isset($dvd->resume) ? trim(strip_tags((string)$dvd->resume)) : ''; - - // 1. Correction du bug d'encodage (les fameux "é") - if (strpos($synopsis, 'Ã') !== false) { - // Rétablit les vrais accents français - $synopsis = utf8_decode($synopsis); + if ($xml && isset($xml->dvd) && isset($xml->dvd[0]->cover)) { + $poster = (string)$xml->dvd[0]->cover; + if (!empty($poster)) { + setCache($pdo, $cacheKey, $poster, 'dvdfr'); + return $poster; } - - // 2. Filtre anti-pollution (Vendeurs / Annonces) - $synopsisLower = strtolower($synopsis); - $badWords = ['sous blister', 'vendeur', 'colissimo', 'expédition', 'livraison', 'ouvrables', 'garantie', 'produit neuf']; - $isGarbage = false; - - foreach ($badWords as $word) { - if (strpos($synopsisLower, $word) !== false) { - $isGarbage = true; - break; - } - } - - // Si c'est une annonce vendeur OU que le texte est anormalement court (< 30 caractères) - if ($isGarbage || strlen($synopsis) < 30) { - $synopsis = ''; // On vide le champ ! - } - - $data = ['poster' => $poster, 'synopsis' => $synopsis]; - - setCache($pdo, $cacheKey, json_encode($data), 'dvdfr'); - return $data; } } catch (\Exception $e) { return null; @@ -466,25 +437,45 @@ switch ($action) { if (empty($format) || $format === 'Blu-ray') $format = $upcData['format']; } - // 🔥 NOUVEAU : 1.5 DVDFr (Jaquette FR et Synopsis) - $dvdfrData = fetchDVDFr($ean, $pdo); - if (!empty($dvdfrData)) { - if (!empty($dvdfrData['poster'])) { - $poster = $dvdfrData['poster']; - } - if (!empty($dvdfrData['synopsis'])) { - $description = $dvdfrData['synopsis']; // Injecte le résumé du DVD/Coffret + // 🔥 1.5 DVDFr (Récupère uniquement la vraie jaquette FR) + $dvdfrCover = fetchDVDFr($ean, $pdo); + if (!empty($dvdfrCover)) { + $poster = $dvdfrCover; + } + + // 2. TMDB (Données Officielles & Synopsis) + if ($tmdbApiKey && !empty($title)) { + // Premier essai classique + $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); + + // 🔥 NOUVEAU : Si TMDB ne trouve rien (cas des Coffrets ou titres multiples) + if (!$tmdbData || empty($tmdbData['overview'])) { + $cleanTitle = $title; + + // A. Supprime les mots parasites + $cleanTitle = preg_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle); + + // B. Sépare les listes de films (ex: "Alien / Aliens" ou "Batman + Batman Le Défi") + $cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0]; + + // C. Sépare les tirets longs (ex: "Le Parrain - L'intégrale") + $cleanTitle = explode(' - ', $cleanTitle)[0]; + + $cleanTitle = trim($cleanTitle); + + // Si le titre a été nettoyé, on relance TMDB avec le titre du 1er film + if (!empty($cleanTitle) && $cleanTitle !== $title) { + // On omet volontairement l'année, car l'année du coffret n'est pas celle du 1er film + $tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo); + if ($tmdbFallback && !empty($tmdbFallback['overview'])) { + $tmdbData = $tmdbFallback; + } } } - } - - // 2. TMDB (Écrase les données CSV avec les données officielles) - if ($tmdbApiKey && !empty($title)) { - $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); + if ($tmdbData) { if (!empty($tmdbData['title'])) $title = $tmdbData['title']; - // 🔥 FIX : Gestion intelligente des réalisateurs (Coffrets / Anthologies) if (empty($director)) { $director = $tmdbData['director'] ?? ''; } elseif (!empty($tmdbData['director']) && strpos($director, ',') === false && strpos($tmdbData['director'], ',') !== false) { @@ -494,19 +485,20 @@ switch ($action) { if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year']; if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length']; - // 👇 LIGNE DÉSACTIVÉE ICI UNIQUEMENT POUR LA VIDÉOTHÈQUE 👇 + // LIGNE DÉSACTIVÉE : TMDB ne touche jamais à l'affiche de la vidéothèque // if (empty($poster) && !empty($tmdbData['poster'])) $poster = $tmdbData['poster']; - // 🔥 Récupération du Synopsis officiel (Seulement si DVDFr n'a rien trouvé) - if (empty($description) && !empty($tmdbData['overview'])) { + // 🔥 Récupération du Synopsis via TMDB (Même pour le 1er film d'un coffret) + if (!empty($tmdbData['overview'])) { $description = $tmdbData['overview']; } - // 🔥 Récupération des Acteurs officiels (Top 4) + + // Récupération des Acteurs officiels (Top 4) if (!empty($tmdbData['cast'])) { $actors = implode(', ', $tmdbData['cast']); } } - } // <--- ✅ Fermeture correcte de if ($tmdbApiKey) + } // 🔥 L'INSERT SQL DOIT ÊTRE ICI (EN DEHORS DU IF TMDB) $sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE director=IF(VALUES(director)!='',VALUES(director),director), poster=IF(VALUES(poster)!='',VALUES(poster),poster), format=IF(VALUES(format)!='',VALUES(format),format), length=IF(VALUES(length)!='',VALUES(length),length), publisher=IF(VALUES(publisher)!='',VALUES(publisher),publisher), ean_isbn13=IF(VALUES(ean_isbn13)!='',VALUES(ean_isbn13),ean_isbn13), number_of_discs=IF(VALUES(number_of_discs)!=1,VALUES(number_of_discs),number_of_discs), aspect_ratio=IF(VALUES(aspect_ratio)!='',VALUES(aspect_ratio),aspect_ratio), description=IF(VALUES(description)!='',VALUES(description),description), actors=IF(VALUES(actors)!='',VALUES(actors),actors)"; $stmt = $pdo->prepare($sql);