From 00d0b3198043163c3904c4b8c5ca91d89102ddda Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sun, 18 Feb 2024 20:21:19 +0900 Subject: [PATCH] fix(scrobbler): remove `snake_case` --- src/config/store.ts | 36 ++++++++++++++----- src/plugins/scrobbler/index.ts | 20 +++++------ src/plugins/scrobbler/services/lastfm.ts | 26 +++++++------- .../scrobbler/services/listenbrainz.ts | 6 ++-- 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/config/store.ts b/src/config/store.ts index 7d974166..916a7ccc 100644 --- a/src/config/store.ts +++ b/src/config/store.ts @@ -16,24 +16,42 @@ const migrations = { secret?: string; }; if (lastfmConfig) { - const scrobblerConfig = store.get( + let scrobblerConfig = store.get( 'plugins.scrobbler', ) as { enabled?: boolean; - scrobblers: { - lastfm: { + scrobblers?: { + lastfm?: { enabled?: boolean; token?: string; - session_key?: string; - api_root?: string; - api_key?: string; + sessionKey?: string; + apiRoot?: string; + apiKey?: string; secret?: string; }; }; - }; + } | undefined; - scrobblerConfig.enabled = lastfmConfig.enabled; - scrobblerConfig.scrobblers.lastfm = lastfmConfig; + if (!scrobblerConfig) { + 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); } }, diff --git a/src/plugins/scrobbler/index.ts b/src/plugins/scrobbler/index.ts index 26b24657..e1343e25 100644 --- a/src/plugins/scrobbler/index.ts +++ b/src/plugins/scrobbler/index.ts @@ -11,7 +11,7 @@ export interface ScrobblerPluginConfig { * * @default true */ - scrobble_other_media: boolean, + scrobbleOtherMedia: boolean, scrobblers: { lastfm: { /** @@ -27,19 +27,19 @@ export interface ScrobblerPluginConfig { /** * Session key used for scrobbling */ - session_key: string | undefined, + sessionKey: string | undefined, /** * Root of the Last.fm API * * @default 'http://ws.audioscrobbler.com/2.0/' */ - api_root: string, + apiRoot: string, /** * Last.fm api key registered by @semvis123 * * @default '04d76faaac8726e60988e14c105d421a' */ - api_key: string, + apiKey: string, /** * Last.fm api secret registered by @semvis123 * @@ -63,27 +63,27 @@ export interface ScrobblerPluginConfig { * * @default 'https://api.listenbrainz.org/1/' */ - api_root: string, + apiRoot: string, }, } } export const defaultConfig: ScrobblerPluginConfig = { enabled: false, - scrobble_other_media: true, + scrobbleOtherMedia: true, scrobblers: { lastfm: { enabled: false, token: undefined, - session_key: undefined, - api_root: 'https://ws.audioscrobbler.com/2.0/', - api_key: '04d76faaac8726e60988e14c105d421a', + sessionKey: undefined, + apiRoot: 'https://ws.audioscrobbler.com/2.0/', + apiKey: '04d76faaac8726e60988e14c105d421a', secret: 'a5d2a36fdf64819290f6982481eaffa2', }, listenbrainz: { enabled: false, token: undefined, - api_root: 'https://api.listenbrainz.org/1/', + apiRoot: 'https://api.listenbrainz.org/1/', }, }, }; diff --git a/src/plugins/scrobbler/services/lastfm.ts b/src/plugins/scrobbler/services/lastfm.ts index a8e9e011..bd23a58c 100644 --- a/src/plugins/scrobbler/services/lastfm.ts +++ b/src/plugins/scrobbler/services/lastfm.ts @@ -29,20 +29,20 @@ interface LastFmSongData { export class LastFmScrobbler extends ScrobblerBase { isSessionCreated(config: ScrobblerPluginConfig): boolean { - return !!config.scrobblers.lastfm.session_key; + return !!config.scrobblers.lastfm.sessionKey; } async createSession(config: ScrobblerPluginConfig, setConfig: SetConfType): Promise { // Get and store the session key const data = { - api_key: config.scrobblers.lastfm.api_key, + api_key: config.scrobblers.lastfm.apiKey, format: 'json', method: 'auth.getsession', token: config.scrobblers.lastfm.token, }; const apiSignature = createApiSig(data, config.scrobblers.lastfm.secret); 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 { error?: string; @@ -56,14 +56,14 @@ export class LastFmScrobbler extends ScrobblerBase { setConfig(config); } if (json.session) { - config.scrobblers.lastfm.session_key = json.session.key; + config.scrobblers.lastfm.sessionKey = json.session.key; } setConfig(config); return config; } setNowPlaying(songInfo: SongInfo, config: ScrobblerPluginConfig, setConfig: SetConfType): void { - if (!config.scrobblers.lastfm.session_key) { + if (!config.scrobblers.lastfm.sessionKey) { return; } @@ -75,7 +75,7 @@ export class LastFmScrobbler extends ScrobblerBase { } addScrobble(songInfo: SongInfo, config: ScrobblerPluginConfig, setConfig: SetConfType): void { - if (!config.scrobblers.lastfm.session_key) { + if (!config.scrobblers.lastfm.sessionKey) { return; } @@ -94,7 +94,7 @@ export class LastFmScrobbler extends ScrobblerBase { setConfig: SetConfType, ): Promise { // 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); } @@ -103,8 +103,8 @@ export class LastFmScrobbler extends ScrobblerBase { duration: songInfo.songDuration, artist: songInfo.artist, ...(songInfo.album ? { album: songInfo.album } : undefined), // Will be undefined if current song is a video - api_key: config.scrobblers.lastfm.api_key, - sk: config.scrobblers.lastfm.session_key, + api_key: config.scrobblers.lastfm.apiKey, + sk: config.scrobblers.lastfm.sessionKey, format: 'json', ...data, }; @@ -126,7 +126,7 @@ export class LastFmScrobbler extends ScrobblerBase { }) => { if (error?.response?.data?.error === 9) { // 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); await authenticate(config); setConfig(config); @@ -188,8 +188,8 @@ const createApiSig = (parameters: LastFmSongData, secret: string) => { const createToken = async ({ scrobblers: { lastfm: { - api_key: apiKey, - api_root: apiRoot, + apiKey, + apiRoot, secret, } } @@ -211,6 +211,6 @@ const createToken = async ({ const authenticate = async (config: ScrobblerPluginConfig) => { // Asks the user for authentication 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}`, ); }; diff --git a/src/plugins/scrobbler/services/listenbrainz.ts b/src/plugins/scrobbler/services/listenbrainz.ts index f70da60e..7b19e78c 100644 --- a/src/plugins/scrobbler/services/listenbrainz.ts +++ b/src/plugins/scrobbler/services/listenbrainz.ts @@ -36,7 +36,7 @@ export class ListenbrainzScrobbler extends ScrobblerBase { } 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; } @@ -45,7 +45,7 @@ export class ListenbrainzScrobbler extends ScrobblerBase { } 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; } @@ -80,7 +80,7 @@ function createRequestBody(listenType: string, songInfo: SongInfo): Listenbrainz } 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', body: JSON.stringify(body),