Actualiser api.php
This commit is contained in:
@@ -120,50 +120,42 @@ function extractYear($dateStr) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Fanart.tv API (Jaquettes exclusives) ──
|
// ── 1. Fonction Fanart.tv EXCLUSIVE (Aucune jaquette TMDB ou OpenLibrary) ──
|
||||||
function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) {
|
function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) {
|
||||||
if (empty($title)) return null;
|
// Image locale par défaut si aucun résultat
|
||||||
|
$defaultPoster = 'assets/img/default_physical_media.jpg';
|
||||||
$cleanTitle = cleanTitle($title);
|
$cleanTitle = cleanTitle($title);
|
||||||
if (empty($cleanTitle)) return null;
|
|
||||||
|
|
||||||
// Récupérer la clé API fanart.tv
|
if (empty($cleanTitle)) return ['poster' => $defaultPoster, 'title' => $title, 'format' => $format];
|
||||||
$apiKey = '70f9747dafe9b27f9b14ef80ee0cacc9'; // Clé par défaut
|
|
||||||
|
// Récupération des clés API
|
||||||
|
$apiKey = '70f9747dafe9b27f9b14ef80ee0cacc9';
|
||||||
if ($pdo) {
|
if ($pdo) {
|
||||||
$stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = 'fanart_api_key'");
|
$stmt = $pdo->prepare("SELECT key_value FROM config WHERE key_name = 'fanart_api_key'");
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$row = $stmt->fetch();
|
$row = $stmt->fetch();
|
||||||
if ($row) {
|
if ($row) $apiKey = decryptData($row['key_value']);
|
||||||
$apiKey = decryptData($row['key_value']);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Récupération silencieuse de l'ID via TMDB
|
|
||||||
$tmdbKey = getTmdbApiKey($pdo);
|
$tmdbKey = getTmdbApiKey($pdo);
|
||||||
if (!$tmdbKey) {
|
if (!$tmdbKey) return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => $format];
|
||||||
error_log("Fanart.tv: Clé TMDB requise");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Étape A : Obtenir l'ID TMDB (Recherche silencieuse)
|
||||||
$searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query=" . urlencode($cleanTitle);
|
$searchUrl = "https://api.themoviedb.org/3/search/movie?api_key={$tmdbKey}&query=" . urlencode($cleanTitle);
|
||||||
if (!empty($year)) {
|
if (!empty($year)) $searchUrl .= "&year={$year}";
|
||||||
$searchUrl .= "&year={$year}";
|
|
||||||
}
|
|
||||||
$searchUrl .= "&language=fr-FR";
|
$searchUrl .= "&language=fr-FR";
|
||||||
|
|
||||||
$searchRes = httpGet($searchUrl, 5);
|
$searchRes = httpGet($searchUrl, 5);
|
||||||
$searchData = $searchRes ? json_decode($searchRes, true) : [];
|
$searchData = $searchRes ? json_decode($searchRes, true) : [];
|
||||||
|
|
||||||
if (empty($searchData['results'])) {
|
if (empty($searchData['results'])) {
|
||||||
error_log("Fanart.tv: Film non trouvé sur TMDB pour '$title'");
|
return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => $format];
|
||||||
// Verrouillage : TMDB ne le trouve pas, on force l'image par défaut
|
|
||||||
return ['poster' => 'assets/img/default_physical_media.jpg', 'title' => $cleanTitle, 'format' => $format];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$tmdbId = $searchData['results'][0]['id'];
|
$tmdbId = $searchData['results'][0]['id'];
|
||||||
$poster = null; // Initialisation stricte
|
$poster = null;
|
||||||
|
|
||||||
// 2. Interrogation exclusive de Fanart.tv avec l'ID obtenu
|
// Étape B : Interrogation stricte de Fanart.tv
|
||||||
$fanartUrl = "https://webservice.fanart.tv/v3/movies/{$tmdbId}?api_key={$apiKey}";
|
$fanartUrl = "https://webservice.fanart.tv/v3/movies/{$tmdbId}?api_key={$apiKey}";
|
||||||
$fanartRes = httpGet($fanartUrl, 5);
|
$fanartRes = httpGet($fanartUrl, 5);
|
||||||
$fanartData = $fanartRes ? json_decode($fanartRes, true) : [];
|
$fanartData = $fanartRes ? json_decode($fanartRes, true) : [];
|
||||||
@@ -171,14 +163,14 @@ function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) {
|
|||||||
if (!isset($fanartData['error']) && !empty($fanartData['movieposter'])) {
|
if (!isset($fanartData['error']) && !empty($fanartData['movieposter'])) {
|
||||||
$posters = $fanartData['movieposter'];
|
$posters = $fanartData['movieposter'];
|
||||||
|
|
||||||
// PRIORITÉ 1 : Français
|
// Priorité 1 : Langue Française
|
||||||
$frenchPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'fr'; });
|
$frenchPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'fr'; });
|
||||||
if (!empty($frenchPosters)) {
|
if (!empty($frenchPosters)) {
|
||||||
usort($frenchPosters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); });
|
usort($frenchPosters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); });
|
||||||
$poster = $frenchPosters[0]['url'];
|
$poster = $frenchPosters[0]['url'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRIORITÉ 2 : Anglais (fallback)
|
// Priorité 2 : Langue Anglaise
|
||||||
if (empty($poster)) {
|
if (empty($poster)) {
|
||||||
$englishPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'en'; });
|
$englishPosters = array_filter($posters, function($p) { return isset($p['lang']) && $p['lang'] === 'en'; });
|
||||||
if (!empty($englishPosters)) {
|
if (!empty($englishPosters)) {
|
||||||
@@ -187,18 +179,15 @@ function fetchFanartTv($title, $year = '', $format = 'bluray', $pdo = null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRIORITÉ 3 : N'importe quelle langue
|
// Priorité 3 : N'importe quelle jaquette dispo
|
||||||
if (empty($poster)) {
|
if (empty($poster)) {
|
||||||
usort($posters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); });
|
usort($posters, function($a, $b) { return ($b['likes'] ?? 0) - ($a['likes'] ?? 0); });
|
||||||
$poster = $posters[0]['url'];
|
$poster = $posters[0]['url'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Verrouillage final
|
|
||||||
if (empty($poster)) {
|
if (empty($poster)) {
|
||||||
error_log("Fanart.tv: Aucune jaquette trouvée pour TMDB ID {$tmdbId}");
|
$poster = $defaultPoster;
|
||||||
// AUCUN retour vers TMDB, on force l'image locale
|
|
||||||
$poster = 'assets/img/default_physical_media.jpg';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -498,96 +487,31 @@ case 'get_config_keys':
|
|||||||
|
|
||||||
case 'import_batch':
|
case 'import_batch':
|
||||||
checkAuth($pdo);
|
checkAuth($pdo);
|
||||||
set_time_limit(0);
|
$data = json_decode(file_get_contents("php://input"), true);
|
||||||
|
$type = $data['type'] ?? 'critique';
|
||||||
$items = $data['items'] ?? [];
|
$items = $data['items'] ?? [];
|
||||||
$type = $data['type'] ?? 'videotheque';
|
|
||||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
|
||||||
$imported = 0;
|
|
||||||
$debugLog = [];
|
|
||||||
|
|
||||||
$pdo->beginTransaction();
|
$pdo->beginTransaction();
|
||||||
try {
|
$imported = 0;
|
||||||
$sqlVideotheque = "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
|
|
||||||
title=VALUES(title), year=VALUES(year),
|
|
||||||
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)";
|
|
||||||
$stmtVideotheque = $pdo->prepare($sqlVideotheque);
|
|
||||||
|
|
||||||
$sqlCritiques = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
title=VALUES(title), year=VALUES(year),
|
|
||||||
rating=VALUES(rating),
|
|
||||||
review=IF(VALUES(review)!='',VALUES(review),review),
|
|
||||||
director=IF(VALUES(director)!='',VALUES(director),director),
|
|
||||||
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
|
|
||||||
streaming=IF(VALUES(streaming)!='',VALUES(streaming),streaming)";
|
|
||||||
$stmtCritiques = $pdo->prepare($sqlCritiques);
|
|
||||||
|
|
||||||
foreach ($items as $rowData) {
|
|
||||||
$title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre';
|
|
||||||
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
|
|
||||||
$year = extractYear($publishDate);
|
|
||||||
$id = makeStableId($type, $title, $year);
|
|
||||||
|
|
||||||
if ($type === 'videotheque') {
|
if ($type === 'videotheque') {
|
||||||
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
$stmtVideo = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description) VALUES (?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE ean_isbn13 = ?, poster = ?, description = ?");
|
||||||
$actors = '';
|
|
||||||
if (!empty($csvActors)) {
|
|
||||||
$actorsArray = array_map('trim', explode(',', $csvActors));
|
|
||||||
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
foreach ($items as $item) {
|
||||||
if (!empty($ean)) {
|
$title = $item['title'] ?? '';
|
||||||
$eanFloat = floatval($ean);
|
if (empty($title)) continue;
|
||||||
if ($eanFloat > 0) $ean = (string) round($eanFloat);
|
|
||||||
$ean = preg_replace('/[^0-9]/', '', $ean);
|
|
||||||
}
|
|
||||||
|
|
||||||
$lengthRaw = $rowData['length'] ?? '';
|
$year = $item['year'] ?? '';
|
||||||
$length = '';
|
$ean = $item['ean'] ?? '';
|
||||||
if ($lengthRaw !== '' && $lengthRaw !== null) {
|
$desc = $item['description'] ?? '';
|
||||||
$lengthVal = floatval($lengthRaw);
|
|
||||||
if ($lengthVal > 0) $length = (string) round($lengthVal);
|
|
||||||
}
|
|
||||||
|
|
||||||
$discsRaw = $rowData['number_of_discs'] ?? '';
|
$id = makeStableId('videotheque', $title, $year);
|
||||||
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
|
$fanartData = fetchFanartTv($title, $year, 'Blu-ray', $pdo);
|
||||||
|
|
||||||
$description = $rowData['description'] ?? $rowData['Description'] ?? '';
|
|
||||||
$publisher = $rowData['publisher'] ?? '';
|
|
||||||
$aspect = $rowData['aspect_ratio'] ?? '';
|
|
||||||
$format = $rowData['format'] ?? detectFormat($title, $description);
|
|
||||||
$poster = $rowData['poster'] ?? '';
|
|
||||||
$director = '';
|
|
||||||
|
|
||||||
// Récupération jaquette via DVDCover API
|
|
||||||
$cleanTitleForDC = cleanTitle($title);
|
|
||||||
if (!empty($cleanTitleForDC)) {
|
|
||||||
$fanartData = fetchFanartTv($cleanTitleForDC, $year, $format, $pdo);
|
|
||||||
if (!empty($fanartData)) {
|
|
||||||
if (!empty($fanartData['poster'])) {
|
|
||||||
$poster = $fanartData['poster'];
|
$poster = $fanartData['poster'];
|
||||||
}
|
|
||||||
if (!empty($fanartData['title']) && ($title === 'Sans titre' || empty($title))) {
|
|
||||||
$title = $fanartData['title'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$stmtVideotheque->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
|
|
||||||
|
|
||||||
|
$stmtVideo->execute([$id, $title, $year, 'Blu-ray', $poster, $ean, $desc, $ean, $poster, $desc]);
|
||||||
|
$imported++;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
|
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
|
||||||
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
|
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
|
||||||
@@ -609,18 +533,8 @@ case 'get_config_keys':
|
|||||||
$stmtCritiques->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
|
$stmtCritiques->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
|
||||||
}
|
}
|
||||||
$imported++;
|
$imported++;
|
||||||
}
|
|
||||||
|
|
||||||
$pdo->commit();
|
$pdo->commit();
|
||||||
echo json_encode(["success" => true, "imported" => $imported, "debug" => $debugLog]);
|
echo json_encode(["success" => true, "imported" => $imported]);
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
if ($pdo->inTransaction()) {
|
|
||||||
$pdo->rollBack();
|
|
||||||
}
|
|
||||||
http_response_code(500);
|
|
||||||
echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage(), "debug" => $debugLog]);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'debug_fanart':
|
case 'debug_fanart':
|
||||||
|
|||||||
Reference in New Issue
Block a user