mirror of
https://github.com/markuryy/shark.git
synced 2025-12-12 11:41:02 +00:00
Add ability to download entire playlists as M3U8 files, with UI integration and per-track download actions. Implement track existence checking to avoid duplicate downloads, respecting the overwrite setting. Improve queue manager to sync downloaded tracks to the library incrementally. Refactor playlist parsing and metadata reading to use the Rust backend for better performance and accuracy. Update UI to reflect track existence and download status in playlist views. BREAKING CHANGE: Deezer playlist and track download logic now relies on Rust backend for metadata and new existence checking; some APIs and internal behaviors have changed.
94 lines
3.0 KiB
TypeScript
94 lines
3.0 KiB
TypeScript
/**
|
|
* Download Deezer playlist - adds tracks to queue and creates m3u8 file
|
|
*/
|
|
|
|
import { addDeezerTrackToQueue } from './addToQueue';
|
|
import { writeM3U8, makeRelativePath, type M3U8Track } from '$lib/library/m3u8';
|
|
import { generateTrackPath } from './paths';
|
|
import { settings } from '$lib/stores/settings';
|
|
import { get } from 'svelte/store';
|
|
import type { DeezerTrack } from '$lib/types/deezer';
|
|
import { mkdir } from '@tauri-apps/plugin-fs';
|
|
|
|
/**
|
|
* Download a Deezer playlist
|
|
* - Adds all tracks to the download queue (respects overwrite setting)
|
|
* - Creates an m3u8 playlist file with relative paths
|
|
*
|
|
* @param playlistName - Name of the playlist
|
|
* @param tracks - Array of DeezerTrack objects
|
|
* @param playlistsFolder - Path to playlists folder
|
|
* @param musicFolder - Path to music folder
|
|
* @returns Path to created m3u8 file
|
|
*/
|
|
export async function downloadDeezerPlaylist(
|
|
playlistName: string,
|
|
tracks: DeezerTrack[],
|
|
playlistsFolder: string,
|
|
musicFolder: string
|
|
): Promise<string> {
|
|
const appSettings = get(settings);
|
|
|
|
console.log(`[PlaylistDownloader] Starting download for playlist: ${playlistName}`);
|
|
console.log(`[PlaylistDownloader] Tracks: ${tracks.length}`);
|
|
|
|
// Ensure playlists folder exists
|
|
try {
|
|
await mkdir(playlistsFolder, { recursive: true });
|
|
} catch (error) {
|
|
// Folder might already exist
|
|
}
|
|
|
|
// Add all tracks to download queue
|
|
// Note: Tracks from cache don't have md5Origin/mediaVersion/trackToken needed for download
|
|
// So we need to call addDeezerTrackToQueue which fetches full data from API
|
|
// We add a small delay between requests to avoid rate limiting
|
|
let addedCount = 0;
|
|
let skippedCount = 0;
|
|
|
|
for (let i = 0; i < tracks.length; i++) {
|
|
const track = tracks[i];
|
|
try {
|
|
const result = await addDeezerTrackToQueue(track.id.toString());
|
|
if (result.added) {
|
|
addedCount++;
|
|
} else {
|
|
skippedCount++;
|
|
}
|
|
|
|
// Add delay between requests to avoid rate limiting (except after last track)
|
|
if (i < tracks.length - 1) {
|
|
await new Promise(resolve => setTimeout(resolve, 300));
|
|
}
|
|
} catch (error) {
|
|
console.error(`[PlaylistDownloader] Error adding track ${track.title}:`, error);
|
|
}
|
|
}
|
|
|
|
console.log(`[PlaylistDownloader] Added ${addedCount} tracks to queue, skipped ${skippedCount}`);
|
|
|
|
// Generate m3u8 file
|
|
const m3u8Tracks: M3U8Track[] = tracks.map(track => {
|
|
// Generate expected path for this track
|
|
const paths = generateTrackPath(track, musicFolder, appSettings.deezerFormat, false);
|
|
const absolutePath = `${paths.filepath}/${paths.filename}`;
|
|
|
|
// Convert to relative path from playlists folder
|
|
const relativePath = makeRelativePath(absolutePath, 'Music');
|
|
|
|
return {
|
|
duration: track.duration,
|
|
artist: track.artist,
|
|
title: track.title,
|
|
path: relativePath
|
|
};
|
|
});
|
|
|
|
// Write m3u8 file
|
|
const m3u8Path = await writeM3U8(playlistName, m3u8Tracks, playlistsFolder);
|
|
|
|
console.log(`[PlaylistDownloader] Playlist saved to: ${m3u8Path}`);
|
|
|
|
return m3u8Path;
|
|
}
|