videoscriber-backend / scripts /export-archive-markdown.js
Rimas Kavaliauskas
Switch Space to Docker backend and sync Videoscriber post-processing
6782be3
import 'dotenv/config';
import fs from 'node:fs';
import path from 'node:path';
import { getMongoClient, getMongoDb } from '../lib/mongo.js';
import { COLLECTIONS } from '../db/collections.js';
import { renderUserFacingMarkdown } from '../services/archive/user-facing-artifacts.js';
function parseArgs(argv) {
const args = {
videoKey: '',
outDir: 'output',
debug: false,
};
for (const raw of argv) {
const arg = String(raw || '').trim();
if (arg.startsWith('--video-key=')) {
args.videoKey = arg.slice('--video-key='.length);
continue;
}
if (arg.startsWith('--out=')) {
args.outDir = arg.slice('--out='.length);
continue;
}
if (arg === '--debug') {
args.debug = true;
}
}
return args;
}
async function main() {
const { videoKey, outDir, debug } = parseArgs(process.argv.slice(2));
if (!videoKey) {
console.error('Usage: node scripts/export-archive-markdown.js --video-key=vid_... [--out=output] [--debug]');
process.exit(1);
}
try {
const db = await getMongoDb();
const videos = db.collection(COLLECTIONS.videos);
const texts = db.collection(COLLECTIONS.videoTexts);
const segments = db.collection(COLLECTIONS.videoSegments);
const video = await videos.findOne({ video_key: videoKey });
if (!video) {
console.error(`Archive video not found: ${videoKey}`);
process.exit(1);
}
const videoText = await texts.findOne({ video_id: video._id });
const segmentRows = debug
? await segments.find({ video_id: video._id }).sort({ seq: 1 }).toArray()
: [];
const markdown = renderUserFacingMarkdown(
{
video,
videoText,
segments: segmentRows,
},
{
includeEdited: true,
debug,
}
);
const outputDir = path.resolve(outDir);
fs.mkdirSync(outputDir, { recursive: true });
const suffix = debug ? '.debug.md' : '.md';
const outputPath = path.join(outputDir, `${video.video_key}${suffix}`);
fs.writeFileSync(outputPath, markdown, 'utf8');
console.log(
JSON.stringify(
{
outputPath,
mode: debug ? 'debug' : 'user-facing',
includes: debug
? [
'content_summary_1p',
'content_outline_thematic',
'content_edited',
'content_raw',
'content_clean',
'content_topic_map',
'segments',
]
: ['content_summary_1p', 'content_outline_thematic', 'content_edited'],
},
null,
2
)
);
} finally {
// Keep script process non-sticky in local runs.
try {
const client = await getMongoClient();
await client.close();
} catch {
// ignore close errors
}
}
}
main().catch((error) => {
console.error('Failed to export archive markdown:', error);
process.exit(1);
});