fix(scrobbler): remove snake_case

This commit is contained in:
JellyBrick
2024-02-18 20:21:19 +09:00
parent 5edb2131d2
commit 00d0b31980
4 changed files with 53 additions and 35 deletions

View File

@ -16,24 +16,42 @@ const migrations = {
secret?: string; secret?: string;
}; };
if (lastfmConfig) { if (lastfmConfig) {
const scrobblerConfig = store.get( let scrobblerConfig = store.get(
'plugins.scrobbler', 'plugins.scrobbler',
) as { ) as {
enabled?: boolean; enabled?: boolean;
scrobblers: { scrobblers?: {
lastfm: { lastfm?: {
enabled?: boolean; enabled?: boolean;
token?: string; token?: string;
session_key?: string; sessionKey?: string;
api_root?: string; apiRoot?: string;
api_key?: string; apiKey?: string;
secret?: string; secret?: string;
}; };
}; };
}; } | undefined;
scrobblerConfig.enabled = lastfmConfig.enabled; if (!scrobblerConfig) {
scrobblerConfig.scrobblers.lastfm = lastfmConfig; scrobblerConfig = {
enabled: lastfmConfig.enabled,
};
}
if (!scrobblerConfig.scrobblers) {
scrobblerConfig.scrobblers = {
lastfm: {},
};
}
scrobblerConfig.scrobblers.lastfm = {
enabled: lastfmConfig.enabled,
token: lastfmConfig.token,
sessionKey: lastfmConfig.session_key,
apiRoot: lastfmConfig.api_root,
apiKey: lastfmConfig.api_key,
secret: lastfmConfig.secret,
};
store.set('plugins.scrobbler', scrobblerConfig); store.set('plugins.scrobbler', scrobblerConfig);
} }
}, },

View File

@ -11,7 +11,7 @@ export interface ScrobblerPluginConfig {
* *
* @default true * @default true
*/ */
scrobble_other_media: boolean, scrobbleOtherMedia: boolean,
scrobblers: { scrobblers: {
lastfm: { lastfm: {
/** /**
@ -27,19 +27,19 @@ export interface ScrobblerPluginConfig {
/** /**
* Session key used for scrobbling * Session key used for scrobbling
*/ */
session_key: string | undefined, sessionKey: string | undefined,
/** /**
* Root of the Last.fm API * Root of the Last.fm API
* *
* @default 'http://ws.audioscrobbler.com/2.0/' * @default 'http://ws.audioscrobbler.com/2.0/'
*/ */
api_root: string, apiRoot: string,
/** /**
* Last.fm api key registered by @semvis123 * Last.fm api key registered by @semvis123
* *
* @default '04d76faaac8726e60988e14c105d421a' * @default '04d76faaac8726e60988e14c105d421a'
*/ */
api_key: string, apiKey: string,
/** /**
* Last.fm api secret registered by @semvis123 * Last.fm api secret registered by @semvis123
* *
@ -63,27 +63,27 @@ export interface ScrobblerPluginConfig {
* *
* @default 'https://api.listenbrainz.org/1/' * @default 'https://api.listenbrainz.org/1/'
*/ */
api_root: string, apiRoot: string,
}, },
} }
} }
export const defaultConfig: ScrobblerPluginConfig = { export const defaultConfig: ScrobblerPluginConfig = {
enabled: false, enabled: false,
scrobble_other_media: true, scrobbleOtherMedia: true,
scrobblers: { scrobblers: {
lastfm: { lastfm: {
enabled: false, enabled: false,
token: undefined, token: undefined,
session_key: undefined, sessionKey: undefined,
api_root: 'https://ws.audioscrobbler.com/2.0/', apiRoot: 'https://ws.audioscrobbler.com/2.0/',
api_key: '04d76faaac8726e60988e14c105d421a', apiKey: '04d76faaac8726e60988e14c105d421a',
secret: 'a5d2a36fdf64819290f6982481eaffa2', secret: 'a5d2a36fdf64819290f6982481eaffa2',
}, },
listenbrainz: { listenbrainz: {
enabled: false, enabled: false,
token: undefined, token: undefined,
api_root: 'https://api.listenbrainz.org/1/', apiRoot: 'https://api.listenbrainz.org/1/',
}, },
}, },
}; };

View File

