Fix Lidarr track indexing by querying tracks per albumId

This commit is contained in:
J0Z1L 2026-02-28 00:55:59 +01:00
parent 51456faff4
commit cc580600ae

View file

@ -128,7 +128,6 @@ function addTrackToLibraryIndex(track) {
}
async function rebuildLibraryIndex() {
const tracks = await lidarrRequest('get', '/api/v1/track');
const albums = await lidarrRequest('get', '/api/v1/album');
const byTitle = new Map();
const byAlbum = new Map();
@ -137,8 +136,19 @@ async function rebuildLibraryIndex() {
const prev = libraryIndexCache;
libraryIndexCache = { byTitle, byAlbum, trackById, updatedAt: Date.now() };
let trackCount = 0;
for (const album of albums || []) {
if (!album?.id) continue;
let tracks = [];
try {
tracks = await lidarrRequest('get', '/api/v1/track', undefined, { albumId: album.id });
} catch (_err) {
tracks = [];
}
for (const track of tracks || []) {
addTrackToLibraryIndex(track);
trackCount += 1;
}
}
for (const album of albums || []) {
const k = buildAlbumKey(album.title || album.albumTitle || '', album.artistName || album.artist?.artistName || '');
@ -146,11 +156,11 @@ async function rebuildLibraryIndex() {
}
// Keep previous cache if rebuild produced nothing unexpectedly.
if ((tracks || []).length === 0 && prev.updatedAt > 0) {
if (trackCount === 0 && prev.updatedAt > 0) {
libraryIndexCache = prev;
}
return {
trackCount: (tracks || []).length,
trackCount,
albumCount: (albums || []).length,
updatedAt: libraryIndexCache.updatedAt
};
@ -159,7 +169,11 @@ async function rebuildLibraryIndex() {
async function ensureLibraryIndexFresh(maxAgeMs = 5 * 60 * 1000) {
const age = Date.now() - (libraryIndexCache.updatedAt || 0);
if (!libraryIndexCache.updatedAt || age > maxAgeMs) {
try {
await rebuildLibraryIndex();
} catch (err) {
console.warn('Library index rebuild fehlgeschlagen:', err.message);
}
}
}