set timeout can go away now

This commit is contained in:
Core 2022-07-11 02:02:11 +01:00
parent c410809b68
commit 23b6033432
No known key found for this signature in database
GPG key ID: FE9BF1B547F8F3C6
2 changed files with 68 additions and 59 deletions

View file

@ -7,10 +7,7 @@ export default class lastfm {
public version: string = '2.0.0'; public version: string = '2.0.0';
public author: string = 'Core (Cider Collective)'; public author: string = 'Core (Cider Collective)';
/**
* Private variables for interaction in plugins
*/
private _attributes: any;
private _apiCredentials = { private _apiCredentials = {
key: "f9986d12aab5a0fe66193c559435ede3", key: "f9986d12aab5a0fe66193c559435ede3",
secret: "acba3c29bd5973efa38cc2f0b63cc625" secret: "acba3c29bd5973efa38cc2f0b63cc625"
@ -31,31 +28,36 @@ export default class lastfm {
constructor(utils: any) { constructor(utils: any) {
this._utils = utils; this._utils = utils;
this.initializeLastFM("", this._apiCredentials)
} }
onReady(_win: Electron.BrowserWindow): void { onReady(_win: Electron.BrowserWindow): void {
this.initializeLastFM("", this._apiCredentials)
// Register the ipcMain handlers // Register the ipcMain handlers
this._utils.getIPCMain().handle('lastfm:url', (event: any) => { this._utils.getIPCMain().handle('lastfm:url', (event: any) => {
console.debug(`${lastfm.name}:url`) console.debug(`[${lastfm.name}:url] Called.`)
return this._lfm.getAuthenticationUrl({"cb": "cider://auth/lastfm"}) return this._lfm.getAuthenticationUrl({"cb": "cider://auth/lastfm"})
}) })
this._utils.getIPCMain().on('lastfm:auth', (event: any, token: string) => { this._utils.getIPCMain().on('lastfm:auth', (event: any, token: string) => {
console.debug(`${lastfm.name}:auth`, token) console.debug(`[${lastfm.name}:auth] Token: `, token)
this.authenticateLastFM(token) this.authenticateLastFM(token)
}) })
this._utils.getIPCMain().on('lastfm:disconnect', (_event: any) => { this._utils.getIPCMain().on('lastfm:disconnect', (_event: any) => {
this._lfm.setSessionCredentials(null, null); this._lfm.setSessionCredentials(null, null);
this._authenticated = false; this._authenticated = false;
console.debug(`${lastfm.name}:disconnect`) console.debug(`[${lastfm.name}:disconnect] Disconnected`)
}) })
this._utils.getIPCMain().on('lastfm:nowPlayingChange', (event: any, attributes: any) => { this._utils.getIPCMain().on('lastfm:nowPlayingChange', (event: any, attributes: any) => {
if (this._utils.getStoreValue("connectivity.lastfm.filter_loop")) return; if (this._utils.getStoreValue("connectivity.lastfm.filter_loop") || this._utils.getStoreValue("general.privateEnabled")) return;
this.onNowPlayingItemDidChange(attributes) this.updateNowPlayingTrack(attributes)
})
this._utils.getIPCMain().on('lastfm:scrobbleTrack', (event: any, attributes: any) => {
if (this._utils.getStoreValue("general.privateEnabled")) return;
this.scrobbleTrack(attributes)
}) })
} }
@ -64,22 +66,15 @@ export default class lastfm {
* @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._attributes = attributes
// this.scrobbleTrack(attributes)
} }
/** /**
* Runs on song change * Runs on song change
* @param attributes Music Attributes * @param attributes Music Attributes
* @param scrobble
*/ */
onNowPlayingItemDidChange(attributes: any): void { onNowPlayingItemDidChange(attributes: any, scrobble = false): void {
if (this._utils.getStoreValue("general.privateEnabled")) return; if (this._utils.getStoreValue("general.privateEnabled")) return;
this._attributes = attributes
if (!attributes?.lfmTrack || !attributes?.lfmAlbum) {
this.verifyTrack(attributes)
return
}
this.scrobbleTrack(attributes)
this.updateNowPlayingTrack(attributes) this.updateNowPlayingTrack(attributes)
} }
@ -90,6 +85,7 @@ export default class lastfm {
* @private * @private
*/ */
private initializeLastFM(token: string, api: { key: string, secret: string }): void { private initializeLastFM(token: string, api: { key: string, secret: string }): void {
console.debug(`[${lastfm.name}:initialize] Initializing LastFM`)
const LastfmAPI = require("lastfmapi") const LastfmAPI = require("lastfmapi")
this._lfm = new LastfmAPI({ this._lfm = new LastfmAPI({
'api_key': api.key, 'api_key': api.key,
@ -113,7 +109,7 @@ export default class lastfm {
if (!token) return; if (!token) return;
this._lfm.authenticate(token, (err: any, session: any) => { this._lfm.authenticate(token, (err: any, session: any) => {
if (err) { if (err) {
console.error(err); console.error(`[${lastfm.name}:authenticate] Error: ${typeof err === "string" ? err : err.message}`);
this._utils.getWindow().webContents.executeJavaScript(`app.notyf.error("${err.message}");`) this._utils.getWindow().webContents.executeJavaScript(`app.notyf.error("${err.message}");`)
return; return;
@ -127,37 +123,36 @@ export default class lastfm {
/** /**
* Verifies the track information with lastfm * Verifies the track information with lastfm
* @param attributes * @param attributes
* @param callback
* @private * @private
*/ */
private verifyTrack(attributes: any): object { private verifyTrack(attributes: any, callback: Function): void {
if (!attributes) return attributes; if (!attributes) return attributes;
if (!attributes.lfmAlbum) { if (!attributes.lfmAlbum) {
return this._lfm.album.getInfo({ this._lfm.album.getInfo({
"artist": attributes.artistName, "artist": attributes.artistName,
"album": attributes.albumName "album": attributes.albumName
}, (err: any, data: any) => { }, (err: any, data: any) => {
if (err) { if (err) {
console.error(`[${lastfm.name}] [album.getInfo] Error: ${typeof err === "string" ? err : err.message}`) console.error(`[${lastfm.name}] [album.getInfo] Error: ${typeof err === "string" ? err : err.message}`)
console.error(err)
return {}; return {};
} }
if (data) { if (data) {
attributes.lfmAlbum = data attributes.lfmAlbum = data
callback(attributes)
} }
this.onNowPlayingItemDidChange(attributes)
}) })
} else { } else {
return this._lfm.track.getCorrection(attributes.artistName, attributes.name, (err: any, data: any) => { this._lfm.track.getCorrection(attributes.artistName, attributes.name, (err: any, data: any) => {
if (err) { if (err) {
console.error(`[${lastfm.name}] [track.getCorrection] Error: ${typeof err === "string" ? err : err.message}`) console.error(`[${lastfm.name}] [track.getCorrection] Error: ${typeof err === "string" ? err : err.message}`)
console.error(err)
return {}; return {};
} }
if (data) { if (data) {
attributes.lfmTrack = data.correction.track attributes.lfmTrack = data.correction.track
callback(attributes)
} }
this.onNowPlayingItemDidChange(attributes)
}) })
} }
@ -170,14 +165,14 @@ export default class lastfm {
* @private * @private
*/ */
private scrobbleTrack(attributes: any): void { private scrobbleTrack(attributes: any): void {
if (!this._authenticated || !attributes || this._utils.getStoreValue("connectivity.lastfm.filter_types")[attributes.playParams.kind] || (this._utils.getStoreValue("connectivity.lastfm.filter_loop") && this._scrobbleCache.track === attributes.lfmTrack.name)) return; if (!attributes?.lfmTrack || !attributes?.lfmAlbum) {
this.verifyTrack(attributes, (a: any) => {
if (this._scrobbleDelay) { this.scrobbleTrack(a)
clearTimeout(this._scrobbleDelay); })
return
} }
// Scrobble delay if (!this._authenticated || !attributes || this._utils.getStoreValue("connectivity.lastfm.filter_types")[attributes.playParams.kind] || (this._utils.getStoreValue("connectivity.lastfm.filter_loop") && this._scrobbleCache.track === attributes.lfmTrack.name)) return;
this._scrobbleDelay = setTimeout(() => {
// Scrobble // Scrobble
const scrobble = { const scrobble = {
@ -191,9 +186,7 @@ export default class lastfm {
} }
// Easy Debugging // Easy Debugging
if (!this._utils.getApp().isPackaged) { console.debug(`[${lastfm.name}:scrobble] Scrobbling ${scrobble.artist} - ${scrobble.track}`)
console.debug(scrobble)
}
// Scrobble the track // Scrobble the track
this._lfm.track.scrobble(scrobble, (err: any, _res: any) => { this._lfm.track.scrobble(scrobble, (err: any, _res: any) => {
@ -204,10 +197,21 @@ export default class lastfm {
this._scrobbleCache = scrobble this._scrobbleCache = scrobble
} }
}); });
}, Math.round(attributes.durationInMillis * Math.min((this._utils.getStoreValue("connectivity.lastfm.scrobble_after") / 100), 0.8)))
} }
/**
* Updates the now playing track
* @param attributes
* @private
*/
private updateNowPlayingTrack(attributes: any): void { private updateNowPlayingTrack(attributes: any): void {
if (!attributes?.lfmTrack || !attributes?.lfmAlbum) {
this.verifyTrack(attributes, (a: any) => {
this.updateNowPlayingTrack(a)
})
return
}
if (!this._authenticated || !attributes || this._utils.getStoreValue("connectivity.lastfm.filter_types")[attributes.playParams.kind] || (this._utils.getStoreValue("connectivity.lastfm.filter_loop") && this._nowPlayingCache.track === attributes.lfmTrack.name)) return; if (!this._authenticated || !attributes || this._utils.getStoreValue("connectivity.lastfm.filter_types")[attributes.playParams.kind] || (this._utils.getStoreValue("connectivity.lastfm.filter_loop") && this._nowPlayingCache.track === attributes.lfmTrack.name)) return;
const nowPlaying = { const nowPlaying = {
@ -223,7 +227,6 @@ export default class lastfm {
if (err) { if (err) {
console.error(`[${lastfm.name}:updateNowPlaying] Now Playing Update failed: ${err.message}`); console.error(`[${lastfm.name}:updateNowPlaying] Now Playing Update failed: ${err.message}`);
} else { } else {
console.log(res)
console.debug(`[${lastfm.name}:updateNowPlaying] Now Playing Updated: ${nowPlaying.artist} - ${nowPlaying.track}`); console.debug(`[${lastfm.name}:updateNowPlaying] Now Playing Updated: ${nowPlaying.artist} - ${nowPlaying.track}`);
this._nowPlayingCache = nowPlaying this._nowPlayingCache = nowPlaying
} }

View file

@ -919,6 +919,12 @@ 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) => { this.mk.addEventListener(MusicKit.Events.playbackStateDidChange, (event) => {
ipcRenderer.send('wsapi-updatePlaybackState', wsapi.getAttributes()); ipcRenderer.send('wsapi-updatePlaybackState', wsapi.getAttributes());
document.body.setAttribute("playback-state", event.state == 2 ? "playing" : "paused") document.body.setAttribute("playback-state", event.state == 2 ? "playing" : "paused")