Fix Lidarr track indexing by querying tracks per albumId
This commit is contained in:
parent
51456faff4
commit
cc580600ae
1 changed files with 20 additions and 6 deletions
26
server.js
26
server.js
|
|
@ -128,7 +128,6 @@ function addTrackToLibraryIndex(track) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function rebuildLibraryIndex() {
|
async function rebuildLibraryIndex() {
|
||||||
const tracks = await lidarrRequest('get', '/api/v1/track');
|
|
||||||
const albums = await lidarrRequest('get', '/api/v1/album');
|
const albums = await lidarrRequest('get', '/api/v1/album');
|
||||||
const byTitle = new Map();
|
const byTitle = new Map();
|
||||||
const byAlbum = new Map();
|
const byAlbum = new Map();
|
||||||
|
|
@ -137,8 +136,19 @@ async function rebuildLibraryIndex() {
|
||||||
const prev = libraryIndexCache;
|
const prev = libraryIndexCache;
|
||||||
libraryIndexCache = { byTitle, byAlbum, trackById, updatedAt: Date.now() };
|
libraryIndexCache = { byTitle, byAlbum, trackById, updatedAt: Date.now() };
|
||||||
|
|
||||||
for (const track of tracks || []) {
|
let trackCount = 0;
|
||||||
addTrackToLibraryIndex(track);
|
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 || []) {
|
for (const album of albums || []) {
|
||||||
const k = buildAlbumKey(album.title || album.albumTitle || '', album.artistName || album.artist?.artistName || '');
|
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.
|
// Keep previous cache if rebuild produced nothing unexpectedly.
|
||||||
if ((tracks || []).length === 0 && prev.updatedAt > 0) {
|
if (trackCount === 0 && prev.updatedAt > 0) {
|
||||||
libraryIndexCache = prev;
|
libraryIndexCache = prev;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
trackCount: (tracks || []).length,
|
trackCount,
|
||||||
albumCount: (albums || []).length,
|
albumCount: (albums || []).length,
|
||||||
updatedAt: libraryIndexCache.updatedAt
|
updatedAt: libraryIndexCache.updatedAt
|
||||||
};
|
};
|
||||||
|
|
@ -159,7 +169,11 @@ async function rebuildLibraryIndex() {
|
||||||
async function ensureLibraryIndexFresh(maxAgeMs = 5 * 60 * 1000) {
|
async function ensureLibraryIndexFresh(maxAgeMs = 5 * 60 * 1000) {
|
||||||
const age = Date.now() - (libraryIndexCache.updatedAt || 0);
|
const age = Date.now() - (libraryIndexCache.updatedAt || 0);
|
||||||
if (!libraryIndexCache.updatedAt || age > maxAgeMs) {
|
if (!libraryIndexCache.updatedAt || age > maxAgeMs) {
|
||||||
await rebuildLibraryIndex();
|
try {
|
||||||
|
await rebuildLibraryIndex();
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Library index rebuild fehlgeschlagen:', err.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue