watch-later.js
1 /** 2 * YouTube watch-later — the user's Watch Later queue. 3 * Navigates to /playlist?list=WL and reads ytInitialData directly. 4 */ 5 import { cli, Strategy } from '@jackwener/opencli/registry'; 6 import { FETCH_BROWSE_FN, extractPlaylistVideos } from './utils.js'; 7 import { CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors'; 8 9 cli({ 10 site: 'youtube', 11 name: 'watch-later', 12 description: 'Get your YouTube Watch Later queue', 13 domain: 'www.youtube.com', 14 strategy: Strategy.COOKIE, 15 args: [ 16 { name: 'limit', type: 'int', default: 50, help: 'Max videos to return (default 50, max 200)' }, 17 ], 18 columns: ['rank', 'title', 'channel', 'duration', 'views', 'published', 'url'], 19 func: async (page, kwargs) => { 20 const limit = Math.min(kwargs.limit || 50, 200); 21 await page.goto('https://www.youtube.com/playlist?list=WL'); 22 await page.wait(3); 23 const data = await page.evaluate(` 24 (async () => { 25 const d = window.ytInitialData; 26 if (!d) return { error: 'YouTube data not found — are you logged in?' }; 27 28 const limit = ${limit}; 29 const cfg = window.ytcfg?.data_ || {}; 30 const apiKey = cfg.INNERTUBE_API_KEY; 31 const context = cfg.INNERTUBE_CONTEXT; 32 33 const header = d.header?.playlistHeaderRenderer; 34 const title = header?.title?.simpleText || 'Watch Later'; 35 const stats = (header?.stats || []) 36 .map(s => s.runs?.map(r => r.text)?.join('') || s.simpleText || '') 37 .filter(Boolean); 38 39 const tabs = d.contents?.twoColumnBrowseResultsRenderer?.tabs || []; 40 let listContents = tabs[0]?.tabRenderer?.content?.sectionListRenderer?.contents?.[0]?.itemSectionRenderer?.contents?.[0]?.playlistVideoListRenderer?.contents || []; 41 42 ${FETCH_BROWSE_FN} 43 44 const extractVideos = ${extractPlaylistVideos.toString()}; 45 46 let videos = extractVideos(listContents); 47 48 let contItem = listContents[listContents.length - 1]; 49 while (videos.length < limit && contItem?.continuationItemRenderer && apiKey && context) { 50 const token = contItem.continuationItemRenderer?.continuationEndpoint?.continuationCommand?.token; 51 if (!token) break; 52 const contData = await fetchBrowse(apiKey, { context, continuation: token }); 53 if (contData.error) break; 54 const newItems = contData.onResponseReceivedActions?.[0]?.appendContinuationItemsAction?.continuationItems || []; 55 if (!newItems.length) break; 56 videos = videos.concat(extractVideos(newItems)); 57 contItem = newItems[newItems.length - 1]; 58 } 59 60 return { title, stats, videos: videos.slice(0, limit) }; 61 })() 62 `); 63 if (!data || typeof data !== 'object') { 64 throw new CommandExecutionError('Failed to fetch Watch Later — make sure you are logged into YouTube'); 65 } 66 if (data.error) { 67 throw new CommandExecutionError(String(data.error)); 68 } 69 if (!data.videos?.length) { 70 throw new EmptyResultError('youtube watch-later'); 71 } 72 const statsStr = (data.stats || []).join(' | '); 73 process.stderr.write(`${data.title} ${statsStr}\n`); 74 return data.videos; 75 }, 76 });