Actualiser api.php
This commit is contained in:
@@ -396,54 +396,42 @@ switch ($action) {
|
||||
$type = $data['type'] ?? 'videotheque';
|
||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
||||
$imported = 0;
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// ── PHASE 1 : Enrichissement HTTP (HORS transaction pour éviter les timeouts) ──
|
||||
$enriched = [];
|
||||
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 isolé par type pour éviter les collisions
|
||||
$id = makeStableId($type, $title, $year);
|
||||
|
||||
if ($type === 'videotheque') {
|
||||
$firstName = $rowData['first_name'] ?? '';
|
||||
$lastName = $rowData['last_name'] ?? '';
|
||||
$creators = $rowData['creators'] ?? '';
|
||||
// 🔥 CORRECTION : Dans ce format CSV, first_name/last_name/creators sont les ACTEURS, pas le réalisateur.
|
||||
// On laisse $director vide pour que TMDB récupère le vrai réalisateur.
|
||||
$director = '';
|
||||
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
||||
$actors = '';
|
||||
if (!empty($csvActors)) {
|
||||
$actorsArray = array_map('trim', explode(',', $csvActors));
|
||||
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
||||
}
|
||||
|
||||
// Normalisation EAN
|
||||
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
||||
// Bug 1 : EAN peut arriver comme float "7321950374984.0", on normalise en entier string
|
||||
if (!empty($ean)) {
|
||||
$eanFloat = floatval($ean);
|
||||
if ($eanFloat > 0) $ean = (string) round($eanFloat);
|
||||
$ean = preg_replace('/[^0-9]/', '', $ean);
|
||||
}
|
||||
$description = $rowData['description'] ?? $rowData['Description'] ?? '';
|
||||
$publisher = $rowData['publisher'] ?? '';
|
||||
// Bug 2 : length peut être un float "245.0", on le convertit en entier
|
||||
// Normalisation length
|
||||
$lengthRaw = $rowData['length'] ?? '';
|
||||
$length = '';
|
||||
if ($lengthRaw !== '' && $lengthRaw !== null) {
|
||||
$lengthVal = floatval($lengthRaw);
|
||||
if ($lengthVal > 0) $length = (string) round($lengthVal);
|
||||
}
|
||||
// Bug 3 : number_of_discs peut être NaN ou vide, on sécurise à 1
|
||||
// Normalisation number_of_discs
|
||||
$discsRaw = $rowData['number_of_discs'] ?? '';
|
||||
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
|
||||
|
||||
$description = $rowData['description'] ?? $rowData['Description'] ?? '';
|
||||
$publisher = $rowData['publisher'] ?? '';
|
||||
$aspect = $rowData['aspect_ratio'] ?? '';
|
||||
$format = $rowData['format'] ?? detectFormat($title, $description);
|
||||
$poster = $rowData['poster'] ?? '';
|
||||
$director = '';
|
||||
|
||||
// 🔥 Extraction des acteurs depuis le CSV
|
||||
// Acteurs depuis CSV
|
||||
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
||||
$actors = '';
|
||||
if (!empty($csvActors)) {
|
||||
@@ -451,7 +439,7 @@ switch ($action) {
|
||||
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
||||
}
|
||||
|
||||
// 1. UPCitemDB (enrichissement optionnel, ne bloque pas l'import)
|
||||
// 1. UPCitemDB
|
||||
if (!empty($ean)) {
|
||||
$upcData = fetchUPCitemdb($ean, $pdo);
|
||||
if ($upcData) {
|
||||
@@ -460,19 +448,18 @@ switch ($action) {
|
||||
if (empty($publisher)) $publisher = $upcData['publisher'];
|
||||
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
|
||||
}
|
||||
// 1.5 DVDFr (Récupère uniquement la vraie jaquette FR)
|
||||
// 1.5 DVDFr
|
||||
$dvdfrCover = fetchDVDFr($ean, $pdo);
|
||||
if (!empty($dvdfrCover)) $poster = $dvdfrCover;
|
||||
}
|
||||
|
||||
// 2. TMDB (Données Officielles & Synopsis) — fonctionne avec ou sans EAN
|
||||
// 2. TMDB
|
||||
if ($tmdbApiKey && !empty($title)) {
|
||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
||||
// Si TMDB ne trouve rien (cas des Coffrets ou titres multiples)
|
||||
if (!$tmdbData || empty($tmdbData['overview'])) {
|
||||
$cleanTitle = $title;
|
||||
$cleanTitle = preg_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle);
|
||||
$cleanTitle = preg_split('/(\\/|\+)/', $cleanTitle)[0];
|
||||
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
|
||||
$cleanTitle = explode(' - ', $cleanTitle)[0];
|
||||
$cleanTitle = trim($cleanTitle);
|
||||
if (!empty($cleanTitle) && $cleanTitle !== $title) {
|
||||
@@ -482,11 +469,8 @@ switch ($action) {
|
||||
}
|
||||
if ($tmdbData) {
|
||||
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
|
||||
if (empty($director)) {
|
||||
$director = $tmdbData['director'] ?? '';
|
||||
} elseif (!empty($tmdbData['director']) && strpos($director, ',') === false && strpos($tmdbData['director'], ',') !== false) {
|
||||
$director = $tmdbData['director'];
|
||||
}
|
||||
if (empty($director)) $director = $tmdbData['director'] ?? '';
|
||||
elseif (!empty($tmdbData['director']) && strpos($director, ',') === false && strpos($tmdbData['director'], ',') !== false) $director = $tmdbData['director'];
|
||||
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
|
||||
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
|
||||
if (!empty($tmdbData['overview'])) $description = $tmdbData['overview'];
|
||||
@@ -494,68 +478,50 @@ switch ($action) {
|
||||
}
|
||||
}
|
||||
|
||||
// INSERT toujours exécuté, EAN présent ou non
|
||||
try {
|
||||
// 🔥 CORRECTION : On n'écrase pas le titre et l'année s'ils existent déjà, mais on les met à jour si le CSV change.
|
||||
// Le réalisateur est forcé à vide au début pour laisser TMDB trouver le vrai réalisateur (car le CSV contient les acteurs).
|
||||
$sql = "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)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
|
||||
} catch (\Exception $e) {
|
||||
error_log("import_batch videotheque error on title '{$title}': " . $e->getMessage());
|
||||
continue;
|
||||
}
|
||||
$enriched[] = compact('id','title','year','director','poster','format','length','publisher','ean','discs','aspect','description','actors');
|
||||
|
||||
} else {
|
||||
// Pour les critiques
|
||||
// Critiques
|
||||
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
|
||||
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
|
||||
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
|
||||
$director = ''; $poster = ''; $streaming = '';
|
||||
|
||||
if ($tmdbApiKey && !empty($title)) {
|
||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
||||
if ($tmdbData) {
|
||||
$director = $tmdbData['director'];
|
||||
$poster = $tmdbData['poster'];
|
||||
$streaming = $tmdbData['streaming'];
|
||||
if(empty($year)) $year = $tmdbData['year'];
|
||||
if (empty($year)) $year = $tmdbData['year'];
|
||||
}
|
||||
}
|
||||
if (empty($streaming)) $streaming = 'Support physique / Cinéma';
|
||||
$enriched[] = compact('id','title','year','director','poster','rating','review','streaming');
|
||||
}
|
||||
}
|
||||
|
||||
// 🔥 Valeur par défaut pour le streaming
|
||||
if (empty($streaming)) {
|
||||
$streaming = 'Support physique / Cinéma';
|
||||
}
|
||||
|
||||
$sql = "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)";
|
||||
// ── PHASE 2 : INSERTs dans une transaction courte (pas d'HTTP ici) ──
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
foreach ($enriched as $row) {
|
||||
if ($type === 'videotheque') {
|
||||
$sql = "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 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)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
|
||||
$stmt->execute([$row['id'], $row['title'], $row['year'], $row['director'], $row['poster'], $row['format'], $row['length'], $row['publisher'], $row['ean'], $row['discs'], $row['aspect'], $row['description'], $row['actors']]);
|
||||
} else {
|
||||
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE 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)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$row['id'], $row['title'], $row['year'], $row['director'], $row['poster'], $row['rating'], $row['review'], $row['streaming']]);
|
||||
}
|
||||
$imported++;
|
||||
}
|
||||
$pdo->commit();
|
||||
} catch (\Exception $e) {
|
||||
$pdo->rollback();
|
||||
http_response_code(500);
|
||||
echo json_encode(["success" => false, "error" => $e->getMessage()]);
|
||||
break;
|
||||
}
|
||||
echo json_encode(["success" => true, "imported" => $imported]);
|
||||
break;
|
||||
}
|
||||
Reference in New Issue
Block a user