Actualiser api.php

This commit is contained in:
2026-06-27 11:29:37 +02:00
parent 84303fea95
commit 1cbe5bb79c
+26 -37
View File
@@ -1,6 +1,7 @@
<?php
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php_errors.log');
ini_set('display_errors', 0);
error_reporting(E_ALL);
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *");
@@ -429,9 +430,6 @@ switch ($action) {
$imported = 0;
try {
// ── On récupère la clé d'API une seule fois pour tout le monde ──
$tmdbApiKey = getTmdbApiKey($pdo);
if ($type === 'videotheque') {
$stmtVideo = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
@@ -455,47 +453,42 @@ switch ($action) {
$year = $item['year'] ?? '';
$id = makeStableId('videotheque', $title, $year);
// ── 1. RECUPERATION DE TOUTES LES INFOS PHYSIQUES DEPUIS LE CSV ──
// ── SOURCE 1 : CSV STRICT (Sans Scraping) ──
$ean = $item['ean'] ?? '';
$format = $item['format'] ?? 'Inconnu';
$publisher = $item['publisher'] ?? '';
$discs = $item['number_of_discs'] ?? 1;
$aspect = $item['aspect_ratio'] ?? '';
$length = $item['length'] ?? '';
// Champs cinématographiques de base (du CSV s'ils existent)
$desc = $item['description'] ?? '';
$description = $item['description'] ?? '';
$director = $item['director'] ?? '';
$actors = $item['actors'] ?? '';
$poster = 'assets/img/default_physical_media.jpg'; // Image par défaut
$poster = 'assets/img/default_physical_media.jpg';
// ── 2. APPEL API (TMDB ou OMDb) POUR COMBLER LES MANQUES ──
if ($tmdbApiKey && !empty($title)) {
$apiData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
// ── SOURCE 2 : API TMDB POUR COMPLÉTER ──
$tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
if ($apiData) {
if (empty($director)) $director = $apiData['director'] ?? '';
if (empty($actors)) $actors = $apiData['actors'] ?? '';
if (empty($desc)) $desc = $apiData['synopsis'] ?? '';
if (empty($length) && !empty($apiData['length'])) $length = $apiData['length'];
if (!empty($apiData['poster'])) {
$poster = $apiData['poster'];
}
if ($tmdbData) {
if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') {
$poster = $tmdbData['poster'];
}
if (empty($description)) $description = $tmdbData['description'] ?? '';
if (empty($director)) $director = $tmdbData['director'] ?? '';
if (empty($actors)) $actors = $tmdbData['actors'] ?? '';
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
}
error_log("Import vidéotheque '{$title}' : poster=" . ($poster !== 'assets/img/default_physical_media.jpg' ? 'OK' : 'DEFAULT') . ", format={$format}, discs={$discs}, aspect={$aspect}");
if (empty($format)) $format = detectFormat($title, $description);
// Exécution remise À L'INTÉRIEUR de la boucle, avec les bonnes variables ($year, $desc)
$stmtVideo->execute([
$id, $title, $year, $format, $poster, $ean, $desc,
$id, $title, $year, $format, $poster, $ean, $description,
$length, $discs, $aspect, $actors, $publisher, $director
]);
$imported++;
}
} else {
} else { // ── IMPORTATION CRITIQUES ──
$stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director),
@@ -511,32 +504,28 @@ switch ($action) {
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
$director = ''; $poster = ''; $streaming = '';
$director = ''; $poster = 'assets/img/default_physical_media.jpg'; $streaming = 'Support physique / Cinéma';
if ($tmdbApiKey && !empty($title)) {
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
// ── CORRECTION ICI : Utilisation de la bonne fonction TMDB ──
$tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
if ($tmdbData) {
$director = $tmdbData['director'] ?? '';
$poster = $tmdbData['poster'] ?? '';
$streaming = $tmdbData['streaming'] ?? '';
if (empty($year)) $year = $tmdbData['year'] ?? '';
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
if (!empty($tmdbData['director'])) $director = $tmdbData['director'];
if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') $poster = $tmdbData['poster'];
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
}
}
if (empty($streaming)) $streaming = 'Support physique / Cinéma';
$stmtCritiques->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
$imported++;
}
}
$pdo->commit();
echo json_encode(["success" => true, "imported" => $imported]);
} catch (Exception $e) {
} catch (Throwable $e) { // ── CORRECTION ICI : Throwable capture les Crash Fatals PHP ──
$pdo->rollBack();
error_log("import_batch error: " . $e->getMessage());
http_response_code(500);
echo json_encode(["success" => false, "error" => $e->getMessage()]);
echo json_encode(["success" => false, "error" => $e->getMessage(), "line" => $e->getLine()]);
}
break;
}