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
68 lines
2.8 KiB
JavaScript
68 lines
2.8 KiB
JavaScript
/**
|
|
* YouTube like — like a video via InnerTube like API (requires SAPISIDHASH auth).
|
|
*/
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { parseVideoId, prepareYoutubeApiPage, readYoutubeSapisid, SAPISID_HASH_FN } from './utils.js';
|
|
import { CommandExecutionError, AuthRequiredError } from '@jackwener/opencli/errors';
|
|
|
|
cli({
|
|
site: 'youtube',
|
|
name: 'like',
|
|
access: 'write',
|
|
description: 'Like a YouTube video',
|
|
domain: 'www.youtube.com',
|
|
strategy: Strategy.COOKIE,
|
|
args: [
|
|
{ name: 'url', required: true, positional: true, help: 'YouTube video URL or video ID' },
|
|
],
|
|
columns: ['status', 'message'],
|
|
func: async (page, kwargs) => {
|
|
const videoId = parseVideoId(String(kwargs.url));
|
|
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)' };
|
|
|
|
const resp = await fetch('/youtubei/v1/like/like?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, target: { videoId: ${JSON.stringify(videoId)} } }),
|
|
});
|
|
|
|
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 };
|
|
})()
|
|
`);
|
|
if (result?.error === 'auth') {
|
|
throw new AuthRequiredError('www.youtube.com');
|
|
}
|
|
if (result?.error) {
|
|
throw new CommandExecutionError(result.message || 'Failed to like video');
|
|
}
|
|
return [{ status: 'success', message: 'Liked: ' + videoId }];
|
|
},
|
|
});
|