2023-06-05 11:27:29 +00:00
|
|
|
import { Command } from "./dependencies.ts";
|
2024-02-28 12:15:26 +00:00
|
|
|
import { pathExists } from "./utilities.ts";
|
2022-12-09 11:26:08 +00:00
|
|
|
|
|
|
|
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!")
|
2024-02-28 12:15:26 +00:00
|
|
|
.option("--current-song", "Get the currently playing song.")
|
2022-12-09 11:26:08 +00:00
|
|
|
.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.")
|
2022-12-09 11:26:08 +00:00
|
|
|
.option(
|
|
|
|
"--volume <volume:number>",
|
2023-04-22 11:56:13 +00:00
|
|
|
"Change the volume by a relative amount.",
|
2022-12-09 11:26:08 +00:00
|
|
|
)
|
2023-04-26 09:52:55 +00:00
|
|
|
.option("--write-image", "Write the album cover image to a temporary file.")
|
2022-12-09 11:26:08 +00:00
|
|
|
.parse(Deno.args);
|
|
|
|
|
2022-12-12 16:34:02 +00:00
|
|
|
if (options.currentSong) {
|
2024-02-28 12:15:26 +00:00
|
|
|
await getCurrentSong();
|
2022-12-12 16:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 11:26:08 +00:00
|
|
|
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);
|
|
|
|
}
|
2023-04-26 09:52:55 +00:00
|
|
|
|
|
|
|
if (options.writeImage) {
|
|
|
|
const status = await getStatus();
|
|
|
|
const path = `/tmp/tauon-cover-${status.id}.jpg`;
|
2023-06-05 11:27:29 +00:00
|
|
|
if (await pathExists(path)) {
|
2023-04-26 09:52:55 +00:00
|
|
|
console.log(path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const image = await fetch(`${remoteApi}/pic/medium/${status.id}`);
|
|
|
|
if (image.body === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageBuffer = new Uint8Array(await image.arrayBuffer());
|
|
|
|
await Deno.writeFile(path, imageBuffer);
|
|
|
|
console.log(path);
|
|
|
|
}
|
2022-12-09 11:26:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 13:52:49 +00:00
|
|
|
/** The status data from Tauon's remote API. */
|
2022-12-12 16:34:02 +00:00
|
|
|
type Status = {
|
2023-04-22 10:12:34 +00:00
|
|
|
album: string;
|
|
|
|
artist: string;
|
2023-04-26 09:52:55 +00:00
|
|
|
id: number;
|
2023-04-22 10:12:34 +00:00
|
|
|
progress: number;
|
2024-02-28 15:34:02 +00:00
|
|
|
status: "playing" | "paused" | "stopped";
|
2023-04-22 10:12:34 +00:00
|
|
|
title: string;
|
|
|
|
track: {
|
|
|
|
duration: number;
|
|
|
|
};
|
2022-12-12 16:34:02 +00:00
|
|
|
};
|
|
|
|
|
2023-07-31 13:52:49 +00:00
|
|
|
/** Get the {@linkcode Status} from Tauon's remote API. */
|
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
|
|
|
|
2024-02-28 12:15:26 +00:00
|
|
|
/** Print the current song's artist and title. */
|
|
|
|
async function getCurrentSong(): Promise<void> {
|
2023-04-22 10:12:34 +00:00
|
|
|
const status = await getStatus();
|
2024-02-28 15:34:02 +00:00
|
|
|
if (status.status === "playing") {
|
2024-03-28 18:19:43 +00:00
|
|
|
console.log(`${status.artist} - ${status.title}`);
|
|
|
|
} else {
|
|
|
|
console.log("");
|
2024-02-28 12:15:26 +00:00
|
|
|
}
|
2022-12-12 16:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 11:26:08 +00:00
|
|
|
if (import.meta.main) {
|
|
|
|
void main();
|
|
|
|
}
|