Recover related videos with bounded discovery and retryable fallbacks

This commit is contained in:
Jonathan Sykes
2026-10-04 02:42:02 +08:00
parent df4acfc9f1
commit 2042c9b6fc
15 changed files with 326 additions and 12 deletions

View File

@@ -61,6 +61,7 @@ import { registerIntakeRoutes } from './p2p-intake.js';
import { createP2pHub, holdersPayload, createRehydrator } from './p2p-hub.js';
import { sha256Range } from './hash.js';
import * as innertube from './innertube.js';
import { createRelatedService, registerRelatedRoute } from './related.js';
import * as searchCacheDb from './search-cache.js';
import { ingest as collectVideoMetadata, syncListening, linkListening, startThumbnails } from './video-catalog.js';
import { registerCatalogRoutes } from './recommendations.js';
@@ -488,6 +489,25 @@ app.get('/api/search', async (c) => {
}
});
// Eight related cards should not depend on a 200-result deep search succeeding.
registerRelatedRoute(app, createRelatedService({
next: (id, timeoutMs) => process.env.SEARCH_INNERTUBE === '0' ? Promise.reject(new Error('InnerTube disabled')) : innertube.related(id, { timeoutMs }),
search: async (query, budgetMs) => {
const start = Date.now();
try {
if (process.env.SEARCH_INNERTUBE !== '0') {
const cards = await innertube.search(query, { timeoutMs: Math.min(3500, budgetMs) });
if (cards.length) return cards;
}
} catch { /* use the existing resilient yt-dlp search path */ }
const remaining = budgetMs - (Date.now() - start);
if (remaining <= 0) throw new Error('Related search timed out');
return parseCards(await runYtdlpResilient([`ytsearch12:${query}`, '--dump-json', '--flat-playlist', '--no-warnings', '--ignore-errors', '--socket-timeout', '4', '--retries', '1'], { signal: AbortSignal.timeout(Math.min(6000, remaining)) }));
},
local: query => searchCacheDb.searchVideos(query, 20),
collect: cards => collectVideoMetadata(cards, 'related'),
}));
// GET /api/channel?c=<channel>
// Channel listing that works for every kind of channel, not one-off cases:
// 1. /videos, /streams, /shorts tabs — normal channels