From a8042346460d193ac7525e8f235ae47fe7ce042e Mon Sep 17 00:00:00 2001 From: Chase Ingebritson Date: Sun, 13 Feb 2022 18:01:43 -0600 Subject: [PATCH 1/4] Added a network request to pull artist names on scrobbling --- src/main/plugins/lastfm.ts | 70 ++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/src/main/plugins/lastfm.ts b/src/main/plugins/lastfm.ts index 10440584..6978c8fb 100644 --- a/src/main/plugins/lastfm.ts +++ b/src/main/plugins/lastfm.ts @@ -81,7 +81,7 @@ export default class LastFMPlugin { private scrobbleSong(attributes: any) { if(this._timer) clearTimeout(this._timer); var self = this; - this._timer = setTimeout(() => { + this._timer = setTimeout(async () => { const currentAttributes = attributes; if (!self._lastfm || self._lastfm.cachedAttributes === attributes) { @@ -92,15 +92,17 @@ export default class LastFMPlugin { if (self._lastfm.cachedAttributes.playParams.id === attributes.playParams.id) return; } + const artist = await this.getPrimaryArtist(attributes) + if (currentAttributes.status && currentAttributes === attributes) { if (fs.existsSync(this.sessionPath)) { // Scrobble playing song. if (attributes.status === true) { self._lastfm.track.scrobble({ - 'artist': this.filterArtistName(attributes.artistName), + 'artist': artist, 'track': attributes.name, 'album': attributes.albumName, - 'albumArtist': self.filterArtistName(attributes.artistName), + 'albumArtist': artist, 'timestamp': new Date().getTime() / 1000 }, function (err: any, scrobbled: any) { if (err) { @@ -115,29 +117,11 @@ export default class LastFMPlugin { self.authenticate(); } } else { - return console.log('[LastFM] Did not add ', attributes.name, '—', self.filterArtistName(attributes.artistName), 'because now playing a other song.'); + return console.log('[LastFM] Did not add ', attributes.name, '—', artist, 'because now playing a other song.'); }},Math.round(attributes.durationInMillis * (self._store.lastfm.scrobble_after / 100))); } - private filterArtistName(artist: any) { - if (!this._store.lastfm.enabledRemoveFeaturingArtists) return artist; - - artist = artist.split(' '); - if (artist.includes('&')) { - artist.length = artist.indexOf('&'); - } - if (artist.includes('and')) { - artist.length = artist.indexOf('and'); - } - artist = artist.join(' '); - if (artist.includes(',')) { - artist = artist.split(',') - artist = artist[0] - } - return artist.charAt(0).toUpperCase() + artist.slice(1); - } - - private updateNowPlayingSong(attributes: any) { + private async updateNowPlayingSong(attributes: any) { if (!this._lastfm || this._lastfm.cachedNowPlayingAttributes === attributes || !this._store.lastfm.NowPlaying) { return } @@ -147,13 +131,15 @@ export default class LastFMPlugin { } if (fs.existsSync(this.sessionPath)) { + const artist = await this.getPrimaryArtist(attributes) + // update Now Playing if (attributes.status === true) { this._lastfm.track.updateNowPlaying({ - 'artist': this.filterArtistName(attributes.artistName), + 'artist': artist, 'track': attributes.name, 'album': attributes.albumName, - 'albumArtist': this.filterArtistName(attributes.artistName) + 'albumArtist': artist }, function (err: any, nowPlaying: any) { if (err) { return console.error('[LastFM] An error occurred while updating nowPlayingSong', err); @@ -169,6 +155,40 @@ export default class LastFMPlugin { } } + private async getPrimaryArtist (attributes: any) { + const songId = attributes.playParams.catalogId || attributes.playParams.id + + if (!this._store.lastfm.enabledRemoveFeaturingArtists || !songId) return attributes.artistName; + + const res = await this._win.webContents.executeJavaScript(` + (async () => { + const subMk = await MusicKit.getInstance().api.v3.music('/v1/catalog/us/songs/${songId}', { + include: { + songs: ["artists"] + } + }) + if (!subMk) console.error('[LastFM] Request failed: /v1/catalog/us/songs/${songId}') + return subMk.data + })() + `).catch(console.error) + if (!res) return attributes.artistName + + const data = res.data + if (!data.length) { + console.error(`[LastFM] Unable to locate song with id of ${songId}`) + return attributes.artistName; + } + + const artists = res.data[0].relationships.artists.data + if (!artists.length) { + console.error(`[LastFM] Unable to find artists related to the song with id of ${songId}`) + return attributes.artistName; + } + + const primaryArtist = artists[0] + return primaryArtist.attributes.name + } + /** * Base Plugin Details (Eventually implemented into a GUI in settings) */ From ade11cd717ea267927a36167bb2947fc40e85027 Mon Sep 17 00:00:00 2001 From: vapormusic Date: Mon, 14 Feb 2022 08:14:27 +0700 Subject: [PATCH 2/4] Update lastfm.ts --- src/main/plugins/lastfm.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/plugins/lastfm.ts b/src/main/plugins/lastfm.ts index 6978c8fb..ac56ad1b 100644 --- a/src/main/plugins/lastfm.ts +++ b/src/main/plugins/lastfm.ts @@ -162,7 +162,7 @@ export default class LastFMPlugin { const res = await this._win.webContents.executeJavaScript(` (async () => { - const subMk = await MusicKit.getInstance().api.v3.music('/v1/catalog/us/songs/${songId}', { + const subMk = await MusicKit.getInstance().api.v3.music(`/v1/catalog/${MusicKit.getInstance().storefrontId}/songs/${songId}`, { include: { songs: ["artists"] } @@ -273,4 +273,4 @@ export default class LastFMPlugin { this.scrobbleSong(attributes) } -} \ No newline at end of file +} From 3466869a3417c6f9434a7d59389157d55fb3d000 Mon Sep 17 00:00:00 2001 From: Core <64542347+coredev-uk@users.noreply.github.com> Date: Mon, 14 Feb 2022 13:00:59 +0000 Subject: [PATCH 3/4] Fix the ts error --- src/main/plugins/lastfm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/plugins/lastfm.ts b/src/main/plugins/lastfm.ts index ac56ad1b..1b6b9827 100644 --- a/src/main/plugins/lastfm.ts +++ b/src/main/plugins/lastfm.ts @@ -162,7 +162,7 @@ export default class LastFMPlugin { const res = await this._win.webContents.executeJavaScript(` (async () => { - const subMk = await MusicKit.getInstance().api.v3.music(`/v1/catalog/${MusicKit.getInstance().storefrontId}/songs/${songId}`, { + const subMk = await MusicKit.getInstance().api.v3.music("/v1/catalog/${MusicKit.getInstance().storefrontId}/songs/${songId}", { include: { songs: ["artists"] } From 3653937f69d6750bc7bb6778d1a116b671a81c0e Mon Sep 17 00:00:00 2001 From: Core <64542347+coredev-uk@users.noreply.github.com> Date: Mon, 14 Feb 2022 13:31:11 +0000 Subject: [PATCH 4/4] Fixed the mk reference --- src/main/plugins/lastfm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/plugins/lastfm.ts b/src/main/plugins/lastfm.ts index 1b6b9827..6110ad31 100644 --- a/src/main/plugins/lastfm.ts +++ b/src/main/plugins/lastfm.ts @@ -162,7 +162,7 @@ export default class LastFMPlugin { const res = await this._win.webContents.executeJavaScript(` (async () => { - const subMk = await MusicKit.getInstance().api.v3.music("/v1/catalog/${MusicKit.getInstance().storefrontId}/songs/${songId}", { + const subMk = await MusicKit.getInstance().api.v3.music("/v1/catalog/" + MusicKit.getInstance().storefrontId + "/songs/${songId}", { include: { songs: ["artists"] }