Answer searches from YouTube InnerTube directly with yt-dlp fallback
This commit is contained in:
46
server/innertube.js
Normal file
46
server/innertube.js
Normal file
@@ -0,0 +1,46 @@
|
||||
/* innertube.js — YouTube search via the InnerTube JSON API (no yt-dlp spawn).
|
||||
* parseSearch() is pure (tested against fixtures/innertube-search.json);
|
||||
* search() does the HTTP call. Callers MUST fall back to yt-dlp on any throw. */
|
||||
const CLIENT = { clientName: 'WEB', clientVersion: '2.20250101.00.00', hl: 'en', gl: 'US' };
|
||||
|
||||
export function lengthToSeconds(s) {
|
||||
if (typeof s !== 'string' || !/^\d+(:\d{1,2}){0,2}$/.test(s.trim())) return 0;
|
||||
return s.trim().split(':').map(Number).reduce((acc, n) => acc * 60 + n, 0);
|
||||
}
|
||||
|
||||
export function parseSearch(json) {
|
||||
const sections = json?.contents?.twoColumnSearchResultsRenderer?.primaryContents
|
||||
?.sectionListRenderer?.contents;
|
||||
if (!Array.isArray(sections)) throw new Error('innertube: unexpected response shape');
|
||||
const out = [];
|
||||
for (const s of sections) {
|
||||
for (const it of s?.itemSectionRenderer?.contents || []) {
|
||||
const v = it && it.videoRenderer;
|
||||
if (!v || typeof v.videoId !== 'string') continue;
|
||||
const owner = v.ownerText?.runs?.[0] || {};
|
||||
const be = owner.navigationEndpoint?.browseEndpoint || {};
|
||||
const path = be.canonicalBaseUrl || (be.browseId ? `/channel/${be.browseId}` : '');
|
||||
out.push({
|
||||
id: v.videoId,
|
||||
title: v.title?.runs?.map((r) => r.text).join('') || '(untitled)',
|
||||
channel: owner.text || '',
|
||||
channelId: be.browseId || '',
|
||||
channelUrl: path ? `https://www.youtube.com${path}` : '',
|
||||
duration: lengthToSeconds(v.lengthText?.simpleText),
|
||||
thumbnail: `https://i.ytimg.com/vi/${v.videoId}/mqdefault.jpg`,
|
||||
});
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function search(q, { fetchImpl = fetch, timeoutMs = 6000 } = {}) {
|
||||
const res = await fetchImpl('https://www.youtube.com/youtubei/v1/search?prettyPrint=false', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ context: { client: CLIENT }, query: q }),
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
});
|
||||
if (!res.ok) throw new Error(`innertube: HTTP ${res.status}`);
|
||||
return parseSearch(await res.json());
|
||||
}
|
||||
29
server/innertube.test.js
Normal file
29
server/innertube.test.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { test, expect } from 'bun:test';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { parseSearch, lengthToSeconds, search } from './innertube.js';
|
||||
const fixture = JSON.parse(readFileSync(new URL('./fixtures/innertube-search.json', import.meta.url)));
|
||||
|
||||
test('parses video renderers into slim cards', () => {
|
||||
const r = parseSearch(fixture);
|
||||
expect(r.length).toBe(4);
|
||||
expect(r[0]).toEqual({
|
||||
id: 'nQWFzMvCfLE', title: 'What A Beautiful Name - Hillsong Worship', channel: 'Hillsong Worship',
|
||||
channelId: 'UC4q12NoPNySbVqwpw4iO5Vg', channelUrl: 'https://www.youtube.com/channel/UC4q12NoPNySbVqwpw4iO5Vg',
|
||||
duration: 343, thumbnail: 'https://i.ytimg.com/vi/nQWFzMvCfLE/mqdefault.jpg',
|
||||
});
|
||||
expect(r[1].duration).toBe(7187);
|
||||
expect(r[3].duration).toBe(0); // live, no lengthText
|
||||
});
|
||||
test('lengthToSeconds', () => {
|
||||
expect(lengthToSeconds('5:43')).toBe(343);
|
||||
expect(lengthToSeconds('1:59:47')).toBe(7187);
|
||||
expect(lengthToSeconds('LIVE')).toBe(0);
|
||||
expect(lengthToSeconds(undefined)).toBe(0);
|
||||
});
|
||||
test('unexpected shape throws (caller falls back to yt-dlp)', () => {
|
||||
expect(() => parseSearch({})).toThrow();
|
||||
});
|
||||
test('search() throws on HTTP error', async () => {
|
||||
const fetchImpl = async () => new Response('no', { status: 429 });
|
||||
await expect(search('x', { fetchImpl })).rejects.toThrow('429');
|
||||
});
|
||||
@@ -6,7 +6,7 @@
|
||||
"scripts": {
|
||||
"start": "bun server.js",
|
||||
"dev": "bun --hot server.js",
|
||||
"test": "bun test --timeout 60000 ./media-cache.test.js && bun test ./notes.test.js && bun test ./remote.test.js && bun test ./party.test.js && bun test ./uploads.test.js"
|
||||
"test": "bun test --timeout 60000 ./media-cache.test.js && bun test ./notes.test.js && bun test ./remote.test.js && bun test ./party.test.js && bun test ./uploads.test.js && bun test ./innertube.test.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@hono/node-server": "^1.14.0",
|
||||
|
||||
@@ -48,6 +48,7 @@ import { registerNoteRoutes, parseLrc, sanitizeLyrics } from './notes.js';
|
||||
import { createRemoteHub } from './remote.js';
|
||||
import { createPartyHub } from './party.js';
|
||||
import { registerUploadRoutes } from './uploads.js';
|
||||
import * as innertube from './innertube.js';
|
||||
import QRCode from 'qrcode';
|
||||
import { dirname, join as pathJoin } from 'node:path';
|
||||
|
||||
@@ -382,12 +383,23 @@ app.get('/api/search', async (c) => {
|
||||
let mine = [];
|
||||
try { mine = (await notesDb.listUploads({ q, limit: 20 })).map(uploads.card); } catch { /* library optional */ }
|
||||
try {
|
||||
const out = await runYtdlpResilient([
|
||||
`ytsearch${SEARCH_LIMIT}:${q}`,
|
||||
'--dump-json', '--flat-playlist',
|
||||
'--no-warnings', '--ignore-errors',
|
||||
]);
|
||||
const results = [...mine, ...parseCards(out)];
|
||||
let yt = [];
|
||||
if (process.env.SEARCH_INNERTUBE !== '0') {
|
||||
try {
|
||||
yt = await innertube.search(q);
|
||||
} catch (e) {
|
||||
console.warn(`[search] innertube failed, using yt-dlp: ${e.message}`);
|
||||
}
|
||||
}
|
||||
if (!yt.length) {
|
||||
const out = await runYtdlpResilient([
|
||||
`ytsearch${SEARCH_LIMIT}:${q}`,
|
||||
'--dump-json', '--flat-playlist',
|
||||
'--no-warnings', '--ignore-errors',
|
||||
]);
|
||||
yt = parseCards(out);
|
||||
}
|
||||
const results = [...mine, ...yt];
|
||||
if (searchCache.size >= SEARCH_CACHE_MAX) searchCache.delete(searchCache.keys().next().value);
|
||||
searchCache.set(cacheKey, { results, expiresAt: Date.now() + SEARCH_CACHE_TTL_MS });
|
||||
return c.json({ ok: true, results });
|
||||
|
||||
Reference in New Issue
Block a user