Actualiser api.php
This commit is contained in:
@@ -453,16 +453,17 @@ switch ($action) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
// ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ──
|
// ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ──
|
||||||
case 'import_batch':
|
case 'import_batch':
|
||||||
checkAuth($pdo);
|
checkAuth($pdo);
|
||||||
set_time_limit(0); // 🔥 EMPÊCHE PHP DE TUER LE SCRIPT APRÈS 30 SECONDES
|
set_time_limit(0); // Empêche le timeout PHP
|
||||||
$items = $data['items'] ?? [];
|
$items = $data['items'] ?? [];
|
||||||
$type = $data['type'] ?? 'videotheque';
|
$type = $data['type'] ?? 'videotheque';
|
||||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
$tmdbApiKey = getTmdbApiKey($pdo);
|
||||||
$imported = 0;
|
$imported = 0;
|
||||||
|
|
||||||
// ── PHASE 1 : Enrichissement HTTP (HORS transaction pour éviter les timeouts) ──
|
try {
|
||||||
$enriched = [];
|
$pdo->beginTransaction(); // 🔥 UNE SEULE transaction
|
||||||
|
|
||||||
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'] ?? '';
|
||||||
@@ -470,21 +471,19 @@ switch ($action) {
|
|||||||
$id = makeStableId($type, $title, $year);
|
$id = makeStableId($type, $title, $year);
|
||||||
|
|
||||||
if ($type === 'videotheque') {
|
if ($type === 'videotheque') {
|
||||||
// Normalisation EAN
|
// Normalisation des données
|
||||||
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
||||||
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);
|
||||||
}
|
}
|
||||||
// Normalisation length
|
|
||||||
$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);
|
||||||
}
|
}
|
||||||
// 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;
|
||||||
|
|
||||||
@@ -495,7 +494,6 @@ switch ($action) {
|
|||||||
$poster = $rowData['poster'] ?? '';
|
$poster = $rowData['poster'] ?? '';
|
||||||
$director = '';
|
$director = '';
|
||||||
|
|
||||||
// Acteurs depuis CSV
|
|
||||||
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
||||||
$actors = '';
|
$actors = '';
|
||||||
if (!empty($csvActors)) {
|
if (!empty($csvActors)) {
|
||||||
@@ -512,7 +510,7 @@ 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 : affiche FR + métadonnées (priorité sur UPCitemdb)
|
// 1.5 DVDFr
|
||||||
$dvdfrData = fetchDVDFr($ean, $pdo);
|
$dvdfrData = fetchDVDFr($ean, $pdo);
|
||||||
if (!empty($dvdfrData)) {
|
if (!empty($dvdfrData)) {
|
||||||
if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster'];
|
if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster'];
|
||||||
@@ -528,115 +526,7 @@ switch ($action) {
|
|||||||
if ($tmdbApiKey && !empty($title)) {
|
if ($tmdbApiKey && !empty($title)) {
|
||||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
||||||
if (!$tmdbData || empty($tmdbData['overview'])) {
|
if (!$tmdbData || empty($tmdbData['overview'])) {
|
||||||
$cleanTitle = $title;
|
$cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $title);
|
||||||
$cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle);
|
|
||||||
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
|
|
||||||
$cleanTitle = explode(' - ', $cleanTitle)[0];
|
|
||||||
$cleanTitle = trim($cleanTitle);
|
|
||||||
if (!empty($cleanTitle) && $cleanTitle !== $title) {
|
|
||||||
$tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo);
|
|
||||||
if ($tmdbFallback && !empty($tmdbFallback['overview'])) $tmdbData = $tmdbFallback;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
|
|
||||||
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
|
|
||||||
if (!empty($tmdbData['overview'])) $description = $tmdbData['overview'];
|
|
||||||
if (!empty($tmdbData['cast'])) $actors = implode(', ', $tmdbData['cast']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$enriched[] = compact('id','title','year','director','poster','format','length','publisher','ean','discs','aspect','description','actors');
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// 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($streaming)) $streaming = 'Support physique / Cinéma';
|
|
||||||
$enriched[] = compact('id','title','year','director','poster','rating','review','streaming');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$pdo->beginTransaction();
|
|
||||||
$pdo->beginTransaction();
|
|
||||||
try {
|
|
||||||
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') {
|
|
||||||
$director = '';
|
|
||||||
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
|
||||||
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'] ?? '';
|
|
||||||
|
|
||||||
$lengthRaw = $rowData['length'] ?? '';
|
|
||||||
$length = '';
|
|
||||||
if ($lengthRaw !== '' && $lengthRaw !== null) {
|
|
||||||
$lengthVal = floatval($lengthRaw);
|
|
||||||
if ($lengthVal > 0) $length = (string) round($lengthVal);
|
|
||||||
}
|
|
||||||
|
|
||||||
$discsRaw = $rowData['number_of_discs'] ?? '';
|
|
||||||
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
|
|
||||||
|
|
||||||
$aspect = $rowData['aspect_ratio'] ?? '';
|
|
||||||
$format = $rowData['format'] ?? detectFormat($title, $description);
|
|
||||||
$poster = $rowData['poster'] ?? '';
|
|
||||||
|
|
||||||
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
|
||||||
$actors = '';
|
|
||||||
if (!empty($csvActors)) {
|
|
||||||
$actorsArray = array_map('trim', explode(',', $csvActors));
|
|
||||||
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($ean)) {
|
|
||||||
$upcData = fetchUPCitemdb($ean, $pdo);
|
|
||||||
if ($upcData) {
|
|
||||||
if (empty($poster) && !empty($upcData['poster'])) $poster = $upcData['poster'];
|
|
||||||
if ($title === 'Sans titre') $title = $upcData['title'];
|
|
||||||
if (empty($publisher)) $publisher = $upcData['publisher'];
|
|
||||||
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
|
|
||||||
}
|
|
||||||
$dvdfrData = fetchDVDFr($ean, $pdo);
|
|
||||||
if (!empty($dvdfrData)) {
|
|
||||||
if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster'];
|
|
||||||
if (!empty($dvdfrData['publisher'])) $publisher = $dvdfrData['publisher'];
|
|
||||||
if (!empty($dvdfrData['format']) && (empty($format) || $format === 'Blu-ray')) $format = $dvdfrData['format'];
|
|
||||||
if (!empty($dvdfrData['length']) && empty($length)) $length = $dvdfrData['length'];
|
|
||||||
if (!empty($dvdfrData['aspect']) && empty($aspect)) $aspect = $dvdfrData['aspect'];
|
|
||||||
if (!empty($dvdfrData['discs']) && $discs === 1) $discs = (int)$dvdfrData['discs'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($tmdbApiKey && !empty($title)) {
|
|
||||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
|
||||||
if (!$tmdbData || empty($tmdbData['overview'])) {
|
|
||||||
$cleanTitle = $title;
|
|
||||||
// 🔥 CORRECTION BUG FATAL : preg_ireplace n'existe pas, on utilise str_ireplace
|
|
||||||
$cleanTitle = str_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);
|
||||||
@@ -655,7 +545,7 @@ switch ($action) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🔥 SQL MIS À JOUR (Ajout de title et year dans le UPDATE)
|
// INSERT SQL
|
||||||
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors)
|
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
ON DUPLICATE KEY UPDATE
|
ON DUPLICATE KEY UPDATE
|
||||||
@@ -706,12 +596,12 @@ switch ($action) {
|
|||||||
$pdo->commit();
|
$pdo->commit();
|
||||||
echo json_encode(["success" => true, "imported" => $imported]);
|
echo json_encode(["success" => true, "imported" => $imported]);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
// 🔥 FILET DE SÉCURITÉ : Si une erreur fatale PHP survient, on annule tout proprement
|
// 🔥 FILET DE SÉCURITÉ : Annule tout et renvoie l'erreur en JSON (plus de HTML)
|
||||||
|
if ($pdo->inTransaction()) {
|
||||||
$pdo->rollBack();
|
$pdo->rollBack();
|
||||||
|
}
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
// On renvoie l'erreur en JSON pour que le JavaScript puisse l'afficher proprement
|
|
||||||
echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage()]);
|
echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage()]);
|
||||||
}
|
}
|
||||||
echo json_encode(["success" => true, "imported" => $imported]);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user