This commit is contained in:
vapormusic 2022-02-10 16:41:26 +07:00
parent f4dda32e12
commit f71a22b8e9

View file

@ -78,27 +78,29 @@ export default class LastFMPlugin {
} }
} }
private async scrobbleSong(attributes: any) { private scrobbleSong(attributes: any) {
if(this._timer) clearTimeout(this._timer);
var self = this;
this._timer = setTimeout(() => { this._timer = setTimeout(() => {
const currentAttributes = attributes; const currentAttributes = attributes;
if (!this._lastfm || this._lastfm.cachedAttributes === attributes) { if (!self._lastfm || self._lastfm.cachedAttributes === attributes) {
return return
} }
if (this._lastfm.cachedAttributes) { if (self._lastfm.cachedAttributes) {
if (this._lastfm.cachedAttributes.playParams.id === attributes.playParams.id) return; if (self._lastfm.cachedAttributes.playParams.id === attributes.playParams.id) return;
} }
if (currentAttributes.status && currentAttributes === attributes) { if (currentAttributes.status && currentAttributes === attributes) {
if (fs.existsSync(this.sessionPath)) { if (fs.existsSync(this.sessionPath)) {
// Scrobble playing song. // Scrobble playing song.
if (attributes.status === true) { if (attributes.status === true) {
this._lastfm.track.scrobble({ self._lastfm.track.scrobble({
'artist': this.filterArtistName(attributes.artistName), 'artist': this.filterArtistName(attributes.artistName),
'track': attributes.name, 'track': attributes.name,
'album': attributes.albumName, 'album': attributes.albumName,
'albumArtist': this.filterArtistName(attributes.artistName), 'albumArtist': self.filterArtistName(attributes.artistName),
'timestamp': new Date().getTime() / 1000 'timestamp': new Date().getTime() / 1000
}, function (err: any, scrobbled: any) { }, function (err: any, scrobbled: any) {
if (err) { if (err) {
@ -107,14 +109,14 @@ export default class LastFMPlugin {
console.log('[LastFM] Successfully scrobbled: ', scrobbled); console.log('[LastFM] Successfully scrobbled: ', scrobbled);
}); });
this._lastfm.cachedAttributes = attributes self._lastfm.cachedAttributes = attributes
} }
} else { } else {
this.authenticate(); self.authenticate();
} }
} else { } else {
return console.log('[LastFM] Did not add ', attributes.name, '—', this.filterArtistName(attributes.artistName), 'because now playing a other song.'); return console.log('[LastFM] Did not add ', attributes.name, '—', self.filterArtistName(attributes.artistName), 'because now playing a other song.');
}},Math.round(attributes.durationInMillis * (this._store.lastfm.scrobble_after / 100))); }},Math.round(attributes.durationInMillis * (self._store.lastfm.scrobble_after / 100)));
} }
private filterArtistName(artist: any) { private filterArtistName(artist: any) {
@ -235,8 +237,8 @@ export default class LastFMPlugin {
* @param attributes Music Attributes (attributes.status = current state) * @param attributes Music Attributes (attributes.status = current state)
*/ */
onPlaybackStateDidChange(attributes: object): void { onPlaybackStateDidChange(attributes: object): void {
// this.scrobbleSong(attributes) this.updateNowPlayingSong(attributes)
// this.updateNowPlayingSong(attributes) this.scrobbleSong(attributes)
} }
/** /**
@ -246,10 +248,9 @@ export default class LastFMPlugin {
onNowPlayingItemDidChange(attributes: object): void { onNowPlayingItemDidChange(attributes: object): void {
if (!this._store.lastfm.filterLoop){ if (!this._store.lastfm.filterLoop){
this._lastfm.cachedNowPlayingAttributes = false; this._lastfm.cachedNowPlayingAttributes = false;
this._lastfm.cachedAttributes = false} this._lastfm.cachedAttributes = false}
this.scrobbleSong(attributes) this.updateNowPlayingSong(attributes)
this.updateNowPlayingSong(attributes) this.scrobbleSong(attributes)
if(this._timer) clearTimeout(this._timer)
} }
} }