add dedup script
This commit is contained in:
1 parent
2b47af46de
commit
62c6775e67
7 files changed
+1009
-58
No files matched your search
@@ -1,92 +1,118 @@
|
||||
// @ts-check
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { WebSocketServer } from "ws";
|
||||
import { readFileSync, mkdirSync, writeFileSync } from "fs";
|
||||
import { join as joinPath } from 'path'
|
||||
import matter from 'gray-matter';
|
||||
import { join as joinPath } from "path";
|
||||
import matter from "gray-matter";
|
||||
|
||||
const PORT = 8080
|
||||
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');
|
||||
wss.on("connection", (ws) => {
|
||||
console.log("New client connected");
|
||||
|
||||
let filename = ''
|
||||
const ROOT_DIR = 'docs';
|
||||
let filename = "";
|
||||
const ROOT_DIR = "docs";
|
||||
|
||||
ws.on('message', (message, isBinary) => {
|
||||
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'));
|
||||
const { name, ...payload } = JSON.parse(message.toString("utf-8"));
|
||||
|
||||
switch (name) {
|
||||
case 'download_q': {
|
||||
const { q, duration } = payload;
|
||||
console.log(`Start ${q}`)
|
||||
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();
|
||||
const q_stem = q
|
||||
.replace(/\.[a-z0-9]+$/, "")
|
||||
.replace(/\[.+?\]/g, "")
|
||||
.replace(/\s+/g, "_")
|
||||
.trim();
|
||||
|
||||
try {
|
||||
mkdirSync(joinPath(ROOT_DIR, q_stem));
|
||||
} catch (e) {}
|
||||
try {
|
||||
mkdirSync(joinPath(ROOT_DIR, q_stem));
|
||||
} catch (e) {}
|
||||
|
||||
let isChanged = false;
|
||||
let isChanged = false;
|
||||
const ts = [];
|
||||
|
||||
const ts = [];
|
||||
const mdFile = joinPath(ROOT_DIR, `${q_stem}.md`);
|
||||
let { data, content } = matter(readFileSync(mdFile, "utf-8"));
|
||||
data.video = "/" + q;
|
||||
|
||||
let { data, content } = matter(readFileSync(joinPath(ROOT_DIR, `${q_stem}.md`), '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);
|
||||
|
||||
const interval = data.interval;
|
||||
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`;
|
||||
}
|
||||
}
|
||||
|
||||
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\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 }));
|
||||
}
|
||||
}
|
||||
|
||||
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\n\n`;
|
||||
isChanged = true;
|
||||
} else {
|
||||
content += `${line}\n`;
|
||||
}
|
||||
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`) : "";
|
||||
}
|
||||
|
||||
if (isChanged){
|
||||
writeFileSync(joinPath(ROOT_DIR, `${q_stem}.md`), matter.stringify(content, data));
|
||||
ws.send(JSON.stringify({ name: "seek_q_ts", q, ts }));
|
||||
break;
|
||||
case "done":
|
||||
{
|
||||
const { q } = payload;
|
||||
console.log(`Done ${q}`);
|
||||
}
|
||||
} 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;
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('Client disconnected');
|
||||
ws.on("close", () => {
|
||||
console.log("Client disconnected");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user