Fix albums breaking (ai saving my ass rn)
This commit is contained in:
parent
b01a19be03
commit
c0543b15a5
1 changed files with 50 additions and 15 deletions
65
js/api.js
65
js/api.js
|
|
@ -283,25 +283,47 @@ export class LosslessAPI {
|
||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
|
|
||||||
const response = await this.fetchWithRetry(`/album/?id=${id}`);
|
const response = await this.fetchWithRetry(`/album/?id=${id}`);
|
||||||
const data = await response.json();
|
const jsonData = await response.json();
|
||||||
const entries = Array.isArray(data) ? data : [data];
|
|
||||||
|
// Unwrap the data property if it exists
|
||||||
|
const data = jsonData.data || jsonData;
|
||||||
|
|
||||||
let album, tracksSection;
|
let album, tracksSection;
|
||||||
|
|
||||||
for (const entry of entries) {
|
if (data && typeof data === 'object' && !Array.isArray(data)) {
|
||||||
if (!entry || typeof entry !== 'object') continue;
|
// Check for album metadata at root level
|
||||||
|
if ('numberOfTracks' in data || 'title' in data) {
|
||||||
if (!album && 'numberOfTracks' in entry) {
|
album = this.prepareAlbum(data);
|
||||||
album = this.prepareAlbum(entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tracksSection && 'items' in entry) {
|
// Set tracksSection if items exist
|
||||||
tracksSection = entry;
|
if ('items' in data) {
|
||||||
|
tracksSection = data;
|
||||||
|
|
||||||
|
// If we still don't have album but have items with tracks, try to extract album from first track
|
||||||
|
if (!album && data.items && data.items.length > 0) {
|
||||||
|
const firstItem = data.items[0];
|
||||||
|
const track = firstItem.item || firstItem;
|
||||||
|
|
||||||
|
// Check if track has album property
|
||||||
|
if (track && track.album) {
|
||||||
|
album = this.prepareAlbum(track.album);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!album) throw new Error('Album not found');
|
if (!album) throw new Error('Album not found');
|
||||||
|
|
||||||
|
// If album exists but has no artist, try to extract from tracks
|
||||||
|
if (album && !album.artist && tracksSection?.items && tracksSection.items.length > 0) {
|
||||||
|
const firstTrack = tracksSection.items[0];
|
||||||
|
const track = firstTrack.item || firstTrack;
|
||||||
|
if (track && track.artist) {
|
||||||
|
album = { ...album, artist: track.artist };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const tracks = (tracksSection?.items || []).map(i => this.prepareTrack(i.item || i));
|
const tracks = (tracksSection?.items || []).map(i => this.prepareTrack(i.item || i));
|
||||||
const result = { album, tracks };
|
const result = { album, tracks };
|
||||||
|
|
||||||
|
|
@ -314,7 +336,10 @@ export class LosslessAPI {
|
||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
|
|
||||||
const response = await this.fetchWithRetry(`/playlist/?id=${id}`);
|
const response = await this.fetchWithRetry(`/playlist/?id=${id}`);
|
||||||
const data = await response.json();
|
const jsonData = await response.json();
|
||||||
|
|
||||||
|
// Unwrap the data property if it exists
|
||||||
|
const data = jsonData.data || jsonData;
|
||||||
const entries = Array.isArray(data) ? data : [data];
|
const entries = Array.isArray(data) ? data : [data];
|
||||||
|
|
||||||
let playlist, tracksSection;
|
let playlist, tracksSection;
|
||||||
|
|
@ -322,7 +347,7 @@ export class LosslessAPI {
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
if (!entry || typeof entry !== 'object') continue;
|
if (!entry || typeof entry !== 'object') continue;
|
||||||
|
|
||||||
if (!playlist && ('uuid' in entry || 'numberOfTracks' in entry)) {
|
if (!playlist && ('uuid' in entry || 'numberOfTracks' in entry || 'title' in entry && 'id' in entry)) {
|
||||||
playlist = entry;
|
playlist = entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -331,6 +356,16 @@ export class LosslessAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If still no playlist found, try using the first valid entry
|
||||||
|
if (!playlist && entries.length > 0) {
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (entry && typeof entry === 'object' && ('id' in entry || 'uuid' in entry)) {
|
||||||
|
playlist = entry;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!playlist) throw new Error('Playlist not found');
|
if (!playlist) throw new Error('Playlist not found');
|
||||||
|
|
||||||
const tracks = (tracksSection?.items || []).map(i => this.prepareTrack(i.item || i));
|
const tracks = (tracksSection?.items || []).map(i => this.prepareTrack(i.item || i));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue