From f98a82240b23228639fc588fd4c60af084e6240f Mon Sep 17 00:00:00 2001 From: Core <64542347+coredev-uk@users.noreply.github.com> Date: Wed, 15 Jun 2022 21:22:04 +0100 Subject: [PATCH] Now Playing Working Commented some annoying mpris debugs --- src/main/base/store.ts | 2 +- src/main/plugins/lastfm.ts | 102 ++++++++++++++++++++----------------- src/main/plugins/mpris.ts | 6 +-- 3 files changed, 60 insertions(+), 50 deletions(-) diff --git a/src/main/base/store.ts b/src/main/base/store.ts index 76b96a43..56b0743f 100644 --- a/src/main/base/store.ts +++ b/src/main/base/store.ts @@ -221,7 +221,7 @@ export class Store { }, "lastfm": { "enabled": false, - "scrobble_after": 30, + "scrobble_after": 50, "secrets": { "username": "", "key": "", diff --git a/src/main/plugins/lastfm.ts b/src/main/plugins/lastfm.ts index 902671e7..c49b0b2d 100644 --- a/src/main/plugins/lastfm.ts +++ b/src/main/plugins/lastfm.ts @@ -26,17 +26,10 @@ export default class lastfm { */ private _lfm: any = null; private _authenticated: boolean = false; + private _scrobbleDelay: any = null; private _utils: any = null; - private _activityCache: any = { - details: '', - state: '', - largeImageKey: '', - largeImageText: '', - smallImageKey: '', - smallImageText: '', - instance: false - }; - + private _scrobbleCache: any = {}; + private _nowPlayingCache: any = {}; /** * Public Methods @@ -74,9 +67,14 @@ export default class lastfm { * Runs on song change * @param attributes Music Attributes */ - onNowPlayingItemDidChange(attributes: object): void { + onNowPlayingItemDidChange(attributes: any): void { this._attributes = attributes + if (!attributes?.lfmTrack || !attributes?.lfmAlbum) { + this.verifyTrack(attributes) + return + } this.scrobbleTrack(attributes) + this.updateNowPlayingTrack(attributes) } /** @@ -139,7 +137,7 @@ export default class lastfm { if (data) { attributes.lfmAlbum = data } - this.scrobbleTrack(attributes) + this.onNowPlayingItemDidChange(attributes) }) } else { return this._lfm.track.getCorrection(attributes.artistName, attributes.name, (err: any, data: any) => { @@ -151,7 +149,7 @@ export default class lastfm { if (data) { attributes.lfmTrack = data.correction.track } - this.scrobbleTrack(attributes) + this.onNowPlayingItemDidChange(attributes) }) } @@ -164,52 +162,64 @@ export default class lastfm { * @private */ private scrobbleTrack(attributes: any): void { - if (!attributes?.lfmTrack || !attributes?.lfmAlbum) { - this.verifyTrack(attributes) - return + if (!this._authenticated || !attributes || (this._scrobbleCache.track === attributes.lfmTrack.name)) return; + + if (this._scrobbleDelay) { + clearTimeout(this._scrobbleDelay); } - if (!this._authenticated || !attributes) return; - // Scrobble + // Scrobble delay + this._scrobbleDelay = setTimeout(() => { - const scrobble = { + // Scrobble + const scrobble = { + 'artist': attributes.lfmTrack.artist.name, + 'track': attributes.lfmTrack.name, + 'album': attributes.lfmAlbum.name, + 'albumArtist': attributes.lfmAlbum.artist, + 'timestamp': new Date().getTime() / 1000, + 'trackNumber': attributes.trackNumber, + 'duration': attributes.durationInMillis / 1000, + } + + // Easy Debugging + if (!this._utils.getApp().isPackaged) { + console.debug(scrobble) + } + + // Scrobble the track + this._lfm.track.scrobble(scrobble, (err: any, _res: any) => { + if (err) { + console.error(`[${lastfm.name}] [lastfm:scrobble] Scrobble failed: ${err.message}`); + } else { + console.debug(`[${lastfm.name}] [lastfm:scrobble] Track scrobbled: ${scrobble.artist} - ${scrobble.track}`); + this._scrobbleCache = scrobble + } + }); + }, Math.round(attributes.durationInMillis * Math.min((this._utils.getStoreValue("lastfm.scrobble_after") / 100), 0.8))) + } + + private updateNowPlayingTrack(attributes: any): void { + if (!this._authenticated || !attributes || (this._nowPlayingCache.track === attributes.lfmTrack.name)) return; + + const nowPlaying = { 'artist': attributes.lfmTrack.artist.name, 'track': attributes.lfmTrack.name, 'album': attributes.lfmAlbum.name, - 'albumArtist': attributes.lfmAlbum.artist, - 'timestamp': new Date().getTime() / 1000, 'trackNumber': attributes.trackNumber, 'duration': attributes.durationInMillis / 1000, + 'albumArtist': attributes.lfmAlbum.artist, } - if (!this._utils.getApp().isPackaged) { - console.debug(scrobble) - } - this._lfm.track.scrobble(scrobble, (err: any, res: any) => { + + this._lfm.track.updateNowPlaying(nowPlaying, (err: any, res: any) => { if (err) { - console.error(`[${lastfm.name}] [lastfm:scrobble] Scrobble failed: ${err.message}`); + console.error(`[${lastfm.name}] [lastfm:updateNowPlaying] Now Playing Update failed: ${err.message}`); } else { - console.debug(`[${lastfm.name}] [lastfm:scrobble] Track scrobbled: ${res}`); + console.log(res) + console.debug(`[${lastfm.name}] [lastfm:updateNowPlaying] Now Playing Updated: ${nowPlaying.artist} - ${nowPlaying.track}`); + this._nowPlayingCache = nowPlaying } }); - this._activityCache = attributes - } - - private updateNowPlaying(attributes: any): void { - if (!this._authenticated) return; - this._lfm.track.updateNowPlaying({ - 'artist': attributes.artistName, - 'track': attributes.name, - 'album': attributes.albumName, - 'albumArtist': attributes.albumName, - 'trackNumber': attributes.trackNumber, - 'duration': attributes.duration / 1000, - }, function (err: any, scrobbled: any) { - if (err) { - return console.error('[LastFM] An error occurred while updating now playing', err); - } - - console.log('[LastFM] Successfully updated now playing: ', scrobbled); - }); } } \ No newline at end of file diff --git a/src/main/plugins/mpris.ts b/src/main/plugins/mpris.ts index 25730ee9..6b70c5c8 100644 --- a/src/main/plugins/mpris.ts +++ b/src/main/plugins/mpris.ts @@ -37,7 +37,7 @@ export default class mpris { * @private */ private static runMediaEvent(type: string) { - console.debug(`[Plugin][${this.name}] ${type}.`); + // console.debug(`[Plugin][${this.name}] ${type}.`); mpris.utils.getWindow().webContents.executeJavaScript(`MusicKitInterop.${type}()`).catch(console.error) } @@ -188,7 +188,7 @@ export default class mpris { */ @mpris.linuxOnly onPlaybackStateDidChange(attributes: object): void { - console.debug(`[Plugin][${mpris.name}] onPlaybackStateDidChange.`); + // console.debug(`[Plugin][${mpris.name}] onPlaybackStateDidChange.`); mpris.updatePlayerState(attributes) } @@ -198,7 +198,7 @@ export default class mpris { */ @mpris.linuxOnly onNowPlayingItemDidChange(attributes: object): void { - console.debug(`[Plugin][${mpris.name}] onMetadataDidChange.`); + // console.debug(`[Plugin][${mpris.name}] onMetadataDidChange.`); mpris.updatePlayer(attributes); }