feat(auth): purple app track fetch

This commit is contained in:
2025-09-30 22:38:16 -04:00
parent 48d8b4a593
commit 4ebb77f341
9 changed files with 1094 additions and 20 deletions

View File

@@ -2,6 +2,8 @@
import { onMount } from 'svelte';
import { deezerAuth, loadDeezerAuth, saveArl, saveUser, clearDeezerAuth } from '$lib/stores/deezer';
import { deezerAPI } from '$lib/services/deezer';
import { downloadTrack } from '$lib/services/deezer/downloader';
import { settings } from '$lib/stores/settings';
let arlInput = $state('');
let isLoading = $state(false);
@@ -10,6 +12,14 @@
let testingAuth = $state(false);
let authTestResult = $state<string | null>(null);
// Track download test
let trackIdInput = $state('3135556'); // Default: Daft Punk - One More Time
let isFetchingTrack = $state(false);
let isDownloading = $state(false);
let trackInfo = $state<any>(null);
let downloadStatus = $state('');
let downloadError = $state('');
onMount(async () => {
await loadDeezerAuth();
});
@@ -73,6 +83,104 @@
testingAuth = false;
}
}
async function fetchTrackInfo() {
if (!$deezerAuth.arl || !$deezerAuth.user) {
downloadError = 'Not logged in';
return;
}
isFetchingTrack = true;
downloadError = '';
trackInfo = null;
try {
deezerAPI.setArl($deezerAuth.arl);
const trackData = await deezerAPI.getTrack(trackIdInput);
console.log('Track data:', trackData);
if (!trackData || !trackData.SNG_ID) {
throw new Error('Track not found or invalid track ID');
}
trackInfo = trackData;
} catch (error) {
console.error('Fetch error:', error);
downloadError = error instanceof Error ? error.message : 'Failed to fetch track';
} finally {
isFetchingTrack = false;
}
}
async function downloadTrackNow() {
if (!trackInfo) {
downloadError = 'Please fetch track info first';
return;
}
if (!$settings.musicFolder) {
downloadError = 'Please set a music folder in Settings first';
return;
}
isDownloading = true;
downloadStatus = 'Getting download URL...';
downloadError = '';
try {
const format = $deezerAuth.user!.can_stream_lossless ? 'FLAC' : 'MP3_320';
const downloadURL = await deezerAPI.getTrackDownloadUrl(
trackInfo.TRACK_TOKEN,
format,
$deezerAuth.user!.license_token!
);
if (!downloadURL) {
throw new Error('Could not get download URL');
}
downloadStatus = 'Downloading and decrypting...';
console.log('Download URL:', downloadURL);
// 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, // Simplified for test
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
};
// Download track
const filePath = await downloadTrack(
track,
downloadURL,
$settings.musicFolder,
format
);
downloadStatus = `✓ Downloaded successfully to: ${filePath}`;
console.log('Download complete:', filePath);
} catch (error) {
console.error('Download error:', error);
downloadError = error instanceof Error ? error.message : 'Download failed';
downloadStatus = '';
} finally {
isDownloading = false;
}
}
</script>
<div class="deezer-page">
@@ -177,25 +285,61 @@
</div>
</section>
<!-- Test Authentication -->
<!-- Test Track Download -->
<section class="window test-section">
<div class="title-bar">
<div class="title-bar-text">Test Authentication</div>
<div class="title-bar-text">Test Track Download</div>
</div>
<div class="window-body">
<p>Test if your authentication is working:</p>
<p>Download a test track to verify decryption is working:</p>
{#if authTestResult}
<div class={authTestResult.startsWith('✓') ? 'success-message' : 'error-message'}>
{authTestResult}
<div class="field-row-stacked">
<label for="track-id">Track ID (from Deezer URL)</label>
<input
id="track-id"
type="text"
bind:value={trackIdInput}
placeholder="e.g., 3135556"
disabled={isFetchingTrack || isDownloading}
/>
<small class="help-text">Default: 3135556 (Daft Punk - One More Time)</small>
</div>
{#if trackInfo}
<div class="track-info">
<strong>{trackInfo.SNG_TITLE}</strong> by {trackInfo.ART_NAME}
<br>
<small>Album: {trackInfo.ALB_TITLE} • Duration: {Math.floor(trackInfo.DURATION / 60)}:{String(trackInfo.DURATION % 60).padStart(2, '0')}</small>
</div>
{/if}
{#if downloadStatus}
<div class="success-message">
{downloadStatus}
</div>
{/if}
{#if downloadError}
<div class="error-message">
{downloadError}
</div>
{/if}
<div class="button-row">
<button onclick={testAuthentication} disabled={testingAuth}>
{testingAuth ? 'Testing...' : 'Test Authentication'}
<button onclick={fetchTrackInfo} disabled={isFetchingTrack || isDownloading}>
{isFetchingTrack ? 'Fetching...' : 'Fetch Track Info'}
</button>
<button onclick={downloadTrackNow} disabled={!trackInfo || isDownloading || !$settings.musicFolder}>
{isDownloading ? 'Downloading...' : 'Download'}
</button>
</div>
{#if !$settings.musicFolder}
<p class="help-text" style="margin-top: 8px;">
⚠ Please set a music folder in Settings before downloading.
</p>
{/if}
</div>
</section>
{/if}
@@ -239,11 +383,18 @@
min-width: 140px;
}
input[type="password"] {
input[type="password"],
input[type="text"] {
width: 100%;
padding: 4px;
}
.help-text {
color: var(--text-color, #FFFFFF);
opacity: 0.7;
font-size: 0.85em;
}
.button-row {
margin-top: 12px;
display: flex;
@@ -300,4 +451,15 @@
.user-info {
margin-bottom: 12px;
}
.track-info {
padding: 8px;
margin: 8px 0;
background-color: var(--button-shadow, #2a2a2a);
border: 1px solid var(--button-highlight, #606060);
}
.track-info strong {
font-weight: bold;
}
</style>