getLastPlayedSongs now more universal and instead of /lastplayed there is /lastsong endpoint that returns one previously played song.

This commit is contained in:
Alexander Andreev 2021-09-01 22:10:15 +04:00
parent d7d84a7f52
commit 645e94a5d8
Signed by: Arav
GPG Key ID: 610DF2574456329F

View File

@ -32,13 +32,18 @@ async function getRadioStatus() {
} }
} }
async function getLastSongs(count=11) { async function getLastPlayedSongs(count=11) {
const { stdout, stderr } = await exec(`tail -n ${count} /var/log/icecast/playlist.log | awk -F '|' '{ print $4 }'`); try {
let songs = stdout.trim().split("\n"); const { stdout, _ } = await exec(`tail -n ${count} /var/log/icecast/playlist.log | awk -F '|' 'NR<${count} { print $4 }'`);
songs.pop(); let songs = stdout.trim().split("\n");
for (let i = 0; i < songs.length; ++i) for (let i = 0; i < songs.length; ++i) {
songs[i] = songs[i].split(' - '); let song = songs[i].split(' - ');
return songs; songs[i] = { "artist": song[0], "title": song[1] };
}
return songs;
} catch {
return [];
}
} }
function setRoutes() { function setRoutes() {
@ -48,7 +53,7 @@ function setRoutes() {
description: "Internet-radio broadcasting from under my desk.", description: "Internet-radio broadcasting from under my desk.",
main_site: util.getServiceByHost(ctx.header.host), main_site: util.getServiceByHost(ctx.header.host),
radio_status: await getRadioStatus(), radio_status: await getRadioStatus(),
last_songs: await getLastSongs() last_songs: await getLastPlayedSongs()
}); });
}) })
.get('/filelist', async ctx => { .get('/filelist', async ctx => {
@ -64,8 +69,8 @@ function setRoutes() {
.get('/stats', async ctx => { .get('/stats', async ctx => {
ctx.body = await getRadioStatus(); ctx.body = await getRadioStatus();
}) })
.get('/lastplayed', async ctx => { .get('/lastsong', async ctx => {
ctx.body = await getLastSongs(); ctx.body = await getLastPlayedSongs(2);
}); });
} }