diff --git a/src/preload/cider-preload.js b/src/preload/cider-preload.js index 47d2e35b..8a5ca40c 100644 --- a/src/preload/cider-preload.js +++ b/src/preload/cider-preload.js @@ -22,7 +22,7 @@ const MusicKitInterop = { ipcRenderer.send("wsapi-updatePlaybackState", attributes); // lastfm call if (app.mk.currentPlaybackProgress === app.cfg.connectivity.lastfm.scrobble_after / 100) { - attributes.primaryArtist = app.cfg.connectivity.lastfm.remove_featured ? await this.fetchPrimaryArtist() : attributes.artistName; + attributes.primaryArtist = app.cfg.connectivity.lastfm.remove_featured ? await this.fetchSongRelationships() : attributes.artistName; ipcRenderer.send("lastfm:scrobbleTrack", attributes); } }); @@ -36,7 +36,7 @@ const MusicKitInterop = { MusicKit.getInstance().addEventListener(MusicKit.Events.nowPlayingItemDidChange, async () => { console.debug("[cider:preload] nowPlayingItemDidChange"); const attributes = MusicKitInterop.getAttributes(); - attributes.primaryArtist = app.cfg.connectivity.lastfm.remove_featured ? await this.fetchPrimaryArtist() : attributes.artistName; + attributes.primaryArtist = app.cfg.connectivity.lastfm.remove_featured ? await this.fetchSongRelationships() : attributes.artistName; global.ipcRenderer.send("nowPlayingItemDidChange", attributes); @@ -83,34 +83,45 @@ const MusicKitInterop = { }); }, - async fetchPrimaryArtist(returnType = "name") { - const songID = app.mk.nowPlayingItem.attributes.playParams.catalogId || app.mk.nowPlayingItem.attributes.playParams.id; - const res = await MusicKit.getInstance().api.v3.music("/v1/catalog/" + MusicKit.getInstance().storefrontId + `/songs/${songID}`, { + async fetchSongRelationships({id = app.mk.nowPlayingItem.attributes.playParams.catalogId || app.mk.nowPlayingItem.attributes.playParams.id, relationship = "primaryName"}={}) { + const res = await MusicKit.getInstance().api.v3.music("/v1/catalog/" + MusicKit.getInstance().storefrontId + `/songs/${id}`, { include: { songs: ["artists"], }, }); + if (!res || !res.data) { - console.warn("[cider:preload] fetchPrimaryArtist: no response"); - return app.mk.nowPlayingItem.attributes.artistName; + console.warn("[cider:preload] fetchSongRelationships: no response"); + if (id === app.mk.nowPlayingItem.attributes.playParams.catalogId || id === app.mk.nowPlayingItem.attributes.playParams.id) { + return app.mk.nowPlayingItem.attributes.artistName; + } + } + if (!res.data.data.length) { + console.error(`[cider:preload] fetchSongRelationships: Unable to locate song with id of ${id}`); + if (id === app.mk.nowPlayingItem.attributes.playParams.catalogId || id === app.mk.nowPlayingItem.attributes.playParams.id) { + return app.mk.nowPlayingItem.attributes.artistName; + } } - if (!res.data.data.length) { - console.error(`[cider:preload] fetchPrimaryArtist: Unable to locate song with id of ${songID}`); - return app.mk.nowPlayingItem.attributes.artistName; - } const songData = res.data.data[0]; const artistData = songData.relationships.artists.data; - if (artistData.length < 1) { - console.error(`[cider:preload] fetchPrimaryArtist: Unable to find artists related to the song with id of ${songID}`); - return app.mk.nowPlayingItem.attributes.artistName; - } - + const albumData = songData.relationships.albums.data; const primaryArtist = artistData[0]; - if (returnType === "name") { - return primaryArtist.attributes.name; - } else if (returnType === "id") { - return primaryArtist.id; + + switch(relationship) { + default: + case "primaryName": + if (artistData.length < 1) { + console.error(`[cider:preload] fetchSongRelationships: Unable to find artists related to the song with id of ${id}`); + return app.mk.nowPlayingItem.attributes.artistName; + } + return primaryArtist.attributes.name; + + case "primaryArtist": + return primaryArtist; + + case "album": + return albumData[0] } }, diff --git a/src/renderer/main/vueapp.js b/src/renderer/main/vueapp.js index b0210d22..23164f60 100644 --- a/src/renderer/main/vueapp.js +++ b/src/renderer/main/vueapp.js @@ -2145,6 +2145,18 @@ const app = new Vue({ } // app.getTypeFromID((kind), (id), (isLibrary), params); + } else if (kind.toString().includes("song")) { + const albumUrl = new Promise(async (resolve, reject) => { + resolve(await MusicKitInterop.fetchSongRelationships({id: id, relationship: "album"})); + }); + albumUrl.then((data) => { + if (data && data.type === "albums" && data.id) { + window.location.hash = `album/${data.id}${isLibrary ? "/" + isLibrary : ""}`; + } else { + app.playMediaItemById(id, kind, isLibrary, item.attributes.url ?? ""); + } + }) + } else { app.playMediaItemById(id, kind, isLibrary, item.attributes.url ?? ""); } @@ -4542,8 +4554,8 @@ const app = new Vue({ if (app.mk.nowPlayingItem.relationships.artists.data[0].id) { app.appRoute(`artist/${app.mk.nowPlayingItem.relationships.artists.data[0].id}`); } else { - const id = await MusicKitInterop.fetchPrimaryArtist("id"); - app.appRoute(`artist/${id}`); + const primaryArtist = await MusicKitInterop.fetchSongRelationships({ relationship: "primaryArtist"}) + app.appRoute(`artist/${primaryArtist.id}`); } }, },