From f89fbc2aa9d01adb8ceecf3417a0e271b8ba5f99 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 31 Aug 2021 22:11:35 +0400 Subject: [PATCH] Implemented functionality of getting last played songs. --- radio/index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/radio/index.js b/radio/index.js index 67a97fd..6b6bfb4 100644 --- a/radio/index.js +++ b/radio/index.js @@ -1,5 +1,7 @@ const fs = require("fs"); const path = require("path"); +const uti = require("util"); +const exec = uti.promisify(require("child_process").exec); const Koa = require("koa"); const koaPug = require("koa-pug"); @@ -30,13 +32,23 @@ 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; +} + function setRoutes() { return koaRouter().get('/', async ctx => { await ctx.render('index', { title: "/ Radio", description: "Internet-radio broadcasting from under my desk.", main_site: util.getServiceByHost(ctx.header.host), - radio_status: await getRadioStatus() + radio_status: await getRadioStatus(), + last_songs: await getLastSongs() }); }) .get('/filelist', async ctx => { @@ -51,6 +63,9 @@ function setRoutes() { }) .get('/stats', async ctx => { ctx.body = await getRadioStatus(); + }) + .get('/lastplayed', async ctx => { + ctx.body = await getLastSongs(); }); }