feat(dl): online track search and add-to-queue utility

This commit is contained in:
2025-10-02 09:54:05 -04:00
parent 81ef5bac90
commit d1edc8b7f7
6 changed files with 115 additions and 94 deletions

View File

@@ -0,0 +1,51 @@
/**
* Utility to add a Deezer track to the download queue
* Used by both search results and services/deezer pages
*/
import { deezerAPI } from '$lib/services/deezer';
import { addToQueue } from '$lib/stores/downloadQueue';
/**
* Fetch track metadata and add to download queue
* @param trackId - Deezer track ID
* @returns Promise that resolves when track is added to queue
*/
export async function addDeezerTrackToQueue(trackId: string): Promise<void> {
// Fetch full track data from GW API
const trackInfo = await deezerAPI.getTrack(trackId);
if (!trackInfo || !trackInfo.SNG_ID) {
throw new Error('Track not found or invalid track ID');
}
// Build track object
const track = {
id: trackInfo.SNG_ID,
title: trackInfo.SNG_TITLE,
artist: trackInfo.ART_NAME,
artistId: trackInfo.ART_ID,
artists: [trackInfo.ART_NAME],
album: trackInfo.ALB_TITLE,
albumId: trackInfo.ALB_ID,
albumArtist: trackInfo.ART_NAME,
albumArtistId: trackInfo.ART_ID,
trackNumber: trackInfo.TRACK_NUMBER || 1,
discNumber: trackInfo.DISK_NUMBER || 1,
duration: trackInfo.DURATION,
explicit: trackInfo.EXPLICIT_LYRICS === 1,
md5Origin: trackInfo.MD5_ORIGIN,
mediaVersion: trackInfo.MEDIA_VERSION,
trackToken: trackInfo.TRACK_TOKEN
};
// Add to queue
await addToQueue({
source: 'deezer',
type: 'track',
title: track.title,
artist: track.artist,
totalTracks: 1,
downloadObject: track
});
}