From 9eea5d356125e4aa8ab7d00a51bd1a92eefec228 Mon Sep 17 00:00:00 2001 From: Core Date: Sun, 28 Aug 2022 19:03:51 +0100 Subject: [PATCH] Fixed remove primary artist stuff for lastfm --- src/preload/cider-preload.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/preload/cider-preload.js b/src/preload/cider-preload.js index 380f0696..8beb6860 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.enabled && app.cfg.connectivity.lastfm.remove_featured ? await this.fetchPrimaryArtist(attributes.artistName) : attributes.artistName; + attributes.primaryArtist = app.cfg.connectivity.lastfm.remove_featured ? await this.fetchPrimaryArtist() : 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.enabled && app.cfg.connectivity.lastfm.remove_featured ? await this.fetchPrimaryArtist(attributes.artistName) : attributes.artistName; + attributes.primaryArtist = app.cfg.connectivity.lastfm.remove_featured ? await this.fetchPrimaryArtist() : attributes.artistName; if (MusicKitInterop.filterTrack(attributes, false, true)) { global.ipcRenderer.send("nowPlayingItemDidChange", attributes); @@ -81,13 +81,31 @@ const MusicKitInterop = { }); }, - async fetchPrimaryArtist(artist) { - if (app.mk.nowPlayingItem?.relationships?.artists) { - const artist = await app.mk.api.artist(app.mk.nowPlayingItem.relationships.artists.data[0].id); - return artist.attributes.name; - } else { - return artist; + async fetchPrimaryArtist() { + 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}`, { + include: { + songs: ["artists"] + } + }) + if (!res || !res.data) { + console.warn("[cider:preload] fetchPrimaryArtist: no response"); + 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 primaryArtist = artistData[0] + return primaryArtist.attributes.name }, getAttributes: function () {