mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 11:21:46 +00:00
feat: run prettier
This commit is contained in:
@ -6,21 +6,21 @@ import type { LastFmPluginConfig } from './index';
|
||||
import type { SongInfo } from '@/providers/song-info';
|
||||
|
||||
interface LastFmData {
|
||||
method: string,
|
||||
timestamp?: number,
|
||||
method: string;
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
interface LastFmSongData {
|
||||
track?: string,
|
||||
duration?: number,
|
||||
artist?: string,
|
||||
album?: string,
|
||||
api_key: string,
|
||||
sk?: string,
|
||||
format: string,
|
||||
method: string,
|
||||
timestamp?: number,
|
||||
api_sig?: string,
|
||||
track?: string;
|
||||
duration?: number;
|
||||
artist?: string;
|
||||
album?: string;
|
||||
api_key: string;
|
||||
sk?: string;
|
||||
format: string;
|
||||
method: string;
|
||||
timestamp?: number;
|
||||
api_sig?: string;
|
||||
}
|
||||
|
||||
const createFormData = (parameters: LastFmSongData) => {
|
||||
@ -33,12 +33,19 @@ const createFormData = (parameters: LastFmSongData) => {
|
||||
return formData;
|
||||
};
|
||||
|
||||
const createQueryString = (parameters: Record<string, unknown>, apiSignature: string) => {
|
||||
const createQueryString = (
|
||||
parameters: Record<string, unknown>,
|
||||
apiSignature: string,
|
||||
) => {
|
||||
// Creates a querystring
|
||||
const queryData = [];
|
||||
parameters.api_sig = apiSignature;
|
||||
for (const key in parameters) {
|
||||
queryData.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(parameters[key]))}`);
|
||||
queryData.push(
|
||||
`${encodeURIComponent(key)}=${encodeURIComponent(
|
||||
String(parameters[key]),
|
||||
)}`,
|
||||
);
|
||||
}
|
||||
|
||||
return '?' + queryData.join('&');
|
||||
@ -63,7 +70,11 @@ const createApiSig = (parameters: LastFmSongData, secret: string) => {
|
||||
return sig;
|
||||
};
|
||||
|
||||
const createToken = async ({ api_key: apiKey, api_root: apiRoot, secret }: LastFmPluginConfig) => {
|
||||
const createToken = async ({
|
||||
api_key: apiKey,
|
||||
api_root: apiRoot,
|
||||
secret,
|
||||
}: LastFmPluginConfig) => {
|
||||
// Creates and stores the auth token
|
||||
const data = {
|
||||
method: 'auth.gettoken',
|
||||
@ -71,19 +82,28 @@ const createToken = async ({ api_key: apiKey, api_root: apiRoot, secret }: LastF
|
||||
format: 'json',
|
||||
};
|
||||
const apiSigature = createApiSig(data, secret);
|
||||
const response = await net.fetch(`${apiRoot}${createQueryString(data, apiSigature)}`);
|
||||
const json = await response.json() as Record<string, string>;
|
||||
const response = await net.fetch(
|
||||
`${apiRoot}${createQueryString(data, apiSigature)}`,
|
||||
);
|
||||
const json = (await response.json()) as Record<string, string>;
|
||||
return json?.token;
|
||||
};
|
||||
|
||||
const authenticate = async (config: LastFmPluginConfig) => {
|
||||
// Asks the user for authentication
|
||||
await shell.openExternal(`https://www.last.fm/api/auth/?api_key=${config.api_key}&token=${config.token}`);
|
||||
await shell.openExternal(
|
||||
`https://www.last.fm/api/auth/?api_key=${config.api_key}&token=${config.token}`,
|
||||
);
|
||||
};
|
||||
|
||||
type SetConfType = (conf: Partial<Omit<LastFmPluginConfig, 'enabled'>>) => (void | Promise<void>);
|
||||
type SetConfType = (
|
||||
conf: Partial<Omit<LastFmPluginConfig, 'enabled'>>,
|
||||
) => void | Promise<void>;
|
||||
|
||||
export const getAndSetSessionKey = async (config: LastFmPluginConfig, setConfig: SetConfType) => {
|
||||
export const getAndSetSessionKey = async (
|
||||
config: LastFmPluginConfig,
|
||||
setConfig: SetConfType,
|
||||
) => {
|
||||
// Get and store the session key
|
||||
const data = {
|
||||
api_key: config.api_key,
|
||||
@ -92,12 +112,14 @@ export const getAndSetSessionKey = async (config: LastFmPluginConfig, setConfig:
|
||||
token: config.token,
|
||||
};
|
||||
const apiSignature = createApiSig(data, config.secret);
|
||||
const response = await net.fetch(`${config.api_root}${createQueryString(data, apiSignature)}`);
|
||||
const json = await response.json() as {
|
||||
error?: string,
|
||||
const response = await net.fetch(
|
||||
`${config.api_root}${createQueryString(data, apiSignature)}`,
|
||||
);
|
||||
const json = (await response.json()) as {
|
||||
error?: string;
|
||||
session?: {
|
||||
key: string,
|
||||
}
|
||||
key: string;
|
||||
};
|
||||
};
|
||||
if (json.error) {
|
||||
config.token = await createToken(config);
|
||||
@ -111,7 +133,12 @@ export const getAndSetSessionKey = async (config: LastFmPluginConfig, setConfig:
|
||||
return config;
|
||||
};
|
||||
|
||||
const postSongDataToAPI = async (songInfo: SongInfo, config: LastFmPluginConfig, data: LastFmData, setConfig: SetConfType) => {
|
||||
const postSongDataToAPI = async (
|
||||
songInfo: SongInfo,
|
||||
config: LastFmPluginConfig,
|
||||
data: LastFmData,
|
||||
setConfig: SetConfType,
|
||||
) => {
|
||||
// This sends a post request to the api, and adds the common data
|
||||
if (!config.session_key) {
|
||||
await getAndSetSessionKey(config, setConfig);
|
||||
@ -130,25 +157,35 @@ const postSongDataToAPI = async (songInfo: SongInfo, config: LastFmPluginConfig,
|
||||
|
||||
postData.api_sig = createApiSig(postData, config.secret);
|
||||
const formData = createFormData(postData);
|
||||
net.fetch('https://ws.audioscrobbler.com/2.0/', { method: 'POST', body: formData })
|
||||
.catch(async (error: {
|
||||
response?: {
|
||||
data?: {
|
||||
error: number,
|
||||
net
|
||||
.fetch('https://ws.audioscrobbler.com/2.0/', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
})
|
||||
.catch(
|
||||
async (error: {
|
||||
response?: {
|
||||
data?: {
|
||||
error: number;
|
||||
};
|
||||
};
|
||||
}) => {
|
||||
if (error?.response?.data?.error === 9) {
|
||||
// Session key is invalid, so remove it from the config and reauthenticate
|
||||
config.session_key = undefined;
|
||||
config.token = await createToken(config);
|
||||
await authenticate(config);
|
||||
setConfig(config);
|
||||
}
|
||||
}
|
||||
}) => {
|
||||
if (error?.response?.data?.error === 9) {
|
||||
// Session key is invalid, so remove it from the config and reauthenticate
|
||||
config.session_key = undefined;
|
||||
config.token = await createToken(config);
|
||||
await authenticate(config);
|
||||
setConfig(config);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
export const addScrobble = (songInfo: SongInfo, config: LastFmPluginConfig, setConfig: SetConfType) => {
|
||||
export const addScrobble = (
|
||||
songInfo: SongInfo,
|
||||
config: LastFmPluginConfig,
|
||||
setConfig: SetConfType,
|
||||
) => {
|
||||
// This adds one scrobbled song to last.fm
|
||||
const data = {
|
||||
method: 'track.scrobble',
|
||||
@ -157,7 +194,11 @@ export const addScrobble = (songInfo: SongInfo, config: LastFmPluginConfig, setC
|
||||
postSongDataToAPI(songInfo, config, data, setConfig);
|
||||
};
|
||||
|
||||
export const setNowPlaying = (songInfo: SongInfo, config: LastFmPluginConfig, setConfig: SetConfType) => {
|
||||
export const setNowPlaying = (
|
||||
songInfo: SongInfo,
|
||||
config: LastFmPluginConfig,
|
||||
setConfig: SetConfType,
|
||||
) => {
|
||||
// This sets the now playing status in last.fm
|
||||
const data = {
|
||||
method: 'track.updateNowPlaying',
|
||||
|
||||
Reference in New Issue
Block a user