From 645e94a5d88b710be14283de1be6d02d8493c090 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Wed, 1 Sep 2021 22:10:15 +0400 Subject: [PATCH] getLastPlayedSongs now more universal and instead of /lastplayed there is /lastsong endpoint that returns one previously played song. --- radio/index.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/radio/index.js b/radio/index.js index 6b6bfb4..220ad4a 100644 --- a/radio/index.js +++ b/radio/index.js @@ -32,13 +32,18 @@ async function getRadioStatus() { } } -async function getLastSongs(count=11) { - const { stdout, stderr } = await exec(`tail -n ${count} /var/log/icecast/playlist.log | awk -F '|' '{ print $4 }'`); - let songs = stdout.trim().split("\n"); - songs.pop(); - for (let i = 0; i < songs.length; ++i) - songs[i] = songs[i].split(' - '); - return songs; +async function getLastPlayedSongs(count=11) { + try { + const { stdout, _ } = await exec(`tail -n ${count} /var/log/icecast/playlist.log | awk -F '|' 'NR<${count} { print $4 }'`); + let songs = stdout.trim().split("\n"); + for (let i = 0; i < songs.length; ++i) { + let song = songs[i].split(' - '); + songs[i] = { "artist": song[0], "title": song[1] }; + } + return songs; + } catch { + return []; + } } function setRoutes() { @@ -48,7 +53,7 @@ function setRoutes() { description: "Internet-radio broadcasting from under my desk.", main_site: util.getServiceByHost(ctx.header.host), radio_status: await getRadioStatus(), - last_songs: await getLastSongs() + last_songs: await getLastPlayedSongs() }); }) .get('/filelist', async ctx => { @@ -64,8 +69,8 @@ function setRoutes() { .get('/stats', async ctx => { ctx.body = await getRadioStatus(); }) - .get('/lastplayed', async ctx => { - ctx.body = await getLastSongs(); + .get('/lastsong', async ctx => { + ctx.body = await getLastPlayedSongs(2); }); }