@ -29,20 +29,20 @@ interface LastFmSongData {
export class LastFmScrobbler extends ScrobblerBase { export class LastFmScrobbler extends ScrobblerBase {
isSessionCreated(config: ScrobblerPluginConfig): boolean { isSessionCreated(config: ScrobblerPluginConfig): boolean {
return !!config.scrobblers.lastfm.session_key; return !!config.scrobblers.lastfm.sessionKey;
} }
async createSession(config: ScrobblerPluginConfig, setConfig: SetConfType): Promise<ScrobblerPluginConfig> { async createSession(config: ScrobblerPluginConfig, setConfig: SetConfType): Promise<ScrobblerPluginConfig> {
// Get and store the session key // Get and store the session key
const data = { const data = {
api_key: config.scrobblers.lastfm.api_key, api_key: config.scrobblers.lastfm.apiKey,
format: 'json', format: 'json',
method: 'auth.getsession', method: 'auth.getsession',
token: config.scrobblers.lastfm.token, token: config.scrobblers.lastfm.token,
}; };
const apiSignature = createApiSig(data, config.scrobblers.lastfm.secret); const apiSignature = createApiSig(data, config.scrobblers.lastfm.secret);
const response = await net.fetch( const response = await net.fetch(
`${config.scrobblers.lastfm.api_root}${createQueryString(data, apiSignature)}`, `${config.scrobblers.lastfm.apiRoot}${createQueryString(data, apiSignature)}`,
); );
const json = (await response.json()) as { const json = (await response.json()) as {
error?: string; error?: string;
@ -56,14 +56,14 @@ export class LastFmScrobbler extends ScrobblerBase {
setConfig(config); setConfig(config);
} }
if (json.session) { if (json.session) {
config.scrobblers.lastfm.session_key = json.session.key; config.scrobblers.lastfm.sessionKey = json.session.key;
} }
setConfig(config); setConfig(config);
return config; return config;
} }
setNowPlaying(songInfo: SongInfo, config: ScrobblerPluginConfig, setConfig: SetConfType): void { setNowPlaying(songInfo: SongInfo, config: ScrobblerPluginConfig, setConfig: SetConfType): void {
if (!config.scrobblers.lastfm.session_key) { if (!config.scrobblers.lastfm.sessionKey) {
return; return;
} }
@ -75,7 +75,7 @@ export class LastFmScrobbler extends ScrobblerBase {
} }
addScrobble(songInfo: SongInfo, config: ScrobblerPluginConfig, setConfig: SetConfType): void { addScrobble(songInfo: SongInfo, config: ScrobblerPluginConfig, setConfig: SetConfType): void {
if (!config.scrobblers.lastfm.session_key) { if (!config.scrobblers.lastfm.sessionKey) {
return; return;
} }
@ -94,7 +94,7 @@ export class LastFmScrobbler extends ScrobblerBase {
setConfig: SetConfType, setConfig: SetConfType,
): Promise<void> { ): Promise<void> {
// This sends a post request to the api, and adds the common data // This sends a post request to the api, and adds the common data
if (!config.scrobblers.lastfm.session_key) { if (!config.scrobblers.lastfm.sessionKey) {
await this.createSession(config, setConfig); await this.createSession(config, setConfig);
} }
@ -103,8 +103,8 @@ export class LastFmScrobbler extends ScrobblerBase {
duration: songInfo.songDuration, duration: songInfo.songDuration,
artist: songInfo.artist, artist: songInfo.artist,
...(songInfo.album ? { album: songInfo.album } : undefined), // Will be undefined if current song is a video ...(songInfo.album ? { album: songInfo.album } : undefined), // Will be undefined if current song is a video
api_key: config.scrobblers.lastfm.api_key, api_key: config.scrobblers.lastfm.apiKey,
sk: config.scrobblers.lastfm.session_key, sk: config.scrobblers.lastfm.sessionKey,
format: 'json', format: 'json',
...data, ...data,
}; };
@ -126,7 +126,7 @@ export class LastFmScrobbler extends ScrobblerBase {
}) => { }) => {
if (error?.response?.data?.error === 9) { if (error?.response?.data?.error === 9) {
// Session key is invalid, so remove it from the config and reauthenticate // Session key is invalid, so remove it from the config and reauthenticate
config.scrobblers.lastfm.session_key = undefined; config.scrobblers.lastfm.sessionKey = undefined;
config.scrobblers.lastfm.token = await createToken(config); config.scrobblers.lastfm.token = await createToken(config);
await authenticate(config); await authenticate(config);
setConfig(config); setConfig(config);
@ -188,8 +188,8 @@ const createApiSig = (parameters: LastFmSongData, secret: string) => {
const createToken = async ({ const createToken = async ({
scrobblers: { scrobblers: {
lastfm: { lastfm: {
api_key: apiKey, apiKey,
api_root: apiRoot, apiRoot,
secret, secret,
} }
} }
@ -211,6 +211,6 @@ const createToken = async ({
const authenticate = async (config: ScrobblerPluginConfig) => { const authenticate = async (config: ScrobblerPluginConfig) => {
// Asks the user for authentication // Asks the user for authentication
await shell.openExternal( await shell.openExternal(
`https://www.last.fm/api/auth/?api_key=${config.scrobblers.lastfm.api_key}&token=${config.scrobblers.lastfm.token}`, `https://www.last.fm/api/auth/?api_key=${config.scrobblers.lastfm.apiKey}&token=${config.scrobblers.lastfm.token}`,
); );
}; };

View File

@ -36,7 +36,7 @@ export class ListenbrainzScrobbler extends ScrobblerBase {
} }
setNowPlaying(songInfo: SongInfo, config: ScrobblerPluginConfig, _setConfig: SetConfType): void { setNowPlaying(songInfo: SongInfo, config: ScrobblerPluginConfig, _setConfig: SetConfType): void {
if (!config.scrobblers.listenbrainz.api_root || !config.scrobblers.listenbrainz.token) { if (!config.scrobblers.listenbrainz.apiRoot || !config.scrobblers.listenbrainz.token) {
return; return;
} }
@ -45,7 +45,7 @@ export class ListenbrainzScrobbler extends ScrobblerBase {
} }
addScrobble(songInfo: SongInfo, config: ScrobblerPluginConfig, _setConfig: SetConfType): void { addScrobble(songInfo: SongInfo, config: ScrobblerPluginConfig, _setConfig: SetConfType): void {
if (!config.scrobblers.listenbrainz.api_root || !config.scrobblers.listenbrainz.token) { if (!config.scrobblers.listenbrainz.apiRoot || !config.scrobblers.listenbrainz.token) {
return; return;
} }
@ -80,7 +80,7 @@ function createRequestBody(listenType: string, songInfo: SongInfo): Listenbrainz
} }
function submitListen(body: ListenbrainzRequestBody, config: ScrobblerPluginConfig) { function submitListen(body: ListenbrainzRequestBody, config: ScrobblerPluginConfig) {
net.fetch(config.scrobblers.listenbrainz.api_root + 'submit-listens', net.fetch(config.scrobblers.listenbrainz.apiRoot + 'submit-listens',
{ {
method: 'POST', method: 'POST',
body: JSON.stringify(body), body: JSON.stringify(body),