multi-scrobbler-now-playing/index.js

40 lines
1.0 KiB
JavaScript

import fetch from 'node-fetch';
function getCurrentSong() {
return fetch('https://multi-scrobbler.riksolo.com/api/status').then((data) => {
return data.json();
}).then(data => {
const players = [];
// get all players of all sources
data.sources.forEach(source => {
if (!source.players) return;
const sourcePlayers = Object.values(source.players);
sourcePlayers.forEach(player => {
players.push(player);
});
});
// sort players in order of most recent new song
const sortedPlayers = players.sort((a, b) => new Date(a.playFirstSeenAt).getTime() - new Date(b.playFirstSeenAt).getTime());
// default string
let nowPlaying = "Nothing Playing";
// if a player exists, get it's song
if (sortedPlayers.length !== 0) {
const playData = sortedPlayers[0].play.data;
nowPlaying = `${playData.artists.join(', ')} - ${playData.track}`;
}
return nowPlaying;
});
}
setInterval(async () => {
process.stdout.cursorTo(0);
process.stdout.clearLine(0);
process.stdout.write(await getCurrentSong());
}, 2000);