Files
wehub-resource-sync 9b395f5cc3
Build Chrome Extension / build (push) Waiting to run
Trigger Website Rebuild (Docs Updated) / dispatch (push) Waiting to run
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

59 lines
2.1 KiB
JavaScript

/**
* YouTube subscriptions — list of subscribed channels from /feed/channels.
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
import { CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { extractSubscriptionChannel } from './utils.js';
cli({
site: 'youtube',
name: 'subscriptions',
access: 'read',
description: 'List subscribed YouTube channels',
domain: 'www.youtube.com',
strategy: Strategy.COOKIE,
args: [
{ name: 'limit', type: 'int', default: 50, help: 'Max channels to return (default 50)' },
],
columns: ['rank', 'name', 'handle', 'subscribers', 'url'],
func: async (page, kwargs) => {
const limit = Math.min(kwargs.limit || 50, 1000);
await page.goto('https://www.youtube.com/feed/channels');
await page.wait(3);
const data = await page.evaluate(`
(async () => {
const d = window.ytInitialData;
if (!d) return { error: 'YouTube data not found — are you logged in?' };
const limit = ${limit};
const items = d.contents?.twoColumnBrowseResultsRenderer
?.tabs?.[0]?.tabRenderer?.content
?.sectionListRenderer?.contents?.[0]
?.itemSectionRenderer?.contents?.[0]
?.shelfRenderer?.content
?.expandedShelfContentsRenderer?.items || [];
const extractChannel = ${extractSubscriptionChannel.toString()};
const channels = [];
for (const item of items) {
if (channels.length >= limit) break;
const ch = extractChannel(item.channelRenderer);
if (ch?.name) channels.push(ch);
}
return channels;
})()
`);
if (!Array.isArray(data)) {
const errMsg = data && typeof data === 'object' ? String(data.error || '') : '';
throw new CommandExecutionError(errMsg || 'Failed to fetch subscriptions — make sure you are logged into YouTube');
}
if (data.length === 0) {
throw new EmptyResultError('youtube subscriptions');
}
return data.map((ch, i) => ({ rank: i + 1, ...ch }));
},
});