From 38630ebb34767314e584522d947859631fe84c0f Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 22 Jan 2022 19:00:11 +0400 Subject: [PATCH] Show start time of songs in last played songs. Made yesterday, commit today, yay. :) --- radio/index.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/radio/index.js b/radio/index.js index 0f7c075..8ae50f8 100644 --- a/radio/index.js +++ b/radio/index.js @@ -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]; }); }