added extra comments and corrected existing ones

This commit is contained in:
Sem Visscher
2021-03-25 21:27:33 +01:00
parent f253a69656
commit bd82bd2249

View File

@ -27,7 +27,7 @@ const cleanupArtistName = (config, artist) => {
return artist return artist
} }
const createFormData = (params) => { const createFormData = params => {
// creates the body for in the post request // creates the body for in the post request
let formData = new URLSearchParams(); let formData = new URLSearchParams();
for (key in params) { for (key in params) {
@ -64,7 +64,7 @@ const createApiSig = (params, secret) => {
} }
const createToken = async ({ api_key, api_root, secret }) => { const createToken = async ({ api_key, api_root, secret }) => {
// creates an auth token // creates and stores the auth token
data = { data = {
method: 'auth.gettoken', method: 'auth.gettoken',
api_key: api_key, api_key: api_key,
@ -76,16 +76,16 @@ const createToken = async ({api_key, api_root, secret}) => {
return response?.token; return response?.token;
} }
const authenticate = async (config) => { const authenticate = async config => {
// asks user for authentication // asks the user for authentication
config.token = await createToken(config); config.token = await createToken(config);
setOptions('last-fm', config); setOptions('last-fm', config);
open(`https://www.last.fm/api/auth/?api_key=${config.api_key}&token=${config.token}`); open(`https://www.last.fm/api/auth/?api_key=${config.api_key}&token=${config.token}`);
return config; return config;
} }
const getAndSetSessionKey = async (config) => { const getAndSetSessionKey = async config => {
// get and set the session key // get and store the session key
data = { data = {
api_key: config.api_key, api_key: config.api_key,
format: 'json', format: 'json',
@ -104,7 +104,7 @@ const getAndSetSessionKey = async (config) => {
const addScrobble = async (songInfo, config) => { const addScrobble = async (songInfo, config) => {
// this adds one scrobbled song // this adds one scrobbled song to last.fm
if (!config.session_key) if (!config.session_key)
await getAndSetSessionKey(config); await getAndSetSessionKey(config);
data = { data = {
@ -121,7 +121,7 @@ const addScrobble = async (songInfo, config) => {
axios.post('https://ws.audioscrobbler.com/2.0/', createFormData(data)) axios.post('https://ws.audioscrobbler.com/2.0/', createFormData(data))
.catch(res => { .catch(res => {
if (res.response.data.error == 9) { if (res.response.data.error == 9) {
// session key is invalid // session key is invalid, so remove it from the config and reauthenticate
config.session_key = undefined; config.session_key = undefined;
setOptions('last-fm', config); setOptions('last-fm', config);
authenticate(config); authenticate(config);
@ -130,7 +130,7 @@ const addScrobble = async (songInfo, config) => {
} }
const setNowPlaying = async (songInfo, config) => { const setNowPlaying = async (songInfo, config) => {
// this adds one scrobbled song // this sets the now playing status in last.fm
if (!config.session_key) if (!config.session_key)
await getAndSetSessionKey(config); await getAndSetSessionKey(config);
data = { data = {
@ -146,7 +146,7 @@ const setNowPlaying = async (songInfo, config) => {
axios.post('https://ws.audioscrobbler.com/2.0/', createFormData(data)) axios.post('https://ws.audioscrobbler.com/2.0/', createFormData(data))
.catch(res => { .catch(res => {
if (res.response.data.error == 9) { if (res.response.data.error == 9) {
// session key is invalid // session key is invalid, so remove it from the config and reauthenticate
config.session_key = undefined; config.session_key = undefined;
setOptions('last-fm', config); setOptions('last-fm', config);
authenticate(config); authenticate(config);
@ -173,7 +173,9 @@ const lastfm = async (win, config) => {
} }
registerCallback( songInfo => { registerCallback( songInfo => {
// set remove the old scrobble timer
clearTimeout(scrobbleTimer); clearTimeout(scrobbleTimer);
// make the artist name a bit cleaner
songInfo.artist = cleanupArtistName(config, songInfo.artist); songInfo.artist = cleanupArtistName(config, songInfo.artist);
if (!songInfo.isPaused) { if (!songInfo.isPaused) {
setNowPlaying(songInfo, config); setNowPlaying(songInfo, config);