feat(services): improve lyrics fetch progress and status updates

This commit is contained in:
2025-10-05 00:49:51 -04:00
parent ca5f79b23a
commit 369ea9df02

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { settings } from '$lib/stores/settings'; import { settings } from '$lib/stores/settings';
import { setSuccess, setWarning, setError, setInfo } from '$lib/stores/status'; import { setSuccess, setWarning, setError, setInfo, removeStatus } from '$lib/stores/status';
import { checkApiStatus, fetchAndSaveLyrics } from '$lib/services/lrclib'; import { checkApiStatus, fetchAndSaveLyrics } from '$lib/services/lrclib';
import { scanForTracksWithoutLyrics, loadCachedTracksWithoutLyrics, type TrackWithoutLyrics } from '$lib/library/lyricScanner'; import { scanForTracksWithoutLyrics, loadCachedTracksWithoutLyrics, type TrackWithoutLyrics } from '$lib/library/lyricScanner';
import { getLyricsScanTimestamp, upsertTrack } from '$lib/library/database'; import { getLyricsScanTimestamp, upsertTrack } from '$lib/library/database';
@@ -116,13 +116,16 @@
let successCount = 0; let successCount = 0;
let failCount = 0; let failCount = 0;
const totalTracks = tracks.length;
setInfo(`Fetching lyrics for ${tracks.length} tracks...`, 0); // Create a single status message that we'll update
const statusId = setInfo(`Fetching lyrics... 0/${totalTracks}`, 0);
const tracksCopy = [...tracks]; // Process tracks one by one, removing from array as we go
let processedCount = 0;
for (let i = 0; i < tracksCopy.length; i++) { while (tracks.length > 0) {
const track = tracksCopy[i]; const track = tracks[0]; // Always process first track
processedCount++;
try { try {
const result = await fetchAndSaveLyrics(track.path, { const result = await fetchAndSaveLyrics(track.path, {
@@ -133,23 +136,37 @@
}); });
if (result.success && (result.hasLyrics || result.instrumental)) { if (result.success && (result.hasLyrics || result.instrumental)) {
// Update database to mark track as having lyrics
await upsertTrack({
path: track.path,
title: track.title,
artist: track.artist,
album: track.album,
duration: Math.round(track.duration),
format: track.format,
has_lyrics: true
});
successCount++; successCount++;
// Remove from UI immediately on success
tracks = tracks.slice(1);
} else { } else {
failCount++; failCount++;
// Remove from list even if no lyrics found
tracks = tracks.slice(1);
} }
} catch (error) { } catch (error) {
failCount++; failCount++;
// Remove from list on error
tracks = tracks.slice(1);
} }
// Update progress // Update progress message
if ((i + 1) % 10 === 0 || i === tracksCopy.length - 1) { setInfo(`Fetching lyrics... ${processedCount}/${totalTracks}`, 0);
setInfo(`Fetching lyrics... ${i + 1}/${tracksCopy.length}`, 0);
}
} }
// Rescan to update the list // Remove the progress message
tracks = []; removeStatus(statusId);
await handleScan();
// Show completion message // Show completion message
if (successCount > 0 && failCount > 0) { if (successCount > 0 && failCount > 0) {
@@ -377,12 +394,6 @@
opacity: 0.6; opacity: 0.6;
} }
.status-row {
display: flex;
align-items: center;
gap: 12px;
}
.status-indicator { .status-indicator {
font-weight: bold; font-weight: bold;
font-size: 11px; font-size: 11px;