Added now playing to last-fm

This commit is contained in:
Sem Visscher
2021-03-19 22:00:00 +01:00
parent 4747050b60
commit 42e3d48caf

View File

@ -15,12 +15,12 @@ const createFormData = (params) => {
} }
const createQueryString = (params, api_sig) => { const createQueryString = (params, api_sig) => {
// creates a querystring // creates a querystring
const queryData = [] const queryData = [];
params.api_sig = api_sig params.api_sig = api_sig;
for (key in params) { for (key in params) {
queryData.push(`${key}=${params[key]}`) queryData.push(`${key}=${params[key]}`);
} }
return '?'+queryData.join('&') return '?'+queryData.join('&');
} }
const createApiSig = (params, secret) => { const createApiSig = (params, secret) => {
@ -34,11 +34,11 @@ const createApiSig = (params, secret) => {
for (key of keys) { for (key of keys) {
if (String(key) === 'format') if (String(key) === 'format')
continue continue
sig += `${key}${params[key]}` sig += `${key}${params[key]}`;
} }
sig += secret sig += secret;
sig = md5(sig) sig = md5(sig);
return sig return sig;
} }
const createToken = async ({api_key, api_root, secret}) => { const createToken = async ({api_key, api_root, secret}) => {
@ -47,7 +47,7 @@ const createToken = async ({api_key, api_root, secret}) => {
method: 'auth.gettoken', method: 'auth.gettoken',
api_key: api_key, api_key: api_key,
format: 'json' format: 'json'
} };
let api_sig = createApiSig(data, secret); let api_sig = createApiSig(data, secret);
let response = await fetch(`${api_root}${createQueryString(data, api_sig)}`); let response = await fetch(`${api_root}${createQueryString(data, api_sig)}`);
response = await response.json(); response = await response.json();
@ -59,7 +59,7 @@ const authenticate = async (config) => {
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) => {
@ -69,7 +69,7 @@ const getAndSetSessionKey = async (config) => {
format: 'json', format: 'json',
method: 'auth.getsession', method: 'auth.getsession',
token: config.token, token: config.token,
} };
api_sig = createApiSig(data, config.secret); api_sig = createApiSig(data, config.secret);
res = await fetch(`${config.api_root}${createQueryString(data, api_sig)}`); res = await fetch(`${config.api_root}${createQueryString(data, api_sig)}`);
res = await res.json(); res = await res.json();
@ -77,7 +77,7 @@ const getAndSetSessionKey = async (config) => {
await authenticate(config); await authenticate(config);
config.session_key = res?.session?.key; config.session_key = res?.session?.key;
setOptions('last-fm', config); setOptions('last-fm', config);
return config return config;
} }
@ -94,10 +94,9 @@ const addScrobble = async (songInfo, config) => {
method: 'track.scrobble', method: 'track.scrobble',
timestamp: ~~((Date.now() - songInfo.elapsedSeconds)/1000), timestamp: ~~((Date.now() - songInfo.elapsedSeconds)/1000),
duration: songInfo.songDuration, 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)) axios.post('https://ws.audioscrobbler.com/2.0/', createFormData(data))
.then(res => res.data.scrobbles)
.catch(res => { .catch(res => {
if (res.response.data.error == 9){ if (res.response.data.error == 9){
// session key is invalid // 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 // this will store the timeout that will trigger addScrobble
let scrobbleTimer = undefined; let scrobbleTimer = undefined;
@ -119,9 +144,10 @@ const lastfm = async (win, config) => {
config = await getAndSetSessionKey(config); config = await getAndSetSessionKey(config);
} }
registerCallback((songInfo)=> { registerCallback( songInfo => {
clearTimeout(scrobbleTimer); clearTimeout(scrobbleTimer);
if (!songInfo.isPaused) { if (!songInfo.isPaused) {
setNowPlaying(songInfo, config);
let scrobbleTime = Math.min(Math.ceil(songInfo.songDuration/2), 4*60); let scrobbleTime = Math.min(Math.ceil(songInfo.songDuration/2), 4*60);
if (scrobbleTime > songInfo.elapsedSeconds) { if (scrobbleTime > songInfo.elapsedSeconds) {
// scrobble still needs to happen // scrobble still needs to happen
@ -129,7 +155,7 @@ const lastfm = async (win, config) => {
scrobbleTimer = setTimeout(addScrobble, timeToWait, songInfo, config); scrobbleTimer = setTimeout(addScrobble, timeToWait, songInfo, config);
} }
} }
}) });
} }
module.exports = lastfm; module.exports = lastfm;