Actualiser api.php
This commit is contained in:
@@ -396,54 +396,42 @@ switch ($action) {
|
|||||||
$type = $data['type'] ?? 'videotheque';
|
$type = $data['type'] ?? 'videotheque';
|
||||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
$tmdbApiKey = getTmdbApiKey($pdo);
|
||||||
$imported = 0;
|
$imported = 0;
|
||||||
$pdo->beginTransaction();
|
|
||||||
|
|
||||||
|
// ── PHASE 1 : Enrichissement HTTP (HORS transaction pour éviter les timeouts) ──
|
||||||
|
$enriched = [];
|
||||||
foreach ($items as $rowData) {
|
foreach ($items as $rowData) {
|
||||||
$title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre';
|
$title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre';
|
||||||
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
|
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
|
||||||
$year = extractYear($publishDate);
|
$year = extractYear($publishDate);
|
||||||
|
|
||||||
// 🔥 ID isolé par type pour éviter les collisions
|
|
||||||
$id = makeStableId($type, $title, $year);
|
$id = makeStableId($type, $title, $year);
|
||||||
|
|
||||||
if ($type === 'videotheque') {
|
if ($type === 'videotheque') {
|
||||||
$firstName = $rowData['first_name'] ?? '';
|
// Normalisation EAN
|
||||||
$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));
|
|
||||||
}
|
|
||||||
|
|
||||||
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
||||||
// Bug 1 : EAN peut arriver comme float "7321950374984.0", on normalise en entier string
|
|
||||||
if (!empty($ean)) {
|
if (!empty($ean)) {
|
||||||
$eanFloat = floatval($ean);
|
$eanFloat = floatval($ean);
|
||||||
if ($eanFloat > 0) $ean = (string) round($eanFloat);
|
if ($eanFloat > 0) $ean = (string) round($eanFloat);
|
||||||
$ean = preg_replace('/[^0-9]/', '', $ean);
|
$ean = preg_replace('/[^0-9]/', '', $ean);
|
||||||
}
|
}
|
||||||
$description = $rowData['description'] ?? $rowData['Description'] ?? '';
|
// Normalisation length
|
||||||
$publisher = $rowData['publisher'] ?? '';
|
|
||||||
// Bug 2 : length peut être un float "245.0", on le convertit en entier
|
|
||||||
$lengthRaw = $rowData['length'] ?? '';
|
$lengthRaw = $rowData['length'] ?? '';
|
||||||
$length = '';
|
$length = '';
|
||||||
if ($lengthRaw !== '' && $lengthRaw !== null) {
|
if ($lengthRaw !== '' && $lengthRaw !== null) {
|
||||||
$lengthVal = floatval($lengthRaw);
|
$lengthVal = floatval($lengthRaw);
|
||||||
if ($lengthVal > 0) $length = (string) round($lengthVal);
|
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'] ?? '';
|
$discsRaw = $rowData['number_of_discs'] ?? '';
|
||||||
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
|
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
|
||||||
|
|
||||||
|
$description = $rowData['description'] ?? $rowData['Description'] ?? '';
|
||||||
|
$publisher = $rowData['publisher'] ?? '';
|
||||||
$aspect = $rowData['aspect_ratio'] ?? '';
|
$aspect = $rowData['aspect_ratio'] ?? '';
|
||||||
$format = $rowData['format'] ?? detectFormat($title, $description);
|
$format = $rowData['format'] ?? detectFormat($title, $description);
|
||||||
$poster = $rowData['poster'] ?? '';
|
$poster = $rowData['poster'] ?? '';
|
||||||
|
$director = '';
|
||||||
|
|
||||||
// 🔥 Extraction des acteurs depuis le CSV
|
// Acteurs depuis CSV
|
||||||
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
||||||
$actors = '';
|
$actors = '';
|
||||||
if (!empty($csvActors)) {
|
if (!empty($csvActors)) {
|
||||||
@@ -451,7 +439,7 @@ switch ($action) {
|
|||||||
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. UPCitemDB (enrichissement optionnel, ne bloque pas l'import)
|
// 1. UPCitemDB
|
||||||
if (!empty($ean)) {
|
if (!empty($ean)) {
|
||||||
$upcData = fetchUPCitemdb($ean, $pdo);
|
$upcData = fetchUPCitemdb($ean, $pdo);
|
||||||
if ($upcData) {
|
if ($upcData) {
|
||||||
@@ -460,19 +448,18 @@ switch ($action) {
|
|||||||
if (empty($publisher)) $publisher = $upcData['publisher'];
|
if (empty($publisher)) $publisher = $upcData['publisher'];
|
||||||
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
|
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);
|
$dvdfrCover = fetchDVDFr($ean, $pdo);
|
||||||
if (!empty($dvdfrCover)) $poster = $dvdfrCover;
|
if (!empty($dvdfrCover)) $poster = $dvdfrCover;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. TMDB (Données Officielles & Synopsis) — fonctionne avec ou sans EAN
|
// 2. TMDB
|
||||||
if ($tmdbApiKey && !empty($title)) {
|
if ($tmdbApiKey && !empty($title)) {
|
||||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
||||||
// Si TMDB ne trouve rien (cas des Coffrets ou titres multiples)
|
|
||||||
if (!$tmdbData || empty($tmdbData['overview'])) {
|
if (!$tmdbData || empty($tmdbData['overview'])) {
|
||||||
$cleanTitle = $title;
|
$cleanTitle = $title;
|
||||||
$cleanTitle = preg_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle);
|
$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 = explode(' - ', $cleanTitle)[0];
|
||||||
$cleanTitle = trim($cleanTitle);
|
$cleanTitle = trim($cleanTitle);
|
||||||
if (!empty($cleanTitle) && $cleanTitle !== $title) {
|
if (!empty($cleanTitle) && $cleanTitle !== $title) {
|
||||||
@@ -482,11 +469,8 @@ switch ($action) {
|
|||||||
}
|
}
|
||||||
if ($tmdbData) {
|
if ($tmdbData) {
|
||||||
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
|
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
|
||||||
if (empty($director)) {
|
if (empty($director)) $director = $tmdbData['director'] ?? '';
|
||||||
$director = $tmdbData['director'] ?? '';
|
elseif (!empty($tmdbData['director']) && strpos($director, ',') === false && strpos($tmdbData['director'], ',') !== false) $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($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
|
||||||
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
|
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
|
||||||
if (!empty($tmdbData['overview'])) $description = $tmdbData['overview'];
|
if (!empty($tmdbData['overview'])) $description = $tmdbData['overview'];
|
||||||
@@ -494,38 +478,14 @@ switch ($action) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// INSERT toujours exécuté, EAN présent ou non
|
$enriched[] = compact('id','title','year','director','poster','format','length','publisher','ean','discs','aspect','description','actors');
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Pour les critiques
|
// Critiques
|
||||||
$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 = ''; $streaming = '';
|
||||||
|
|
||||||
if ($tmdbApiKey && !empty($title)) {
|
if ($tmdbApiKey && !empty($title)) {
|
||||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
||||||
if ($tmdbData) {
|
if ($tmdbData) {
|
||||||
@@ -535,27 +495,33 @@ switch ($action) {
|
|||||||
if (empty($year)) $year = $tmdbData['year'];
|
if (empty($year)) $year = $tmdbData['year'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (empty($streaming)) $streaming = 'Support physique / Cinéma';
|
||||||
// 🔥 Valeur par défaut pour le streaming
|
$enriched[] = compact('id','title','year','director','poster','rating','review','streaming');
|
||||||
if (empty($streaming)) {
|
}
|
||||||
$streaming = 'Support physique / Cinéma';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
|
// ── PHASE 2 : INSERTs dans une transaction courte (pas d'HTTP ici) ──
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
try {
|
||||||
ON DUPLICATE KEY UPDATE
|
$pdo->beginTransaction();
|
||||||
title=VALUES(title), year=VALUES(year),
|
foreach ($enriched as $row) {
|
||||||
rating=VALUES(rating),
|
if ($type === 'videotheque') {
|
||||||
review=IF(VALUES(review)!='',VALUES(review),review),
|
$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)";
|
||||||
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 = $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++;
|
$imported++;
|
||||||
}
|
}
|
||||||
$pdo->commit();
|
$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]);
|
echo json_encode(["success" => true, "imported" => $imported]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user