Actualiser api.php

This commit is contained in:
2026-06-24 13:23:48 +02:00
parent a2d174df75
commit ab9e60b5dd
+9 -48
View File
@@ -25,7 +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)"); $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("ALTER TABLE videotheque ADD COLUMN actors TEXT AFTER description"); } catch (\Exception $e) {}
// 🔥 SUPPRESSION DE LA TABLE CACHE SI ELLE EXISTE ENCORE
try { $pdo->exec("DROP TABLE IF EXISTS cache_api"); } 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; } } catch (\PDOException $e) { echo json_encode(["error" => "Erreur BDD : " . $e->getMessage()]); exit; }
@@ -106,32 +105,12 @@ function extractYear($dateStr) {
return ''; return '';
} }
// ── 1. API UPCitemDB (SANS CACHE) ── // ── 1. API DVDFr (SANS CACHE) ──
function fetchUPCitemdb($ean, $pdo) {
if (empty($ean) || strlen($ean) < 8) return null;
$url = "https://api.upcitemdb.com/prod/trial/lookup?upc=" . urlencode($ean);
$res = httpGet($url, 5);
if (!$res) return null;
$data = json_decode($res, true);
if (isset($data['code']) && $data['code'] === 'OK' && !empty($data['items'])) {
$item = $data['items'][0];
return [
'title' => $item['title'] ?? '',
'poster' => $item['images'][0] ?? '',
'publisher' => $item['brand'] ?? $item['publisher'] ?? '',
'format' => detectFormat($item['title'] ?? '', $item['description'] ?? '')
];
}
return null;
}
// ── 2. API DVDFr (SANS CACHE - Récupération complète) ──
function fetchDVDFr($ean, $pdo) { function fetchDVDFr($ean, $pdo) {
if (empty($ean) || strlen($ean) < 8) return null; if (empty($ean) || strlen($ean) < 8) return null;
$ua = 'MonCinema/1.0 (collection privée; contact@moncineapp.fr)'; $ua = 'MonCinema/1.0 (collection privée; contact@moncineapp.fr)';
// Étape 1 : recherche par gencode → récupère l'id DVDFr
$searchUrl = "https://www.dvdfr.com/api/search.php?gencode=" . urlencode($ean); $searchUrl = "https://www.dvdfr.com/api/search.php?gencode=" . urlencode($ean);
$ch = curl_init($searchUrl); $ch = curl_init($searchUrl);
curl_setopt_array($ch, [ curl_setopt_array($ch, [
@@ -155,7 +134,6 @@ function fetchDVDFr($ean, $pdo) {
$dvdId = (string)$xml->dvd[0]->id; $dvdId = (string)$xml->dvd[0]->id;
if (empty($dvdId)) return null; if (empty($dvdId)) return null;
// Étape 2 : fiche complète via dvd.php?id=
$ficheUrl = "https://www.dvdfr.com/api/dvd.php?id=" . urlencode($dvdId); $ficheUrl = "https://www.dvdfr.com/api/dvd.php?id=" . urlencode($dvdId);
$ch2 = curl_init($ficheUrl); $ch2 = curl_init($ficheUrl);
curl_setopt_array($ch2, [ curl_setopt_array($ch2, [
@@ -178,12 +156,10 @@ function fetchDVDFr($ean, $pdo) {
$dvd = $fiche->dvd[0]; $dvd = $fiche->dvd[0];
// Extraction de la jaquette
$poster = ''; $poster = '';
if (isset($dvd->cover)) $poster = (string)$dvd->cover; if (isset($dvd->cover)) $poster = (string)$dvd->cover;
if (empty($poster) && isset($dvd->covers->cover[0])) $poster = (string)$dvd->covers->cover[0]; if (empty($poster) && isset($dvd->covers->cover[0])) $poster = (string)$dvd->covers->cover[0];
// Extraction des métadonnées techniques
$result = [ $result = [
'poster' => $poster, 'poster' => $poster,
'publisher' => isset($dvd->editeur) ? (string)$dvd->editeur : '', 'publisher' => isset($dvd->editeur) ? (string)$dvd->editeur : '',
@@ -196,19 +172,17 @@ function fetchDVDFr($ean, $pdo) {
return (!empty($result['poster']) || !empty($result['publisher'])) ? $result : null; return (!empty($result['poster']) || !empty($result['publisher'])) ? $result : null;
} }
// ── 2. API TMDB (SANS CACHE - TITRE FRANÇAIS) ──
function fetchTMDBFull($title, $year, $apiKey, $pdo) { function fetchTMDBFull($title, $year, $apiKey, $pdo) {
if (empty($apiKey) || empty($title)) return null; if (empty($apiKey) || empty($title)) return null;
$cleanTitle = cleanTitle($title); $cleanTitle = cleanTitle($title);
$cacheKey = 'tmdb_full_' . md5(strtolower($cleanTitle) . '|' . $year);
$cached = getCache($pdo, $cacheKey);
if ($cached) return $cached;
// 1. Recherche avec l'année // 🔥 SUPPRESSION DES APPELS À getCache() QUI N'EXISTE PLUS
$searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$apiKey}&query=" . urlencode($cleanTitle) . "&year={$year}&language=fr-FR"; $searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$apiKey}&query=" . urlencode($cleanTitle) . "&year={$year}&language=fr-FR";
$searchRes = httpGet($searchUrl, 5); $searchRes = httpGet($searchUrl, 5);
$searchData = $searchRes ? json_decode($searchRes, true) : []; $searchData = $searchRes ? json_decode($searchRes, true) : [];
// 2. Fallback sans l'année
if (empty($searchData['results'])) { if (empty($searchData['results'])) {
$searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$apiKey}&query=" . urlencode($cleanTitle) . "&language=fr-FR"; $searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$apiKey}&query=" . urlencode($cleanTitle) . "&language=fr-FR";
$searchRes = httpGet($searchUrl, 5); $searchRes = httpGet($searchUrl, 5);
@@ -218,16 +192,13 @@ function fetchTMDBFull($title, $year, $apiKey, $pdo) {
if (empty($searchData['results'])) return null; if (empty($searchData['results'])) return null;
$movieId = $searchData['results'][0]['id']; $movieId = $searchData['results'][0]['id'];
// 🔥 AJOUT : On demande aussi les translations pour récupérer le titre français
$detailsUrl = "https://api.themoviedb.org/3/movie/{$movieId}?api_key={$apiKey}&append_to_response=credits,watch/providers,translations&language=fr-FR"; $detailsUrl = "https://api.themoviedb.org/3/movie/{$movieId}?api_key={$apiKey}&append_to_response=credits,watch/providers,translations&language=fr-FR";
$detailsRes = httpGet($detailsUrl, 5); $detailsRes = httpGet($detailsUrl, 5);
if (!$detailsRes) return null; if (!$detailsRes) return null;
$details = json_decode($detailsRes, true); $details = json_decode($detailsRes, true);
// 🔥 RÉCUPÉRATION DU TITRE FRANÇAIS $frenchTitle = $details['title'] ?? '';
$frenchTitle = $details['title'] ?? ''; // Titre par défaut (peut être l'original)
// Chercher le titre français dans les traductions
if (!empty($details['translations']['translations'])) { if (!empty($details['translations']['translations'])) {
foreach ($details['translations']['translations'] as $translation) { foreach ($details['translations']['translations'] as $translation) {
if ($translation['iso_3166_1'] === 'FR' && !empty($translation['data']['title'])) { if ($translation['iso_3166_1'] === 'FR' && !empty($translation['data']['title'])) {
@@ -237,7 +208,6 @@ function fetchTMDBFull($title, $year, $apiKey, $pdo) {
} }
} }
// Extraction Réalisateurs
$director = ''; $director = '';
if (!empty($details['credits']['crew'])) { if (!empty($details['credits']['crew'])) {
$directorsList = []; $directorsList = [];
@@ -247,17 +217,14 @@ function fetchTMDBFull($title, $year, $apiKey, $pdo) {
$director = implode(', ', $directorsList); $director = implode(', ', $directorsList);
} }
// Extraction Acteurs (Top 4)
$cast = []; $cast = [];
if (!empty($details['credits']['cast'])) { if (!empty($details['credits']['cast'])) {
$topCast = array_slice($details['credits']['cast'], 0, 4); $topCast = array_slice($details['credits']['cast'], 0, 4);
foreach ($topCast as $actor) $cast[] = $actor['name']; foreach ($topCast as $actor) $cast[] = $actor['name'];
} }
// Synopsis
$overview = $details['overview'] ?? ''; $overview = $details['overview'] ?? '';
// Streaming
$streaming = ''; $streaming = '';
$frProviders = $details['watch/providers']['results']['FR'] ?? []; $frProviders = $details['watch/providers']['results']['FR'] ?? [];
$platforms = []; $platforms = [];
@@ -269,7 +236,7 @@ function fetchTMDBFull($title, $year, $apiKey, $pdo) {
if (!empty($platforms)) $streaming = implode(', ', array_unique($platforms)); if (!empty($platforms)) $streaming = implode(', ', array_unique($platforms));
$result = [ $result = [
'title' => $frenchTitle, // 🔥 Titre français prioritaire 'title' => $frenchTitle,
'year' => !empty($details['release_date']) ? substr($details['release_date'], 0, 4) : '', 'year' => !empty($details['release_date']) ? substr($details['release_date'], 0, 4) : '',
'director' => $director, 'director' => $director,
'poster' => !empty($details['poster_path']) ? "https://image.tmdb.org/t/p/w500" . $details['poster_path'] : '', 'poster' => !empty($details['poster_path']) ? "https://image.tmdb.org/t/p/w500" . $details['poster_path'] : '',
@@ -279,7 +246,8 @@ function fetchTMDBFull($title, $year, $apiKey, $pdo) {
'cast' => $cast 'cast' => $cast
]; ];
setCache($pdo, $cacheKey, $result, 'tmdb'); // 🔥 SUPPRESSION DE L'APPEL À setCache() QUI N'EXISTE PLUS
return $result; return $result;
} }
@@ -354,7 +322,6 @@ switch ($action) {
'length' => '', 'number_of_discs' => 1, 'aspect_ratio' => '', 'actors' => '' 'length' => '', 'number_of_discs' => 1, 'aspect_ratio' => '', 'actors' => ''
]; ];
// DVDFr en priorité
$dvdfrData = fetchDVDFr($ean, $pdo); $dvdfrData = fetchDVDFr($ean, $pdo);
if (!empty($dvdfrData)) { if (!empty($dvdfrData)) {
if (!empty($dvdfrData['poster'])) $result['poster'] = $dvdfrData['poster']; if (!empty($dvdfrData['poster'])) $result['poster'] = $dvdfrData['poster'];
@@ -365,7 +332,6 @@ switch ($action) {
if (!empty($dvdfrData['discs'])) $result['number_of_discs'] = (int)$dvdfrData['discs']; if (!empty($dvdfrData['discs'])) $result['number_of_discs'] = (int)$dvdfrData['discs'];
} }
// TMDB pour le reste
$tmdbKey = getTmdbApiKey($pdo); $tmdbKey = getTmdbApiKey($pdo);
if ($tmdbKey && !empty($result['publisher'])) { if ($tmdbKey && !empty($result['publisher'])) {
$tmdbData = fetchTMDBFull($result['publisher'], '', $tmdbKey, $pdo); $tmdbData = fetchTMDBFull($result['publisher'], '', $tmdbKey, $pdo);
@@ -432,7 +398,7 @@ switch ($action) {
$imported = 0; $imported = 0;
$debugLog = []; $debugLog = [];
$pdo->beginTransaction(); // 🔥 UNE SEULE TRANSACTION $pdo->beginTransaction();
try { try {
foreach ($items as $rowData) { foreach ($items as $rowData) {
$title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre'; $title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre';
@@ -441,7 +407,6 @@ switch ($action) {
$id = makeStableId($type, $title, $year); $id = makeStableId($type, $title, $year);
if ($type === 'videotheque') { if ($type === 'videotheque') {
// Acteurs depuis CSV
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
$actors = ''; $actors = '';
if (!empty($csvActors)) { if (!empty($csvActors)) {
@@ -449,7 +414,6 @@ switch ($action) {
$actors = implode(', ', array_slice($actorsArray, 0, 4)); $actors = implode(', ', array_slice($actorsArray, 0, 4));
} }
// Normalisation EAN
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? ''; $ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
if (!empty($ean)) { if (!empty($ean)) {
$eanFloat = floatval($ean); $eanFloat = floatval($ean);
@@ -474,7 +438,6 @@ switch ($action) {
$poster = $rowData['poster'] ?? ''; $poster = $rowData['poster'] ?? '';
$director = ''; $director = '';
// 1. DVDFr (priorité pour les supports physiques FR)
if (!empty($ean)) { if (!empty($ean)) {
$dvdfrData = fetchDVDFr($ean, $pdo); $dvdfrData = fetchDVDFr($ean, $pdo);
if (!empty($dvdfrData)) { if (!empty($dvdfrData)) {
@@ -487,7 +450,6 @@ switch ($action) {
} }
} }
// 2. TMDB (réalisateur, synopsis, acteurs)
if ($tmdbApiKey && !empty($title)) { if ($tmdbApiKey && !empty($title)) {
$tmdbTitle = $title; $tmdbTitle = $title;
$tmdbTitle = preg_replace('/\s*[\[\(].*?[\]\)]\s*/', '', $tmdbTitle); $tmdbTitle = preg_replace('/\s*[\[\(].*?[\]\)]\s*/', '', $tmdbTitle);
@@ -531,7 +493,6 @@ switch ($action) {
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]); $stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
} else { } else {
// Pour les critiques
$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'] ?? '';