diff --git a/src/main/plugins/lastfm.ts b/resources/lastfm.ts similarity index 100% rename from src/main/plugins/lastfm.ts rename to resources/lastfm.ts diff --git a/src/main/base/app.ts b/src/main/base/app.ts index 58ccda47..2799eb9f 100644 --- a/src/main/base/app.ts +++ b/src/main/base/app.ts @@ -162,13 +162,13 @@ export class AppEvents { // LastFM Auth URL if (arg.includes('auth')) { - let authURI = arg.split('/auth/')[1] + const authURI = arg.split('/auth/')[1] if (authURI.startsWith('lastfm')) { // If we wanted more auth options - const authKey = authURI.split('lastfm?token=')[1]; - utils.setStoreValue('lastfm.enabled', true); - utils.setStoreValue('lastfm.auth_token', authKey); - utils.getWindow().webContents.send('LastfmAuthenticated', authKey); - this.plugin.callPlugin('lastfm', 'authenticate', authKey); + // const authKey = authURI.split('lastfm?token=')[1]; + // utils.setStoreValue('lastfm.enabled', true); + // utils.setStoreValue('lastfm.auth_token', authKey); + // utils.getWindow().webContents.send('LastfmAuthenticated', authKey); + this.plugin.callPlugin('lastfm', 'authenticateUser', authURI.split('lastfm?token=')[1]); } } // Play diff --git a/src/main/base/store.ts b/src/main/base/store.ts index 8cbcd2c4..53f3a42a 100644 --- a/src/main/base/store.ts +++ b/src/main/base/store.ts @@ -222,10 +222,14 @@ export class Store { "lastfm": { "enabled": false, "scrobble_after": 30, - "auth_token": "", "enabledRemoveFeaturingArtists": true, "filterLoop": true, - "NowPlaying": "true" + "NowPlaying": "true", + "secrets": { + "auth_token": "", + "session": {}, + } + }, "advanced": { "AudioContext": false, diff --git a/src/main/base/utils.ts b/src/main/base/utils.ts index 41e0ae12..035553d7 100644 --- a/src/main/base/utils.ts +++ b/src/main/base/utils.ts @@ -43,6 +43,13 @@ export class utils { return app; } + /** + * Get the IPCMain + */ + static getIPCMain(): Electron.IpcMain { + return ipcMain + } + /** * Fetches the i18n locale for the given language. * @param language {string} The language to fetch the locale for. diff --git a/src/main/plugins/lfm_new.ts b/src/main/plugins/lfm_new.ts index 2165d937..8bcbba88 100644 --- a/src/main/plugins/lfm_new.ts +++ b/src/main/plugins/lfm_new.ts @@ -1,7 +1,4 @@ -import * as utils from '../base/utils'; import {app} from 'electron'; -// @ts-ignore -import LastfmAPI from 'lastfmapi'; // https://github.com/maxkueng/node-lastfmapi // https://github.com/maxkueng/lastfm-autocorrect @@ -30,8 +27,9 @@ export default class lfm_new { /** * Plugin Initialization */ - private _client: any = null; - private _lastfm: any = null; + private _lfm: any = null; + private _authenticated: boolean = false; + private _utils: any = null; private _activityCache: any = { details: '', state: '', @@ -42,15 +40,67 @@ export default class lfm_new { instance: false }; - constructor() { + /** + * Initialize LastFM + * @param token + * @param api + * @private + */ + private initializeLastFM(token: string, api: {key: string, secret: string}): void { + const LastfmAPI = require("lastfmapi") + this._lfm = new LastfmAPI({ + 'api_key' : api.key, + 'secret' : api.secret, + }); + + if (this._utils.getStoreValue("lastfm.secrets.session")) { + this._lfm.setSessionCredentials(this._utils.getStoreValue("lastfm.secrets.session")); + this._authenticated = true; + } else { + this.authenticateLastFM(token) + } } /** - * Private Methods + * Authenticate the user with the given token + * @param token + * @private */ - private initializeLastFM(clientSession: string): void { - + private authenticateLastFM(token: string): void { + if (!token) return; + this._lfm.authenticate(token, (err: any, session: any) => { + if (err) { console.error(err); return; } + console.log(session); // {"name": "LASTFM_USERNAME", "key": "THE_USER_SESSION_KEY"} + this._utils.setStoreValue('lastfm.secrets.session', session); + this._authenticated = true; + }); + } + + /** + * Public Methods + */ + public authenticateUser(token: string): void { + this.initializeLastFM(token, this._apiCredentials) + } + + constructor(utils: any) { + this._utils = utils; + this.authenticateUser("") + } + + public onReady(win: Electron.BrowserWindow): void { + + this._utils.getIPCMain().handle('lfm_new:url', (event: any) => { + console.debug('lfm_new:url', event) + return this._lfm.getAuthenticationUrl({"cb": "cider://auth/lastfm"}) + }) + + this._utils.getIPCMain().on('lfm_new:auth', (event: any, token: string) => { + console.debug('lfm_new:auth', event, token) + this.authenticateUser(token) + }) } + } \ No newline at end of file