diff --git a/plugins/last-fm/back.js b/plugins/last-fm/back.js index 9a88d4fb..49eaaa51 100644 --- a/plugins/last-fm/back.js +++ b/plugins/last-fm/back.js @@ -15,12 +15,12 @@ const createFormData = (params) => { } const createQueryString = (params, api_sig) => { // creates a querystring - const queryData = [] - params.api_sig = api_sig + const queryData = []; + params.api_sig = api_sig; for (key in params) { - queryData.push(`${key}=${params[key]}`) + queryData.push(`${key}=${params[key]}`); } - return '?'+queryData.join('&') + return '?'+queryData.join('&'); } const createApiSig = (params, secret) => { @@ -34,11 +34,11 @@ const createApiSig = (params, secret) => { for (key of keys) { if (String(key) === 'format') continue - sig += `${key}${params[key]}` + sig += `${key}${params[key]}`; } - sig += secret - sig = md5(sig) - return sig + sig += secret; + sig = md5(sig); + return sig; } const createToken = async ({api_key, api_root, secret}) => { @@ -47,7 +47,7 @@ const createToken = async ({api_key, api_root, secret}) => { method: 'auth.gettoken', api_key: api_key, format: 'json' - } + }; let api_sig = createApiSig(data, secret); let response = await fetch(`${api_root}${createQueryString(data, api_sig)}`); response = await response.json(); @@ -59,7 +59,7 @@ const authenticate = async (config) => { config.token = await createToken(config); setOptions('last-fm', config); open(`https://www.last.fm/api/auth/?api_key=${config.api_key}&token=${config.token}`); - return config + return config; } const getAndSetSessionKey = async (config) => { @@ -69,7 +69,7 @@ const getAndSetSessionKey = async (config) => { format: 'json', method: 'auth.getsession', token: config.token, - } + }; api_sig = createApiSig(data, config.secret); res = await fetch(`${config.api_root}${createQueryString(data, api_sig)}`); res = await res.json(); @@ -77,7 +77,7 @@ const getAndSetSessionKey = async (config) => { await authenticate(config); config.session_key = res?.session?.key; setOptions('last-fm', config); - return config + return config; } @@ -94,10 +94,9 @@ const addScrobble = async (songInfo, config) => { method: 'track.scrobble', timestamp: ~~((Date.now() - songInfo.elapsedSeconds)/1000), duration: songInfo.songDuration, - } - data.api_sig = createApiSig(data, config.secret) + }; + data.api_sig = createApiSig(data, config.secret); axios.post('https://ws.audioscrobbler.com/2.0/', createFormData(data)) - .then(res => res.data.scrobbles) .catch(res => { if (res.response.data.error == 9){ // session key is invalid @@ -108,6 +107,32 @@ const addScrobble = async (songInfo, config) => { }); } +const setNowPlaying = async (songInfo, config) => { + // this adds one scrobbled song + if (!config.session_key) + await getAndSetSessionKey(config); + data = { + track: songInfo.title, + artist: songInfo.artist, + api_key: config.api_key, + sk: config.session_key, + format: 'json', + method: 'track.updateNowPlaying', + duration: songInfo.songDuration, + }; + data.api_sig = createApiSig(data, config.secret); + axios.post('https://ws.audioscrobbler.com/2.0/', createFormData(data)) + .catch(res => { + if (res.response.data.error == 9){ + // session key is invalid + config.session_key = undefined; + setOptions('last-fm', config); + authenticate(config); + } + }); +} + + // this will store the timeout that will trigger addScrobble let scrobbleTimer = undefined; @@ -119,9 +144,10 @@ const lastfm = async (win, config) => { config = await getAndSetSessionKey(config); } - registerCallback((songInfo)=> { + registerCallback( songInfo => { clearTimeout(scrobbleTimer); if (!songInfo.isPaused) { + setNowPlaying(songInfo, config); let scrobbleTime = Math.min(Math.ceil(songInfo.songDuration/2), 4*60); if (scrobbleTime > songInfo.elapsedSeconds) { // scrobble still needs to happen @@ -129,7 +155,7 @@ const lastfm = async (win, config) => { scrobbleTimer = setTimeout(addScrobble, timeToWait, songInfo, config); } } - }) + }); } module.exports = lastfm; \ No newline at end of file