Actualiser api.php
This commit is contained in:
@@ -120,23 +120,28 @@ function extractYear($dateStr) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Fanart.tv API (Jaquettes DVD/Blu-ray françaises uniquement) ──
|
||||||
function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) {
|
function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) {
|
||||||
if (empty($title)) return null;
|
if (empty($title)) return null;
|
||||||
|
|
||||||
$cleanTitle = cleanTitle($title);
|
$cleanTitle = cleanTitle($title);
|
||||||
if (empty($cleanTitle)) return null;
|
if (empty($cleanTitle)) return null;
|
||||||
|
|
||||||
// Récupérer la clé API
|
// Récupérer la clé API fanart.tv
|
||||||
$apiKey = getFanartApiKey($pdo);
|
$apiKey = '70f9747dafe9b27f9b14ef80ee0cacc9'; // Clé par défaut
|
||||||
if (empty($apiKey)) {
|
if ($pdo) {
|
||||||
// Fallback sur la clé en dur si non configurée
|
$stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = 'fanart_api_key'");
|
||||||
$apiKey = '70f9747dafe9b27f9b14ef80ee0cacc9';
|
$stmt->execute();
|
||||||
|
$row = $stmt->fetch();
|
||||||
|
if ($row) {
|
||||||
|
$apiKey = decryptData($row['key_value']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Étape 1 : Chercher le film sur TMDB pour obtenir son ID
|
// Étape 1 : Chercher le film sur TMDB pour obtenir son ID
|
||||||
$tmdbKey = getTmdbApiKey($pdo);
|
$tmdbKey = getTmdbApiKey($pdo);
|
||||||
if (!$tmdbKey) {
|
if (!$tmdbKey) {
|
||||||
error_log("Fanart.tv: Clé TMDB requise pour obtenir l'ID du film");
|
error_log("Fanart.tv: Clé TMDB requise");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,6 +149,7 @@ function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) {
|
|||||||
if (!empty($year)) {
|
if (!empty($year)) {
|
||||||
$searchUrl .= "&year={$year}";
|
$searchUrl .= "&year={$year}";
|
||||||
}
|
}
|
||||||
|
$searchUrl .= "&language=fr-FR";
|
||||||
|
|
||||||
$searchRes = httpGet($searchUrl, 5);
|
$searchRes = httpGet($searchUrl, 5);
|
||||||
$searchData = $searchRes ? json_decode($searchRes, true) : [];
|
$searchData = $searchRes ? json_decode($searchRes, true) : [];
|
||||||
@@ -167,38 +173,49 @@ function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) {
|
|||||||
|
|
||||||
$poster = '';
|
$poster = '';
|
||||||
|
|
||||||
// Priorité aux movieposter (jaquettes complètes)
|
// PRIORITÉ 1 : movieposter en français (jaquettes DVD/Blu-ray FR)
|
||||||
if (!empty($fanartData['movieposter'])) {
|
if (!empty($fanartData['movieposter'])) {
|
||||||
// Filtrer par langue (fr d'abord, puis en, puis tout)
|
|
||||||
$posters = $fanartData['movieposter'];
|
$posters = $fanartData['movieposter'];
|
||||||
$preferredLangs = ['fr', 'en', ''];
|
|
||||||
|
|
||||||
foreach ($preferredLangs as $lang) {
|
// Filtrer uniquement les jaquettes françaises
|
||||||
$filtered = array_filter($posters, function($p) use ($lang) {
|
$frenchPosters = array_filter($posters, function($p) {
|
||||||
return ($lang === '' || ($p['lang'] ?? '') === $lang);
|
return isset($p['lang']) && $p['lang'] === 'fr';
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!empty($filtered)) {
|
|
||||||
// Trier par likes (le plus populaire en premier)
|
// Trier par likes (le plus populaire en premier)
|
||||||
usort($filtered, function($a, $b) {
|
if (!empty($frenchPosters)) {
|
||||||
|
usort($frenchPosters, function($a, $b) {
|
||||||
return ($b['likes'] ?? 0) - ($a['likes'] ?? 0);
|
return ($b['likes'] ?? 0) - ($a['likes'] ?? 0);
|
||||||
});
|
});
|
||||||
$poster = $filtered[0]['url'];
|
$poster = $frenchPosters[0]['url'];
|
||||||
break;
|
error_log("Fanart.tv: Jaquette FR trouvée - {$poster}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback sur hdmovielogo si pas de movieposter
|
// PRIORITÉ 2 : Si pas de jaquette FR, prendre movieposter en anglais
|
||||||
if (empty($poster) && !empty($fanartData['hdmovielogo'])) {
|
if (empty($poster) && !empty($fanartData['movieposter'])) {
|
||||||
$logos = $fanartData['hdmovielogo'];
|
$posters = $fanartData['movieposter'];
|
||||||
$poster = $logos[0]['url'];
|
$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'];
|
||||||
|
error_log("Fanart.tv: Jaquette EN trouvée (fallback) - {$poster}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback sur moviebackground
|
// PRIORITÉ 3 : Fallback sur n'importe quelle langue
|
||||||
if (empty($poster) && !empty($fanartData['moviebackground'])) {
|
if (empty($poster) && !empty($fanartData['movieposter'])) {
|
||||||
$backgrounds = $fanartData['moviebackground'];
|
$posters = $fanartData['movieposter'];
|
||||||
$poster = $backgrounds[0]['url'];
|
usort($posters, function($a, $b) {
|
||||||
|
return ($b['likes'] ?? 0) - ($a['likes'] ?? 0);
|
||||||
|
});
|
||||||
|
$poster = $posters[0]['url'];
|
||||||
|
error_log("Fanart.tv: Jaquette trouvée (toute langue) - {$poster}");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($poster)) {
|
if (empty($poster)) {
|
||||||
@@ -344,18 +361,13 @@ switch ($action) {
|
|||||||
|
|
||||||
case 'get_config_keys':
|
case 'get_config_keys':
|
||||||
checkAuth($pdo);
|
checkAuth($pdo);
|
||||||
$stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name IN ('tmdb_api_key', 'fanart_api_key')");
|
$stmt = $pdo->prepare("SELECT key_name, key_value FROM config WHERE key_name IN ('tmdb_api_key', 'fanart_api_key')");
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$rows = $stmt->fetchAll();
|
$rows = $stmt->fetchAll();
|
||||||
$config = [];
|
$config = [];
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
if ($row['key_name'] === 'tmdb_api_key') {
|
$config[$row['key_name']] = $row['key_value'] ? '••••••••' : '';
|
||||||
$config['tmdb_api_key'] = $row['key_value'] ? '••••••••' : '';
|
|
||||||
} elseif ($row['key_name'] === 'fanart_api_key') {
|
|
||||||
$config['fanart_api_key'] = $row['key_value'] ? '••••••••' : '';
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Valeurs par défaut si non configurées
|
|
||||||
if (!isset($config['tmdb_api_key'])) $config['tmdb_api_key'] = '';
|
if (!isset($config['tmdb_api_key'])) $config['tmdb_api_key'] = '';
|
||||||
if (!isset($config['fanart_api_key'])) $config['fanart_api_key'] = '';
|
if (!isset($config['fanart_api_key'])) $config['fanart_api_key'] = '';
|
||||||
echo json_encode($config);
|
echo json_encode($config);
|
||||||
@@ -370,7 +382,8 @@ switch ($action) {
|
|||||||
$stmt->execute([$keyName, encryptData($keyValue)]);
|
$stmt->execute([$keyName, encryptData($keyValue)]);
|
||||||
echo json_encode(["success" => true]);
|
echo json_encode(["success" => true]);
|
||||||
} else {
|
} else {
|
||||||
http_response_code(400); echo json_encode(["error" => "Données invalides."]);
|
http_response_code(400);
|
||||||
|
echo json_encode(["error" => "Données invalides."]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -430,10 +443,10 @@ switch ($action) {
|
|||||||
|
|
||||||
if ($type === 'videotheque') {
|
if ($type === 'videotheque') {
|
||||||
$format = $result['format'] ?: 'Blu-ray';
|
$format = $result['format'] ?: 'Blu-ray';
|
||||||
$dcData = fetchFanartTv($titleForSearch, $result['year'], $format, $pdo);
|
$fanartData = fetchFanartTv($titleForSearch, $result['year'], $format, $pdo);
|
||||||
if (!empty($dcData)) {
|
if (!empty($fanartData)) {
|
||||||
if (!empty($dcData['poster'])) $result['poster'] = $dcData['poster'];
|
if (!empty($fanartData['poster'])) $result['poster'] = $fanartData['poster'];
|
||||||
if (!empty($dcData['title'])) $result['title'] = $dcData['title'];
|
if (!empty($fanartData['title'])) $result['title'] = $fanartData['title'];
|
||||||
$result['format'] = $format;
|
$result['format'] = $format;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -476,9 +489,9 @@ switch ($action) {
|
|||||||
} else {
|
} else {
|
||||||
if (empty($data['poster']) && !empty($data['title'])) {
|
if (empty($data['poster']) && !empty($data['title'])) {
|
||||||
$format = $data['format'] ?: 'Blu-ray';
|
$format = $data['format'] ?: 'Blu-ray';
|
||||||
$dcData = fetchFanartTv($data['title'], $data['year'] ?? '', $format, $pdo);
|
$fanartData = fetchFanartTv($data['title'], $data['year'] ?? '', $format, $pdo);
|
||||||
if (!empty($dcData['poster'])) {
|
if (!empty($fanartData['poster'])) {
|
||||||
$data['poster'] = $dcData['poster'];
|
$data['poster'] = $fanartData['poster'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -584,13 +597,13 @@ switch ($action) {
|
|||||||
// Récupération jaquette via DVDCover API
|
// Récupération jaquette via DVDCover API
|
||||||
$cleanTitleForDC = cleanTitle($title);
|
$cleanTitleForDC = cleanTitle($title);
|
||||||
if (!empty($cleanTitleForDC)) {
|
if (!empty($cleanTitleForDC)) {
|
||||||
$dcData = fetchFanartTv($cleanTitleForDC, $year, $format, $pdo);
|
$fanartData = fetchFanartTv($cleanTitleForDC, $year, $format, $pdo);
|
||||||
if (!empty($dcData)) {
|
if (!empty($fanartData)) {
|
||||||
if (!empty($dcData['poster'])) {
|
if (!empty($fanartData['poster'])) {
|
||||||
$poster = $dcData['poster'];
|
$poster = $fanartData['poster'];
|
||||||
}
|
}
|
||||||
if (!empty($dcData['title']) && ($title === 'Sans titre' || empty($title))) {
|
if (!empty($fanartData['title']) && ($title === 'Sans titre' || empty($title))) {
|
||||||
$title = $dcData['title'];
|
$title = $fanartData['title'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -632,62 +645,15 @@ switch ($action) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'debug_dvdcover':
|
case 'debug_fanart':
|
||||||
$title = $_GET['title'] ?? '';
|
$title = $_GET['title'] ?? '';
|
||||||
$year = $_GET['year'] ?? '';
|
$year = $_GET['year'] ?? '';
|
||||||
$format = $_GET['format'] ?? 'Blu-ray';
|
|
||||||
|
|
||||||
if (!$title) { echo json_encode(['error' => 'Titre manquant']); exit; }
|
if (!$title) { echo json_encode(['error' => 'Titre manquant']); exit; }
|
||||||
|
|
||||||
$result = ['title' => $title, 'year' => $year, 'format' => $format];
|
$result = ['title' => $title, 'year' => $year];
|
||||||
|
$fanartData = fetchFanartTv($title, $year, 'Blu-ray', $pdo);
|
||||||
// Test direct de l'API
|
$result['data'] = $fanartData;
|
||||||
$cleanTitle = cleanTitle($title);
|
$result['status'] = $fanartData ? 'OK' : 'AUCUN_RÉSULTAT';
|
||||||
$apiUrl = "https://www.dvdcover.com/wp-json/wp/v2/posts?search=" . urlencode($cleanTitle) . "&_embed&per_page=5";
|
|
||||||
$result['api_url'] = $apiUrl;
|
|
||||||
|
|
||||||
$json = httpGet($apiUrl, 10);
|
|
||||||
if ($json) {
|
|
||||||
$posts = json_decode($json, true);
|
|
||||||
$result['posts_count'] = is_array($posts) ? count($posts) : 0;
|
|
||||||
$result['posts'] = [];
|
|
||||||
|
|
||||||
if (is_array($posts)) {
|
|
||||||
foreach ($posts as $post) {
|
|
||||||
$postData = [
|
|
||||||
'id' => $post['id'] ?? null,
|
|
||||||
'title' => $post['title']['rendered'] ?? '',
|
|
||||||
'link' => $post['link'] ?? '',
|
|
||||||
];
|
|
||||||
|
|
||||||
// Extraire les images
|
|
||||||
$imgs = [];
|
|
||||||
if (isset($post['_embedded']['wp:featuredmedia'][0])) {
|
|
||||||
$featured = $post['_embedded']['wp:featuredmedia'][0];
|
|
||||||
if (!empty($featured['source_url'])) {
|
|
||||||
$imgs[] = $featured['source_url'];
|
|
||||||
}
|
|
||||||
if (!empty($featured['media_details']['sizes'])) {
|
|
||||||
foreach ($featured['media_details']['sizes'] as $size => $data) {
|
|
||||||
if (!empty($data['source_url'])) {
|
|
||||||
$imgs[] = $data['source_url'] . " ($size)";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$postData['images'] = $imgs;
|
|
||||||
|
|
||||||
$result['posts'][] = $postData;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$result['api_error'] = 'Impossible de joindre l\'API';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test de la fonction complète
|
|
||||||
$data = fetchDVDCover($title, $year, $format);
|
|
||||||
$result['fetch_result'] = $data;
|
|
||||||
$result['status'] = $data ? 'OK' : 'AUCUN_RÉSULTAT';
|
|
||||||
|
|
||||||
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||||
exit;
|
exit;
|
||||||
|
|||||||
Reference in New Issue
Block a user