/ clis / youtube / feed.js
feed.js
  1  /**
  2   * YouTube feed — homepage recommended videos.
  3   * Reads ytInitialData from the homepage directly (personalized, no separate API call needed).
  4   */
  5  import { cli, Strategy } from '@jackwener/opencli/registry';
  6  import { CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
  7  
  8  cli({
  9      site: 'youtube',
 10      name: 'feed',
 11      description: 'Get YouTube homepage recommended videos',
 12      domain: 'www.youtube.com',
 13      strategy: Strategy.COOKIE,
 14      args: [
 15          { name: 'limit', type: 'int', default: 20, help: 'Max videos to return (default 20, max 100)' },
 16      ],
 17      columns: ['rank', 'title', 'channel', 'views', 'duration', 'published', 'url'],
 18      func: async (page, kwargs) => {
 19          const limit = Math.min(kwargs.limit || 20, 100);
 20          await page.goto('https://www.youtube.com');
 21          await page.wait(3);
 22          const data = await page.evaluate(`
 23        (async () => {
 24          const d = window.ytInitialData;
 25          if (!d) return { error: 'YouTube data not found — are you logged in?' };
 26  
 27          const limit = ${limit};
 28          const cfg = window.ytcfg?.data_ || {};
 29          const apiKey = cfg.INNERTUBE_API_KEY;
 30          const context = cfg.INNERTUBE_CONTEXT;
 31  
 32          function extractFromItem(item) {
 33            // Modern lockupViewModel format
 34            const lvm = item.richItemRenderer?.content?.lockupViewModel;
 35            if (lvm && lvm.contentType === 'LOCKUP_CONTENT_TYPE_VIDEO') {
 36              const meta = lvm.metadata?.lockupMetadataViewModel;
 37              const rows = meta?.metadata?.contentMetadataViewModel?.metadataRows || [];
 38              const parts = rows.flatMap(r => (r.metadataParts || []).map(p => p.text?.content || '').filter(Boolean));
 39              let duration = '';
 40              for (const ov of (lvm.contentImage?.thumbnailViewModel?.overlays || [])) {
 41                for (const b of (ov.thumbnailBottomOverlayViewModel?.badges || [])) {
 42                  if (b.thumbnailBadgeViewModel?.text) duration = b.thumbnailBadgeViewModel.text;
 43                }
 44              }
 45              return {
 46                title: meta?.title?.content || '',
 47                channel: parts[0] || '',
 48                views: parts[1] || '',
 49                duration,
 50                published: parts[2] || '',
 51                videoId: lvm.contentId,
 52              };
 53            }
 54  
 55            // Legacy videoRenderer format
 56            const v = item.richItemRenderer?.content?.videoRenderer || item.videoRenderer;
 57            if (v?.videoId) {
 58              return {
 59                title: v.title?.runs?.[0]?.text || '',
 60                channel: v.ownerText?.runs?.[0]?.text || v.shortBylineText?.runs?.[0]?.text || '',
 61                views: v.viewCountText?.simpleText || v.shortViewCountText?.simpleText || '',
 62                duration: v.lengthText?.simpleText || '',
 63                published: v.publishedTimeText?.simpleText || '',
 64                videoId: v.videoId,
 65              };
 66            }
 67            return null;
 68          }
 69  
 70          const tabs = d.contents?.twoColumnBrowseResultsRenderer?.tabs || [];
 71          const richContents = tabs[0]?.tabRenderer?.content?.richGridRenderer?.contents || [];
 72  
 73          const videos = [];
 74          for (const item of richContents) {
 75            if (videos.length >= limit) break;
 76            const v = extractFromItem(item);
 77            if (v?.videoId) {
 78              videos.push({ rank: videos.length + 1, ...v, url: 'https://www.youtube.com/watch?v=' + v.videoId });
 79            }
 80          }
 81  
 82          // Pagination
 83          if (videos.length < limit && apiKey && context) {
 84            let contItem = richContents[richContents.length - 1];
 85            while (videos.length < limit && contItem?.continuationItemRenderer) {
 86              const token = contItem.continuationItemRenderer?.continuationEndpoint?.continuationCommand?.token;
 87              if (!token) break;
 88              const resp = await fetch('/youtubei/v1/browse?key=' + apiKey + '&prettyPrint=false', {
 89                method: 'POST', credentials: 'include',
 90                headers: { 'Content-Type': 'application/json' },
 91                body: JSON.stringify({ context, continuation: token }),
 92              });
 93              if (!resp.ok) break;
 94              const contData = await resp.json();
 95              const newItems = contData.onResponseReceivedActions?.[0]?.appendContinuationItemsAction?.continuationItems || [];
 96              if (!newItems.length) break;
 97              for (const item of newItems) {
 98                if (videos.length >= limit) break;
 99                const v = extractFromItem(item);
100                if (v?.videoId) {
101                  videos.push({ rank: videos.length + 1, ...v, url: 'https://www.youtube.com/watch?v=' + v.videoId });
102                }
103              }
104              contItem = newItems[newItems.length - 1];
105            }
106          }
107  
108          return videos;
109        })()
110      `);
111          if (!Array.isArray(data)) {
112              const errMsg = data && typeof data === 'object' ? String(data.error || '') : '';
113              throw new CommandExecutionError(errMsg || 'Failed to fetch YouTube feed');
114          }
115          if (data.length === 0) {
116              throw new EmptyResultError('youtube feed');
117          }
118          return data;
119      },
120  });