From d4eac9a17c35bbb1fe8cfc5c0c8af31a49a94e82 Mon Sep 17 00:00:00 2001 From: Core <64542347+coredev-uk@users.noreply.github.com> Date: Sat, 30 Jul 2022 17:42:15 +0100 Subject: [PATCH] Exposed songid in attributes, reimplemented lastfm primary artist scrobbling, cleaned up mkinterop --- src/main/plugins/lastfm.ts | 4 ++-- src/preload/cider-preload.js | 36 ++++++++++++++++++++++++++++++------ src/renderer/main/vueapp.js | 6 ------ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/plugins/lastfm.ts b/src/main/plugins/lastfm.ts index 027f997c..ea4d071e 100644 --- a/src/main/plugins/lastfm.ts +++ b/src/main/plugins/lastfm.ts @@ -131,7 +131,7 @@ export default class lastfm { if (!attributes.lfmAlbum) { this._lfm.album.getInfo({ - "artist": attributes.artistName, + "artist": attributes.primaryArtist, "album": attributes.albumName }, (err: any, data: any) => { if (err) { @@ -144,7 +144,7 @@ export default class lastfm { } }) } else { - this._lfm.track.getCorrection(attributes.artistName, attributes.name, (err: any, data: any) => { + this._lfm.track.getCorrection(attributes.primaryArtist, attributes.name, (err: any, data: any) => { if (err) { console.error(`[${lastfm.name}] [track.getCorrection] Error: ${typeof err === "string" ? err : err.message}`) return {}; diff --git a/src/preload/cider-preload.js b/src/preload/cider-preload.js index 8afae876..b746f6ef 100644 --- a/src/preload/cider-preload.js +++ b/src/preload/cider-preload.js @@ -6,6 +6,7 @@ let cache = {playParams: {id: 0}, status: null, remainingTime: 0}, const MusicKitInterop = { init: function () { + /* MusicKit.Events.playbackStateDidChange */ MusicKit.getInstance().addEventListener(MusicKit.Events.playbackStateDidChange, () => { const attributes = MusicKitInterop.getAttributes() if (MusicKitInterop.filterTrack(attributes, true, false)) { @@ -14,19 +15,28 @@ const MusicKitInterop = { } }); - /** wsapi */ - MusicKit.getInstance().addEventListener(MusicKit.Events.playbackProgressDidChange, () => { - ipcRenderer.send('wsapi-updatePlaybackState', MusicKitInterop.getAttributes()); + /* MusicKit.Events.playbackProgressDidChange */ + MusicKit.getInstance().addEventListener(MusicKit.Events.playbackProgressDidChange, async () => { + const attributes = MusicKitInterop.getAttributes() + // wsapi call + 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 ? await this.fetchPrimaryArtist(attributes.artistName) : attributes.artistName; + ipcRenderer.send('lastfm:scrobbleTrack', attributes); + } }); - /** wsapi */ + /* MusicKit.Events.playbackTimeDidChange */ MusicKit.getInstance().addEventListener(MusicKit.Events.playbackTimeDidChange, () => { - ipcRenderer.send('mpris:playbackTimeDidChange', (MusicKit.getInstance()?.currentPlaybackTime * 1000 * 1000 ) ?? 0); - }) + ipcRenderer.send('mpris:playbackTimeDidChange', (MusicKit.getInstance()?.currentPlaybackTime * 1000 * 1000) ?? 0); + }); + /* MusicKit.Events.nowPlayingItemDidChange */ MusicKit.getInstance().addEventListener(MusicKit.Events.nowPlayingItemDidChange, async () => { console.debug('[cider:preload] nowPlayingItemDidChange') const attributes = MusicKitInterop.getAttributes() + attributes.primaryArtist = app.cfg.connectivity.lastfm.enabled ? await this.fetchPrimaryArtist(attributes.artistName) : attributes.artistName; if (MusicKitInterop.filterTrack(attributes, false, true)) { global.ipcRenderer.send('nowPlayingItemDidChange', attributes); @@ -40,18 +50,22 @@ const MusicKitInterop = { } }); + /* MusicKit.Events.authorizationStatusDidChange */ MusicKit.getInstance().addEventListener(MusicKit.Events.authorizationStatusDidChange, () => { global.ipcRenderer.send('authorizationStatusDidChange', MusicKit.getInstance().authorizationStatus) }); + /* MusicKit.Events.mediaPlaybackError */ MusicKit.getInstance().addEventListener(MusicKit.Events.mediaPlaybackError, (e) => { console.warn(`[cider:preload] mediaPlaybackError] ${e}`); }); + /* MusicKit.Events.shuffleModeDidChange */ MusicKit.getInstance().addEventListener(MusicKit.Events.shuffleModeDidChange, () => { global.ipcRenderer.send('shuffleModeDidChange', MusicKit.getInstance().shuffleMode) }); + /* MusicKit.Events.repeatModeDidChange */ MusicKit.getInstance().addEventListener(MusicKit.Events.repeatModeDidChange, () => { global.ipcRenderer.send('repeatModeDidChange', MusicKit.getInstance().repeatMode) }); @@ -63,6 +77,15 @@ 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 + } + }, + getAttributes: function () { const mk = MusicKit.getInstance() const nowPlayingItem = mk.nowPlayingItem; @@ -71,6 +94,7 @@ const MusicKitInterop = { const currentPlaybackProgress = mk.currentPlaybackProgress; const attributes = (nowPlayingItem != null ? nowPlayingItem.attributes : {}); + attributes.songId = attributes.songId ?? attributes.playParams?.catalogId ?? attributes.playParams?.id attributes.status = isPlayingExport ?? null; attributes.name = attributes?.name ?? 'no-title-found'; attributes.artwork = attributes?.artwork ?? {url: ''}; diff --git a/src/renderer/main/vueapp.js b/src/renderer/main/vueapp.js index bc5b03ff..b055fc86 100644 --- a/src/renderer/main/vueapp.js +++ b/src/renderer/main/vueapp.js @@ -949,12 +949,6 @@ const app = new Vue({ } }); - this.mk.addEventListener(MusicKit.Events.playbackProgressDidChange, () => { - if (self.mk.currentPlaybackProgress === (app.cfg.connectivity.lastfm.scrobble_after / 100)) { - ipcRenderer.send('lastfm:scrobbleTrack', MusicKitInterop.getAttributes()); - } - }) - this.mk.addEventListener(MusicKit.Events.playbackStateDidChange, (event) => { ipcRenderer.send('wsapi-updatePlaybackState', wsapi.getAttributes()); document.body.setAttribute("playback-state", event.state == 2 ? "playing" : "paused")