New version

This commit is contained in:
2026-03-21 00:43:49 +01:00
parent 51b1c274e9
commit b4070dc0ba
10 changed files with 1508 additions and 219 deletions

View File

@@ -1,116 +1,299 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Downloader spotify</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
background-color: #121212;
color: white;
position: relative;
}
.container {
margin-top: 50px;
}
.form-label {
color: white;
}
.output-box {
background-color: #333;
padding: 20px;
border-radius: 8px;
white-space: pre-wrap;
color: lightgreen;
height: 500px;
overflow-y: auto;
text-align: left;
}
.spotify-icon {
position: absolute;
top: 10px;
right: 10px;
width: 150px;
height: 150px;
}
.d-flex {
display: flex;
align-items: center;
}
/* Ajuster la taille de la liste déroulante */
.format-select {
width: auto;
}
</style>
</head>
<body>
<!-- Ajouter l'icône Spotify pirate -->
<img src="{{ url_for('static', filename='images/spotify_pirate.png') }}" alt="Spotify Pirate Icon" class="spotify-icon">
{% extends "layout.html" %}
<div class="container text-center">
<h1>Downloader spotify</h1>
<form id="downloadForm" action="/download" method="POST">
<div class="mb-3">
<label for="url" class="form-label">URL Spotify</label>
<input type="text" class="form-control" id="url" name="url" placeholder="Entrez une URL Spotify" required>
</div>
<div class="mb-3 d-flex">
<label for="format" class="form-label">Choisissez le format</label>
<select class="form-select format-select" id="format" name="format" required>
<option value="flac">FLAC</option>
<option value="mp3">MP3</option>
</select>
</div>
{% block title %}Télécharger - Spotify Downloader{% endblock %}
<div class="mb-3 d-flex">
<input type="checkbox" class="form-check-input me-2" id="delete_old" name="delete_old">
<label class="form-check-label me-3" for="delete_old">Supprimer les anciens fichiers</label>
<button type="submit" class="btn btn-primary ms-auto">Télécharger</button>
{% block content %}
<div class="row justify-content-center">
<div class="col-lg-8">
<div class="card">
<div class="card-header">
<h4 class="mb-0"><i class="bi bi-download me-2"></i>Nouveau téléchargement</h4>
</div>
<div class="card-body">
<form id="downloadForm">
<div class="mb-3">
<label for="url" class="form-label">
<i class="bi bi-link-45deg me-1"></i>URL Spotify
</label>
<textarea
class="form-control"
id="url"
name="url"
rows="4"
placeholder="Collez une ou plusieurs URLs Spotify (une par ligne)&#10;Exemple:&#10;https://open.spotify.com/track/...&#10;https://open.spotify.com/playlist/..."
required
></textarea>
<div class="form-text">Accepte les tracks, albums et playlists Spotify</div>
<div id="urlError" class="text-danger mt-1" style="display: none;"></div>
</div>
<div class="mb-3">
<label for="copy_choice" class="form-label">Copier vers /mnt/data/Musique ?</label><br>
<input type="radio" id="copy_yes" name="copy_choice" value="yes" required>
<label for="copy_yes">Oui</label>
<input type="radio" id="copy_no" name="copy_choice" value="no" required>
<label for="copy_no">Non</label>
<div class="row g-3 mb-3">
<div class="col-md-4">
<label for="format" class="form-label">
<i class="bi bi-music-note me-1"></i>Format
</label>
<select class="form-select" id="format" name="format">
<option value="mp3">MP3</option>
<option value="flac">FLAC</option>
</select>
</div>
<div class="col-md-4">
<label for="copy_choice" class="form-label">
<i class="bi bi-folder-plus me-1"></i>Copier vers NAS
</label>
<select class="form-select" id="copy_choice" name="copy_choice">
<option value="no">Non</option>
<option value="yes">Oui</option>
</select>
</div>
<div class="col-md-4 d-flex align-items-end">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="delete_old" name="delete_old">
<label class="form-check-label" for="delete_old">Supprimer anciens fichiers</label>
</div>
</div>
</div>
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<button type="button" id="cancelBtn" class="btn btn-outline-danger me-md-2" disabled>
<i class="bi bi-x-circle me-1"></i>Annuler
</button>
<button type="submit" id="submitBtn" class="btn btn-spotify">
<i class="bi bi-download me-1"></i>Télécharger
</button>
</div>
</form>
<div id="downloadStatus" class="mt-4" style="display: none;">
<div class="d-flex justify-content-between align-items-center mb-2">
<span id="statusText" class="badge bg-info">En attente...</span>
<span id="progressText" class="text-muted small">0%</span>
</div>
<div id="progressContainer" class="progress mb-3 progress-indeterminate" style="height: 25px;">
<div id="progressBar" class="progress-bar progress-bar-striped progress-bar-animated" style="width: 100%;"></div>
</div>
<div class="output-box rounded p-3" id="logOutput" style="max-height: 400px; overflow-y: auto;">
<div class="text-muted text-center">En attente du téléchargement...</div>
</div>
</div>
</div>
</form>
</div>
<div id="output" class="output-box mt-3"></div>
{% if recent_downloads %}
<div class="card mt-4">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0"><i class="bi bi-clock-history me-2"></i>Téléchargements récents</h5>
<a href="{{ url_for('history') }}" class="btn btn-sm btn-outline-light">Voir tout</a>
</div>
<div class="card-body p-0">
<div class="list-group list-group-flush">
{% for dl in recent_downloads %}
<div class="list-group-item bg-transparent d-flex justify-content-between align-items-center">
<div>
<span class="badge bg-{{ 'success' if dl.status == 'completed' else 'danger' if dl.status == 'error' else 'warning' if dl.status == 'running' else 'secondary' }}">
{{ dl.status }}
</span>
<small class="text-muted ms-2">{{ dl.format_type.upper() }}</small>
</div>
<small class="text-muted">{{ dl.created_at.strftime('%d/%m %H:%M') }}</small>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
</div>
</div>
{% endblock %}
<script>
$(document).ready(function() {
$('#downloadForm').submit(function(event) {
event.preventDefault();
$('#output').empty(); // Clear previous output
{% block extra_js %}
<script>
(function() {
'use strict';
var form = $(this);
var xhr = new XMLHttpRequest();
xhr.open("POST", form.attr('action'), true);
const form = document.getElementById('downloadForm');
const submitBtn = document.getElementById('submitBtn');
const cancelBtn = document.getElementById('cancelBtn');
const downloadStatus = document.getElementById('downloadStatus');
const statusText = document.getElementById('statusText');
const progressText = document.getElementById('progressText');
const progressBar = document.getElementById('progressBar');
const progressContainer = document.getElementById('progressContainer');
const logOutput = document.getElementById('logOutput');
const urlError = document.getElementById('urlError');
var seenLines = new Set(); // Stocker les lignes déjà vues
let currentDownloadId = null;
let eventSource = null;
xhr.onreadystatechange = function() {
if (xhr.readyState == 3 || xhr.readyState == 4) { // Partial or complete data received
var newOutput = xhr.responseText.split("\n"); // Diviser les lignes de l'output
newOutput.forEach(function(line) {
if (!seenLines.has(line) && line.trim() !== "") { // Vérifier si la ligne est nouvelle
seenLines.add(line); // Marquer la ligne comme vue
$('#output').append(line + "\n"); // Ajouter la nouvelle ligne
}
});
$('#output').scrollTop($('#output')[0].scrollHeight); // Scroll to the bottom
}
};
const spotifyPattern = /^https?:\/\/open\.spotify\.com\/(track|album|playlist)\/[\w-]+/;
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(form.serialize());
});
function validateUrls(text) {
const urls = text.split('\n').filter(u => u.trim());
for (const url of urls) {
if (!spotifyPattern.test(url.trim())) {
return { valid: false, invalid: url };
}
}
return { valid: true, count: urls.length };
}
document.getElementById('url').addEventListener('input', function() {
const result = validateUrls(this.value);
if (this.value.trim() && !result.valid) {
urlError.textContent = `URL invalide: ${result.invalid.substring(0, 50)}...`;
urlError.style.display = 'block';
} else {
urlError.style.display = 'none';
}
});
function appendLog(message, type = 'info') {
const lines = message.split('\n').filter(l => l.trim());
lines.forEach(line => {
const span = document.createElement('div');
span.className = `log-${type}`;
span.textContent = line;
logOutput.appendChild(span);
});
</script>
</body>
</html>
logOutput.scrollTop = logOutput.scrollHeight;
}
function setStatus(status, progress) {
const badges = {
'pending': ['bg-secondary', 'En attente...'],
'running': ['bg-warning', 'En cours...'],
'completed': ['bg-success', 'Terminé !'],
'error': ['bg-danger', 'Erreur'],
'cancelled': ['bg-secondary', 'Annulé']
};
const [color, text] = badges[status] || ['bg-secondary', status];
statusText.className = `badge ${color}`;
statusText.textContent = text;
progressText.textContent = `${progress}%`;
if (status === 'running') {
progressBar.classList.add('progress-bar-animated');
progressBar.style.width = '100%';
progressContainer.classList.add('progress-indeterminate');
} else {
progressBar.classList.remove('progress-bar-animated');
progressBar.style.width = `${progress}%`;
progressContainer.classList.remove('progress-indeterminate');
if (status === 'completed') {
progressBar.classList.remove('progress-bar-striped');
showToast('Téléchargement terminé avec succès !', 'success');
} else if (status === 'error') {
showToast('Erreur lors du téléchargement', 'error');
} else if (status === 'cancelled') {
showToast('Téléchargement annulé', 'warning');
}
}
}
function startStream(downloadId) {
if (eventSource) {
eventSource.close();
}
eventSource = new EventSource(`/api/download/${downloadId}/stream`);
eventSource.onmessage = function(e) {
const [id, status, progress, log] = e.data.split('|');
if (log !== '__END__' && log) {
let type = 'info';
if (log.includes('Erreur') || log.includes('Error')) type = 'error';
else if (log.includes('terminé') || log.includes('Terminé')) type = 'success';
else if (log.includes('Downloaded')) type = 'success';
else if (log.includes('Ajout')) type = 'info';
appendLog(log, type);
}
setStatus(status, parseInt(progress));
if (status === 'completed' || status === 'error' || status === 'cancelled') {
eventSource.close();
eventSource = null;
submitBtn.disabled = false;
cancelBtn.disabled = true;
}
};
eventSource.onerror = function() {
eventSource.close();
eventSource = null;
};
}
form.addEventListener('submit', async function(e) {
e.preventDefault();
const urlValue = document.getElementById('url').value;
const validation = validateUrls(urlValue);
if (!validation.valid) {
urlError.textContent = `URL invalide: ${validation.invalid.substring(0, 50)}...`;
urlError.style.display = 'block';
return;
}
const formData = {
url: urlValue,
format: document.getElementById('format').value,
delete_old: document.getElementById('delete_old').checked,
copy_choice: document.getElementById('copy_choice').value
};
submitBtn.disabled = true;
cancelBtn.disabled = false;
downloadStatus.style.display = 'block';
logOutput.innerHTML = '';
setStatus('pending', 0);
appendLog(`Démarrage de ${validation.count} téléchargement(s)...`, 'info');
try {
const response = await fetch('/api/download', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
});
const data = await response.json();
if (data.error) {
appendLog(`Erreur: ${data.error}`, 'error');
setStatus('error', 0);
submitBtn.disabled = false;
cancelBtn.disabled = true;
return;
}
currentDownloadId = data.id;
setStatus('running', 0);
startStream(currentDownloadId);
} catch (err) {
appendLog(`Erreur réseau: ${err.message}`, 'error');
setStatus('error', 0);
submitBtn.disabled = false;
cancelBtn.disabled = true;
}
});
cancelBtn.addEventListener('click', async function() {
if (!currentDownloadId) return;
try {
await fetch(`/api/download/${currentDownloadId}/cancel`, {
method: 'POST'
});
appendLog('Annulation en cours...', 'warning');
} catch (err) {
appendLog(`Erreur: ${err.message}`, 'error');
}
});
})();
</script>
{% endblock %}