This commit is contained in:
cool.gitter.choco
2025-02-09 13:15:11 -06:00
parent afe4fe505a
commit e27dc15475

View File

@@ -31,13 +31,6 @@ document.addEventListener('DOMContentLoaded', () => {
}
});
/**
* A helper function to delay execution for a given number of milliseconds.
*/
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Renders playlist header and tracks.
*/
@@ -86,7 +79,7 @@ function renderPlaylist(playlist) {
downloadPlaylistBtn.addEventListener('click', () => {
// Remove individual track download buttons (but leave the whole playlist button).
document.querySelectorAll('.download-btn').forEach(btn => {
if (btn.id !== 'downloadPlaylistBtn' && btn.id !== 'downloadAlbumPlaylistBtn') {
if (btn.id !== 'downloadPlaylistBtn') {
btn.remove();
}
});
@@ -104,55 +97,6 @@ function renderPlaylist(playlist) {
});
});
// --- Add "Download Album's Playlist" Button ---
let downloadAlbumPlaylistBtn = document.getElementById('downloadAlbumPlaylistBtn');
if (!downloadAlbumPlaylistBtn) {
downloadAlbumPlaylistBtn = document.createElement('button');
downloadAlbumPlaylistBtn.id = 'downloadAlbumPlaylistBtn';
downloadAlbumPlaylistBtn.textContent = "Download Album's Playlist";
downloadAlbumPlaylistBtn.className = 'download-btn download-btn--main';
// Insert the button into the header container.
const headerContainer = document.getElementById('playlist-header');
headerContainer.appendChild(downloadAlbumPlaylistBtn);
}
downloadAlbumPlaylistBtn.addEventListener('click', () => {
// Remove individual track download buttons (but leave the whole playlist and album buttons).
document.querySelectorAll('.download-btn').forEach(btn => {
if (btn.id !== 'downloadPlaylistBtn' && btn.id !== 'downloadAlbumPlaylistBtn') {
btn.remove();
}
});
// Disable the album download button to prevent repeated clicks.
downloadAlbumPlaylistBtn.disabled = true;
downloadAlbumPlaylistBtn.textContent = 'Queueing...';
// Extract unique album external URLs from the playlist info.
const uniqueAlbums = new Map();
playlist.tracks.items.forEach(item => {
const album = item.track.album;
// Use the album's external URL if available.
const albumUrl = album.external_urls?.spotify;
if (albumUrl && !uniqueAlbums.has(albumUrl)) {
uniqueAlbums.set(albumUrl, album.name);
}
});
// Queue a download request for each unique album with a 20 ms delay between requests.
(async () => {
try {
for (const [url, name] of uniqueAlbums.entries()) {
await startDownload(url, 'album', { name });
await delay(20); // 20 ms delay between each request
}
downloadAlbumPlaylistBtn.textContent = 'Queued!';
} catch (err) {
showError('Failed to queue album downloads: ' + err.message);
downloadAlbumPlaylistBtn.disabled = false;
}
})();
});
// Render tracks list
const tracksList = document.getElementById('tracks-list');
tracksList.innerHTML = ''; // Clear any existing content
@@ -222,8 +166,8 @@ function showError(message) {
*/
function attachDownloadListeners() {
document.querySelectorAll('.download-btn').forEach((btn) => {
// Skip the whole playlist and album buttons.
if (btn.id === 'downloadPlaylistBtn' || btn.id === 'downloadAlbumPlaylistBtn') return;
// Skip the whole playlist button.
if (btn.id === 'downloadPlaylistBtn') return;
btn.addEventListener('click', (e) => {
e.stopPropagation();
const url = e.currentTarget.dataset.url;
@@ -278,7 +222,7 @@ async function startDownload(url, type, item, albumType) {
} else if (type === 'artist') {
apiUrl = `/api/artist/download?service=${service}&artist_url=${encodeURIComponent(url)}&album_type=${encodeURIComponent(albumType || 'album,single,compilation')}`;
} else {
// For album (and track) download, use the endpoint based on type.
// Default is track download.
apiUrl = `/api/${type}/download?service=${service}&url=${encodeURIComponent(url)}`;
}