Implemented functionality of getting last played songs.

This commit is contained in:
Alexander Andreev 2021-08-31 22:11:35 +04:00
parent 76a4f42d7a
commit f89fbc2aa9
Signed by: Arav
GPG Key ID: 610DF2574456329F

View File

@ -1,5 +1,7 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const uti = require("util");
const exec = uti.promisify(require("child_process").exec);
const Koa = require("koa"); const Koa = require("koa");
const koaPug = require("koa-pug"); 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() { function setRoutes() {
return koaRouter().get('/', async ctx => { return koaRouter().get('/', async ctx => {
await ctx.render('index', { await ctx.render('index', {
title: "/ Radio", title: "/ Radio",
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()
}); });
}) })
.get('/filelist', async ctx => { .get('/filelist', async ctx => {
@ -51,6 +63,9 @@ function setRoutes() {
}) })
.get('/stats', async ctx => { .get('/stats', async ctx => {
ctx.body = await getRadioStatus(); ctx.body = await getRadioStatus();
})
.get('/lastplayed', async ctx => {
ctx.body = await getLastSongs();
}); });
} }