Actualiser js/admin.js

This commit is contained in:
2026-06-28 14:13:04 +02:00
parent 62f4570d7c
commit 6522aa098c
+38 -57
View File
@@ -351,6 +351,12 @@ async function openConfigModal() {
fanartInput.value = '';
fanartInput.placeholder = data.fanart_api_key ? '✅ Clé Fanart.tv configurée (laisser vide pour garder)' : 'Entrez votre clé Fanart.tv';
}
const upcmdbInput = document.getElementById('upcmdb-key-input');
if (upcmdbInput) {
upcmdbInput.value = '';
upcmdbInput.placeholder = data.upcmdb_api_key ? '✅ Clé UPCMDB configurée (laisser vide pour garder)' : 'Gratuite sur upcmdb.com — fallback EAN';
}
} catch(e) { console.error(e); }
}
@@ -419,34 +425,11 @@ async function handleCritiqueUpload(input) {
loadDashboardData();
}
// 1. Normalisation des colonnes du CSV
// Extraction EAN uniquement depuis le CSV vidéothèque
function normalizeVideothequeRow(row) {
// Récupération stricte de l'EAN sans supprimer les zéros du début
let ean = row['ean_isbn13'] || row['EAN'] || row['ean'] || row['Barcode'] || row['UPC'] || '';
if (ean !== '') ean = String(ean).replace(/[^0-9]/g, ''); // Garde que les chiffres
let length = row['length'] || row['Length'] || row['Runtime'] || row['runtime'] || '';
if (length !== '') length = String(Math.round(parseFloat(length)));
let discs = row['number_of_discs'] || row['Number_of_discs'] || row['Discs'] || row['discs'] || '';
discs = (!discs || isNaN(parseFloat(discs))) ? 1 : Math.round(parseFloat(discs));
let aspect = row['aspect_ratio'] || row['Aspect_ratio'] || row['AspectRatio'] || row['Aspect Ratio'] || '';
let publisher = row['publisher'] || row['Publisher'] || row['Studio'] || row['studio'] || row['Label'] || '';
let format = row['format'] || row['Format'] || row['Media'] || row['media'] || '';
// Certains CSV ont déjà les acteurs, on les garde au cas où
let actors = row['creators'] || row['Actors'] || row['actors'] || row['Cast'] || '';
return Object.assign({}, row, {
ean: ean,
length: length,
number_of_discs: discs,
aspect_ratio: aspect,
publisher: publisher,
format: format,
actors: actors
});
let ean = row['ean_isbn13'] || row['EAN'] || row['ean'] || row['Barcode'] || row['UPC'] || row['upc_isbn10'] || '';
if (ean !== '') ean = String(ean).replace(/[^0-9]/g, '');
return { ean };
}
function handleVideothequeUpload(input) {
@@ -463,42 +446,27 @@ function handleVideothequeUpload(input) {
return;
}
// 2. Construction de l'objet pour l'API
const items = parsedData.map(row => {
const normalizedRow = normalizeVideothequeRow(row);
const title = row['title'] || row['Title'] || '';
if (!title) return null;
const publishDate = row['publish_date'] || row['Publish_date'] || row['publishdate'] || '';
const year = publishDate ? publishDate.split('-')[0] : (row['year'] || row['Year'] || '');
return {
title: title.trim(),
year: year,
description: (row['description'] || row['Description'] || '').trim(),
ean: normalizedRow['ean'],
length: normalizedRow['length'],
number_of_discs: normalizedRow['number_of_discs'],
aspect_ratio: normalizedRow['aspect_ratio'],
publisher: normalizedRow['publisher'],
format: normalizedRow['format'],
actors: normalizedRow['actors'],
director: row['director'] || row['Director'] || ''
};
const { ean } = normalizeVideothequeRow(row);
if (!ean || ean.length < 8) return null;
return { ean };
}).filter(item => item !== null);
if (items.length === 0) {
alert("❌ Aucun film valide trouvé dans le fichier CSV.");
alert("❌ Aucun code EAN valide trouvé dans le fichier CSV.");
return;
}
showImportModal(items.length, 'videotheque');
let processed = 0;
let totalImported = 0;
let totalSkipped = 0;
try {
// Traiter par lots de 3 pour éviter la surcharge (notrecinema.com est plus lent)
for (let i = 0; i < items.length; i += 3) {
const batch = items.slice(i, i + 3);
// Lots de 2 : UPCitemdb impose ~2 s entre chaque requête EAN
for (let i = 0; i < items.length; i += 2) {
const batch = items.slice(i, i + 2);
const response = await fetch(`${API_URL}?action=import_batch`, {
method: 'POST',
@@ -519,16 +487,17 @@ function handleVideothequeUpload(input) {
throw new Error(data.error || "Erreur inconnue");
}
totalImported += data.imported || 0;
totalSkipped += data.skipped || 0;
processed += batch.length;
updateImportModal(processed, items.length);
// Délai réduit (Les API supportent une cadence plus élevée)
await new Promise(resolve => setTimeout(resolve, 300));
}
input.value = '';
closeImportModal();
showSuccessModal(`${items.length} édition(s) physique(s) importée(s) avec succès !`);
const skipMsg = totalSkipped > 0 ? ` (${totalSkipped} EAN ignoré(s) — introuvable dans UPCitemdb/UPCMDB)` : '';
showSuccessModal(`${totalImported} édition(s) importée(s) (UPCitemdb + UPCMDB + TMDB).${skipMsg}`);
loadDashboardData();
} catch (err) {
@@ -561,6 +530,7 @@ function closeImportModal() { document.getElementById('import-progress-modal').c
async function saveConfigKeys() {
const tmdbKeyValue = document.getElementById('tmdb-key-input').value.trim();
const fanartKeyValue = document.getElementById('fanart-key-input').value.trim();
const upcmdbKeyValue = document.getElementById('upcmdb-key-input').value.trim();
try {
if (tmdbKeyValue) {
@@ -574,6 +544,17 @@ async function saveConfigKeys() {
});
}
if (upcmdbKeyValue) {
await fetch(`${API_URL}?action=save_config`, {
method: 'POST',
headers: {
'Authorization': localStorage.getItem('token'),
'Content-Type': 'application/json'
},
body: JSON.stringify({ key_name: 'upcmdb_api_key', key_value: upcmdbKeyValue })
});
}
if (fanartKeyValue) {
await fetch(`${API_URL}?action=save_config`, {
method: 'POST',
@@ -613,7 +594,7 @@ function updateImportInterface() {
desc.textContent = 'Sélectionnez vos fichiers "ratings.csv" et "reviews.csv" (Letterboxd).';
} else {
title.innerHTML = '<strong>Importer ma Vidéothèque</strong>';
desc.textContent = 'Sélectionnez vos listes CSV de supports physiques (Blu-ray, DVD, 4K).';
desc.textContent = 'Import CSV (EAN seul) : UPCitemdb puis UPCMDB en secours, TMDB pour affiche/synopsis/réalisateur/acteurs.';
}
}