Files
wehub-resource-sync 9b395f5cc3
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
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

77 lines
3.2 KiB
JavaScript

/**
* YouTube subscribe — subscribe to a channel via InnerTube subscription API.
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
import { prepareYoutubeApiPage, readYoutubeSapisid, SAPISID_HASH_FN, RESOLVE_CHANNEL_HANDLE_FN } from './utils.js';
import { CommandExecutionError, AuthRequiredError } from '@jackwener/opencli/errors';
cli({
site: 'youtube',
name: 'subscribe',
access: 'write',
description: 'Subscribe to a YouTube channel',
domain: 'www.youtube.com',
strategy: Strategy.COOKIE,
args: [
{ name: 'channel', required: true, positional: true, help: 'Channel ID (UCxxxx) or handle (@name)' },
],
columns: ['status', 'message'],
func: async (page, kwargs) => {
const channelInput = String(kwargs.channel);
await prepareYoutubeApiPage(page);
// Read SAPISID directly from the cookie store via CDP — zero document.cookie round-trip
const sapisid = await readYoutubeSapisid(page);
if (!sapisid)
throw new AuthRequiredError('www.youtube.com', 'Not logged in (SAPISID cookie missing)');
const result = await page.evaluate(`
(async () => {
${SAPISID_HASH_FN}
const cfg = window.ytcfg?.data_ || {};
const apiKey = cfg.INNERTUBE_API_KEY;
const context = cfg.INNERTUBE_CONTEXT;
if (!apiKey || !context) return { error: 'config', message: 'YouTube config not found' };
const authHash = await getSapisidHash(${JSON.stringify(sapisid)}, 'https://www.youtube.com');
if (!authHash) return { error: 'auth', message: 'Not logged in (SAPISID cookie missing)' };
${RESOLVE_CHANNEL_HANDLE_FN}
let channelId = ${JSON.stringify(channelInput)};
channelId = await resolveChannelHandle(channelId, apiKey, context);
if (!channelId.startsWith('UC')) {
return { error: 'arg', message: 'Could not resolve channel ID from: ' + ${JSON.stringify(channelInput)} };
}
const resp = await fetch('/youtubei/v1/subscription/subscribe?key=' + apiKey + '&prettyPrint=false', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Authorization': authHash,
'X-Origin': 'https://www.youtube.com',
},
body: JSON.stringify({ context, channelIds: [channelId] }),
});
if (resp.status === 401 || resp.status === 403) return { error: 'auth', message: 'Not logged in' };
if (!resp.ok) {
const body = await resp.json().catch(() => ({}));
const errStatus = body?.error?.status || '';
if (errStatus === 'UNAUTHENTICATED') return { error: 'auth', message: 'Not logged in' };
return { error: 'http', message: 'HTTP ' + resp.status + (errStatus ? ' ' + errStatus : '') };
}
return { ok: true, channelId };
})()
`);
if (result?.error === 'auth') {
throw new AuthRequiredError('www.youtube.com');
}
if (result?.error) {
throw new CommandExecutionError(result.message || 'Failed to subscribe');
}
return [{ status: 'success', message: 'Subscribed to: ' + (result.channelId || channelInput) }];
},
});