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); }); }