2021-02-10 01:01:50 +04:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2021-08-31 22:11:35 +04:00
|
|
|
const uti = require("util");
|
|
|
|
const exec = uti.promisify(require("child_process").exec);
|
2021-02-10 01:01:50 +04:00
|
|
|
|
|
|
|
const Koa = require("koa");
|
|
|
|
const koaPug = require("koa-pug");
|
|
|
|
const koaRouter = require("koa-router");
|
|
|
|
const fetch = require("node-fetch");
|
|
|
|
|
|
|
|
const config = require("./config");
|
2021-06-05 18:58:22 +04:00
|
|
|
const util = require("../shared/util");
|
2021-02-10 01:01:50 +04:00
|
|
|
|
2021-02-17 03:44:34 +04:00
|
|
|
async function getRadioStatus() {
|
2021-02-17 19:34:44 +04:00
|
|
|
try {
|
2021-11-16 18:21:26 +04:00
|
|
|
let status = await fetch('http://radio.arav.home.arpa/status-json.xsl').then(r => r.json());
|
2021-02-17 19:34:44 +04:00
|
|
|
return {
|
|
|
|
server_start_iso8601: status.icestats.server_start_iso8601,
|
|
|
|
server_start_date: util.datetime(status.icestats.server_start_iso8601, util.post_date_format),
|
|
|
|
song: `${status.icestats.source.artist} - ${status.icestats.source.title}`,
|
|
|
|
listener_peak: status.icestats.source.listener_peak,
|
|
|
|
listeners: status.icestats.source.listeners
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
return {
|
|
|
|
server_start_iso8601: "n/a",
|
|
|
|
server_start_date: "n/a",
|
|
|
|
song: "n/a",
|
|
|
|
listener_peak: "n/a",
|
|
|
|
listeners: "n/a"
|
|
|
|
}
|
2021-02-17 03:44:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 22:10:15 +04:00
|
|
|
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 [];
|
|
|
|
}
|
2021-08-31 22:11:35 +04:00
|
|
|
}
|
|
|
|
|
2021-06-05 18:58:22 +04:00
|
|
|
function setRoutes() {
|
|
|
|
return koaRouter().get('/', async ctx => {
|
2021-02-10 01:01:50 +04:00
|
|
|
await ctx.render('index', {
|
2021-02-16 01:43:52 +04:00
|
|
|
title: "/ Radio",
|
2021-02-10 01:01:50 +04:00
|
|
|
description: "Internet-radio broadcasting from under my desk.",
|
2021-02-17 03:44:34 +04:00
|
|
|
main_site: util.getServiceByHost(ctx.header.host),
|
2021-08-31 22:11:35 +04:00
|
|
|
radio_status: await getRadioStatus(),
|
2021-09-01 22:10:15 +04:00
|
|
|
last_songs: await getLastPlayedSongs()
|
2021-02-10 01:01:50 +04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.get('/stats', async ctx => {
|
2021-02-17 03:44:34 +04:00
|
|
|
ctx.body = await getRadioStatus();
|
2021-08-31 22:11:35 +04:00
|
|
|
})
|
2021-09-01 22:10:15 +04:00
|
|
|
.get('/lastsong', async ctx => {
|
2021-09-01 22:17:09 +04:00
|
|
|
ctx.body = (await getLastPlayedSongs(2))[0];
|
2021-02-10 01:01:50 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-05 18:58:22 +04:00
|
|
|
const app = new Koa();
|
|
|
|
const pug = new koaPug({
|
|
|
|
viewPath: path.join(__dirname, "views"),
|
|
|
|
locals: {
|
|
|
|
moment: util.datetime },
|
|
|
|
app: app
|
|
|
|
});
|
2021-02-10 01:01:50 +04:00
|
|
|
|
2021-06-05 18:58:22 +04:00
|
|
|
app.proxy = true;
|
|
|
|
app
|
|
|
|
.use(setRoutes().routes())
|
|
|
|
.listen(config.port, config.host);
|