From 720b4b7f41a6394c074c22336bb22cc6e006b554 Mon Sep 17 00:00:00 2001 From: Igor Soarez Date: Wed, 18 Nov 2026 12:44:11 +0000 Subject: castle-search: YouTube transcripts, filed in a folder and returned POST /youtube-transcript, described at /youtube/openapi.json: captions only, manual over automatic, rolling repeats removed, filed as -.txt. yt-dlp updates itself daily and on failure; a 429 falls back to youtube-transcript-api. Hermes's recipe from bambos. --- castle-search/test/youtube.test.ts | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 castle-search/test/youtube.test.ts (limited to 'castle-search/test') diff --git a/castle-search/test/youtube.test.ts b/castle-search/test/youtube.test.ts new file mode 100644 index 0000000..c5530fb --- /dev/null +++ b/castle-search/test/youtube.test.ts @@ -0,0 +1,72 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { handleTranscript } from "../src/http.ts"; +import { cueTexts, dedupeRolling, isoDate, paragraphs, titleSlug, videoIdOf } from "../src/youtube.ts"; + +test("every shape of YouTube link gives the video id, share junk ignored", () => { + for (const url of [ + "https://www.youtube.com/watch?v=jNQXAC9IVRw", + "https://youtube.com/watch?feature=share&v=jNQXAC9IVRw&t=30s", + "https://youtu.be/jNQXAC9IVRw?si=T-d5TUXNFjmR1Uad", + "https://www.youtube.com/shorts/jNQXAC9IVRw", + "https://www.youtube.com/live/jNQXAC9IVRw?feature=shared", + "https://m.youtube.com/watch?v=jNQXAC9IVRw&list=PL123", + "jNQXAC9IVRw", + ]) { + assert.equal(videoIdOf(url), "jNQXAC9IVRw", url); + } + assert.equal(videoIdOf("https://vimeo.com/123"), undefined); +}); + +test("titles become the archive's dashed names", () => { + assert.equal(titleSlug("How to stay calm when you know you'll be stressed | Daniel Levitin | TED"), "How-to-stay-calm-when-you-know-youll-be-stressed-Daniel-Levitin-TED"); + assert.equal(titleSlug("Anthropic Files for IPO: Nasdaq!"), "Anthropic-Files-for-IPO-Nasdaq"); + assert.equal(titleSlug("Café Müller"), "Cafe-Muller"); + assert.equal(titleSlug("!!!"), "transcript"); + assert.equal(isoDate("20051024"), "2005-10-24"); +}); + +// The shape of YouTube's automatic captions: each cue repeats the last one's words. +const ROLLING = `WEBVTT +Kind: captions +Language: en + +00:00:00.000 --> 00:00:02.000 align:start position:0% +the filing is out<00:00:01.000><c> ENT</c><00:00:01.500><c> is</c> + +00:00:02.000 --> 00:00:02.010 align:start position:0% +the filing is out ENT is + +00:00:02.010 --> 00:00:04.000 align:start position:0% +the filing is out ENT is +going to be [Music] big & bold + +00:00:04.000 --> 00:00:06.000 +going to be big & bold for everyone +`; + +test("rolling captions come out once, markup and noise gone", () => { + const cues = cueTexts(ROLLING); + assert.equal(cues[0], "the filing is out ENT is"); + assert.equal(dedupeRolling(cues).join(" "), "the filing is out ENT is going to be big & bold for everyone"); +}); + +test("words are set in paragraphs of 90", () => { + const words = Array.from({ length: 200 }, (_, i) => `w${i}`); + const text = paragraphs(words); + assert.equal(text.split("\n\n").length, 3); + assert.equal(text.split("\n\n")[0]!.split(" ").length, 90); +}); + +test("the transcript route checks the link and language first", async () => { + const signal = new AbortController().signal; + const never = async (): Promise<never> => { + throw new Error("must not run"); + }; + assert.equal((await handleTranscript(JSON.stringify({ url: "https://vimeo.com/1" }), never, signal)).status, 400); + assert.equal((await handleTranscript(JSON.stringify({ url: "https://youtu.be/jNQXAC9IVRw", language: "english!" }), never, signal)).status, 400); + const seen: unknown[] = []; + await handleTranscript(JSON.stringify({ url: "https://youtu.be/jNQXAC9IVRw", language: "pt", max_chars: 10 }), async (r) => (seen.push(r), {}) as never, signal); + assert.deepEqual(seen, [{ url: "https://youtu.be/jNQXAC9IVRw", language: "pt", maxChars: 1000 }]); +}); -- cgit v1.2.3