Files
yt2press/server.js
T
2026-02-03 12:48:21 +07:00

119 lines
3.1 KiB
JavaScript

// @ts-check
import { WebSocketServer } from "ws";
import { readFileSync, mkdirSync, writeFileSync } from "fs";
import { join as joinPath } from "path";
import matter from "gray-matter";
import { renderFile } from "./group-similar.js";
const PORT = 8080;
const wss = new WebSocketServer({ port: PORT });
console.log(`WebSocket server running on ws://localhost:${PORT}`);
wss.on("connection", (ws) => {
console.log("New client connected");
let filename = "";
const ROOT_DIR = "docs";
ws.on("message", (message, isBinary) => {
if (message instanceof Buffer && isBinary) {
writeFileSync(joinPath(ROOT_DIR, filename), message);
return;
}
const { name, ...payload } = JSON.parse(message.toString("utf-8"));
switch (name) {
case "download_q":
{
const { q, duration } = payload;
console.log(`Start ${q}`);
const q_stem = q
.replace(/\.[a-z0-9]+$/, "")
.replace(/\[.+?\]/g, "")
.replace(/\s+/g, "_")
.trim();
try {
mkdirSync(joinPath(ROOT_DIR, q_stem));
} catch (e) {}
let isChanged = false;
const ts = [];
const mdFile = joinPath(ROOT_DIR, `${q_stem}.md`);
let { data, content } = matter(readFileSync(mdFile, "utf-8"));
data.video = "/" + q;
const { dedup, interval } = data;
if (typeof interval === "number") {
for (let t = 0; t < duration; t += interval) {
const min = Math.floor(t / 60);
const sec = Math.floor(t - min * 60);
content += `\n${min}:${sec.toString().padStart(2, "0")}\n`;
}
}
const re = /^(?<min>\d{1,2}):(?<sec>\d{2})\s*$/;
const lines = content.split("\n");
content = "";
for (const line of lines) {
const m = re.exec(line);
if (m?.groups) {
const { min, sec } = m.groups;
const t = `${min}_${sec}`;
ts.push(t);
content += `\n![${line}](./${q_stem}/${t}.png)\n\n`;
isChanged = true;
} else {
content += `${line}\n`;
}
}
if (isChanged) {
writeFileSync(mdFile, matter.stringify(content, data));
if (dedup) {
renderFile(
mdFile,
mdFile,
typeof dedup === "number" ? dedup : undefined,
).then((stats) => {
// Don't log a thing yet.
});
}
ws.send(JSON.stringify({ name: "seek_q_ts", q, ts }));
}
}
break;
case "timed_q":
{
const { q, t } = payload;
const q_stem = q
.replace(/\.[a-z0-9]+$/, "")
.replace(/\[.+?\]/g, "")
.trim();
filename = t ? joinPath(q_stem, `${t}.png`) : "";
}
break;
case "done":
{
const { q } = payload;
console.log(`Done ${q}`);
}
break;
}
});
ws.on("close", () => {
console.log("Client disconnected");
});
});