Show start time of songs in last played songs. Made yesterday, commit today, yay. :)

This commit is contained in:
Alexander Andreev 2022-01-22 19:00:11 +04:00
parent d7b0997ef0
commit 38630ebb34
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F
1 changed files with 12 additions and 7 deletions

View File

@ -7,6 +7,7 @@ const Koa = require("koa");
const koaPug = require("koa-pug");
const koaRouter = require("koa-router");
const fetch = require("node-fetch");
const moment = require("moment-timezone");
const config = require("./config");
const util = require("../shared/util");
@ -32,16 +33,20 @@ async function getRadioStatus() {
}
}
async function getLastPlayedSongs(count=11) {
async function getLastPlayedSongs(count, tz) {
try {
const { stdout, _ } = await exec(`tail -n ${count} /var/log/icecast/playlist.log | awk -F '|' 'NR<${count} { print $4 }'`);
const { stdout, _ } = await exec(`tail -n${count} /var/log/icecast/playlist.log | head -n-1 | cut -d"|" -f1,4`);
console.log(tz);
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] };
let [t, s] = songs[i].split('|');
t = moment(t, "DD/MMM/YYYY:HH:mm:ss ZZ").utc(false).tz(tz).format("HH:mm");
let song = s.split(' - ');
songs[i] = { "start_time_local": t,"artist": song[0], "title": song[1] };
}
return songs;
} catch {
} catch(err) {
console.log(err);
return [];
}
}
@ -53,14 +58,14 @@ function setRoutes() {
description: "Internet-radio broadcasting from under my desk.",
main_site: util.getServiceByHost(ctx.header.host),
radio_status: await getRadioStatus(),
last_songs: await getLastPlayedSongs()
last_songs: await getLastPlayedSongs(11, util.getClientTimezone(ctx))
});
})
.get('/stats', async ctx => {
ctx.body = await getRadioStatus();
})
.get('/lastsong', async ctx => {
ctx.body = (await getLastPlayedSongs(2))[0];
ctx.body = (await getLastPlayedSongs(2, util.getClientTimezone(ctx)))[0];
});
}