Actualiser api.php

This commit is contained in:
2026-06-27 11:29:37 +02:00
parent 84303fea95
commit 1cbe5bb79c
+30 -41
View File
@@ -1,6 +1,7 @@
<?php <?php
ini_set('log_errors', 1); ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php_errors.log'); ini_set('error_log', __DIR__ . '/php_errors.log');
ini_set('display_errors', 0);
error_reporting(E_ALL); error_reporting(E_ALL);
header("Content-Type: application/json; charset=UTF-8"); header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Origin: *");
@@ -419,7 +420,7 @@ switch ($action) {
else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); }
break; break;
case 'import_batch': case 'import_batch':
checkAuth($pdo); checkAuth($pdo);
$data = json_decode(file_get_contents("php://input"), true); $data = json_decode(file_get_contents("php://input"), true);
$type = $data['type'] ?? 'critique'; $type = $data['type'] ?? 'critique';
@@ -429,9 +430,6 @@ switch ($action) {
$imported = 0; $imported = 0;
try { try {
// ── On récupère la clé d'API une seule fois pour tout le monde ──
$tmdbApiKey = getTmdbApiKey($pdo);
if ($type === 'videotheque') { 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) $stmtVideo = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
@@ -455,47 +453,42 @@ switch ($action) {
$year = $item['year'] ?? ''; $year = $item['year'] ?? '';
$id = makeStableId('videotheque', $title, $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'] ?? ''; $ean = $item['ean'] ?? '';
$format = $item['format'] ?? 'Inconnu'; $format = $item['format'] ?? 'Inconnu';
$publisher = $item['publisher'] ?? ''; $publisher = $item['publisher'] ?? '';
$discs = $item['number_of_discs'] ?? 1; $discs = $item['number_of_discs'] ?? 1;
$aspect = $item['aspect_ratio'] ?? ''; $aspect = $item['aspect_ratio'] ?? '';
$length = $item['length'] ?? ''; $length = $item['length'] ?? '';
$description = $item['description'] ?? '';
$director = $item['director'] ?? '';
$actors = $item['actors'] ?? '';
$poster = 'assets/img/default_physical_media.jpg';
// Champs cinématographiques de base (du CSV s'ils existent) // ── SOURCE 2 : API TMDB POUR COMPLÉTER ──
$desc = $item['description'] ?? ''; $tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
$director = $item['director'] ?? '';
$actors = $item['actors'] ?? '';
$poster = 'assets/img/default_physical_media.jpg'; // Image par défaut
// ── 2. APPEL API (TMDB ou OMDb) POUR COMBLER LES MANQUES ── if ($tmdbData) {
if ($tmdbApiKey && !empty($title)) { if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') {
$apiData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); $poster = $tmdbData['poster'];
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 (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([ $stmtVideo->execute([
$id, $title, $year, $format, $poster, $ean, $desc, $id, $title, $year, $format, $poster, $ean, $description,
$length, $discs, $aspect, $actors, $publisher, $director $length, $discs, $aspect, $actors, $publisher, $director
]); ]);
$imported++; $imported++;
} }
} else { } else { // ── IMPORTATION CRITIQUES ──
$stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) $stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director),
@@ -511,32 +504,28 @@ switch ($action) {
$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'] ?? '';
$director = ''; $poster = ''; $streaming = ''; $director = ''; $poster = 'assets/img/default_physical_media.jpg'; $streaming = 'Support physique / Cinéma';
if ($tmdbApiKey && !empty($title)) { // ── CORRECTION ICI : Utilisation de la bonne fonction TMDB ──
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); $tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
if ($tmdbData) { if ($tmdbData) {
$director = $tmdbData['director'] ?? ''; if (!empty($tmdbData['director'])) $director = $tmdbData['director'];
$poster = $tmdbData['poster'] ?? ''; if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') $poster = $tmdbData['poster'];
$streaming = $tmdbData['streaming'] ?? ''; if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
if (empty($year)) $year = $tmdbData['year'] ?? '';
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
}
} }
if (empty($streaming)) $streaming = 'Support physique / Cinéma';
$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]); echo json_encode(["success" => true, "imported" => $imported]);
} catch (Exception $e) {
} catch (Throwable $e) { // ── CORRECTION ICI : Throwable capture les Crash Fatals PHP ──
$pdo->rollBack(); $pdo->rollBack();
error_log("import_batch error: " . $e->getMessage()); error_log("import_batch error: " . $e->getMessage());
http_response_code(500); http_response_code(500);
echo json_encode(["success" => false, "error" => $e->getMessage()]); echo json_encode(["success" => false, "error" => $e->getMessage(), "line" => $e->getLine()]);
} }
break; break;
} }