1
Fork 0
dotfiles/.bauke/scripts/tauon-controls.ts

86 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Command } from "./dependencies.ts";
const hiddenApi = "http://127.0.0.1:7813";
const remoteApi = "http://127.0.0.1:7814/api1";
async function main(): Promise<void> {
const { options } = await new Command()
.name("tauon-controls")
.description("Small remote control CLI for Tauon Music Box!")
2022-12-12 16:34:02 +00:00
.option("--current-song", "Send a notification with the current song.")
.option("--next-song", "Play the next song.")
.option("--play-pause", "Toggle play or pause.")
.option("--previous-song", "Play the previous song.")
2023-04-22 10:12:34 +00:00
.option("--print <print:string>", "Print data from the current song.")
.option(
"--volume <volume:number>",
2023-04-22 11:56:13 +00:00
"Change the volume by a relative amount.",
)
.parse(Deno.args);
2022-12-12 16:34:02 +00:00
if (options.currentSong) {
await notifyCurrentSong();
}
if (options.nextSong) {
await fetch(`${remoteApi}/next`);
}
if (options.playPause) {
await fetch(`${hiddenApi}/playpause/`);
}
if (options.previousSong) {
await fetch(`${remoteApi}/back`);
}
if (options.volume !== undefined) {
await fetch(`${remoteApi}/setvolumerel/${options.volume}`);
}
2023-04-22 10:12:34 +00:00
if (options.print !== undefined) {
const status = await getStatus();
const progressPercentage = ((status.progress / status.track.duration) * 100)
.toFixed(2);
const markersAndReplacements = [
["album", status.album],
["artist", status.artist],
["title", status.title],
["progress", progressPercentage],
];
let formattedString = options.print;
for (const [marker, replacement] of markersAndReplacements) {
const regex = new RegExp(`<${marker}>`, "g");
formattedString = formattedString.replace(regex, replacement);
}
console.log(formattedString);
}
}
2022-12-12 16:34:02 +00:00
type Status = {
2023-04-22 10:12:34 +00:00
album: string;
artist: string;
progress: number;
title: string;
track: {
duration: number;
};
2022-12-12 16:34:02 +00:00
};
2023-04-22 10:12:34 +00:00
async function getStatus(): Promise<Status> {
return await (await fetch(`${remoteApi}/status`)).json();
}
2022-12-12 16:34:02 +00:00
2023-04-22 10:12:34 +00:00
async function notifyCurrentSong(): Promise<void> {
const status = await getStatus();
2022-12-12 16:34:02 +00:00
await Deno.run({
cmd: ["notify-send", status.title, status.artist],
}).status();
}
if (import.meta.main) {
void main();
}