This commit is contained in:
Core 2022-06-14 12:40:49 +01:00
parent f9becc61ae
commit c8c437449e
No known key found for this signature in database
GPG key ID: FE9BF1B547F8F3C6
5 changed files with 78 additions and 17 deletions

View file

@ -162,13 +162,13 @@ export class AppEvents {
// LastFM Auth URL // LastFM Auth URL
if (arg.includes('auth')) { 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 if (authURI.startsWith('lastfm')) { // If we wanted more auth options
const authKey = authURI.split('lastfm?token=')[1]; // const authKey = authURI.split('lastfm?token=')[1];
utils.setStoreValue('lastfm.enabled', true); // utils.setStoreValue('lastfm.enabled', true);
utils.setStoreValue('lastfm.auth_token', authKey); // utils.setStoreValue('lastfm.auth_token', authKey);
utils.getWindow().webContents.send('LastfmAuthenticated', authKey); // utils.getWindow().webContents.send('LastfmAuthenticated', authKey);
this.plugin.callPlugin('lastfm', 'authenticate', authKey); this.plugin.callPlugin('lastfm', 'authenticateUser', authURI.split('lastfm?token=')[1]);
} }
} }
// Play // Play

View file

@ -222,10 +222,14 @@ export class Store {
"lastfm": { "lastfm": {
"enabled": false, "enabled": false,
"scrobble_after": 30, "scrobble_after": 30,
"auth_token": "",
"enabledRemoveFeaturingArtists": true, "enabledRemoveFeaturingArtists": true,
"filterLoop": true, "filterLoop": true,
"NowPlaying": "true" "NowPlaying": "true",
"secrets": {
"auth_token": "",
"session": {},
}
}, },
"advanced": { "advanced": {
"AudioContext": false, "AudioContext": false,

View file

@ -43,6 +43,13 @@ export class utils {
return app; return app;
} }
/**
* Get the IPCMain
*/
static getIPCMain(): Electron.IpcMain {
return ipcMain
}
/** /**
* Fetches the i18n locale for the given language. * Fetches the i18n locale for the given language.
* @param language {string} The language to fetch the locale for. * @param language {string} The language to fetch the locale for.

View file

@ -1,7 +1,4 @@
import * as utils from '../base/utils';
import {app} from 'electron'; import {app} from 'electron';
// @ts-ignore
import LastfmAPI from 'lastfmapi';
// https://github.com/maxkueng/node-lastfmapi // https://github.com/maxkueng/node-lastfmapi
// https://github.com/maxkueng/lastfm-autocorrect // https://github.com/maxkueng/lastfm-autocorrect
@ -30,8 +27,9 @@ export default class lfm_new {
/** /**
* Plugin Initialization * Plugin Initialization
*/ */
private _client: any = null; private _lfm: any = null;
private _lastfm: any = null; private _authenticated: boolean = false;
private _utils: any = null;
private _activityCache: any = { private _activityCache: any = {
details: '', details: '',
state: '', state: '',
@ -42,15 +40,67 @@ export default class lfm_new {
instance: false 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)
})
} }
} }