Files
YouTube.js/lib/core/Feed.js
Daniel Wykerd 4a102878d8 refactor!: feature complete contents parser
* feat: allow setting search params to custom value

This is useful for getting results other than videos, like playlists and
channels.

* feat: add initial parsers for common renderers

* feat: artist search renderers

Added common renderers used when searching artists

* refactor: snake_case

* feat: channel home page renderers

* feat: parsers for more channel tabs

These are needed for channel tabs: Videos, Playlists, Community,
Channels

Additionally, do not merely return text as string, since they may
include links which may be navigated to

* feat: channel full metadata

* feat: renderers for playlists

* refactor!: Actions.browse

Channels should be viewable when not logged in, also added 'navigation'
type for use in NagivationEndpoint in the future.

* feat: home feed parsers

* feat: watch page renderers

* feat: start implementing HomeFeed API

The HomeFeed class remains compatible with the existing API

* feat: generate types using tsc and jsdoc

* feat: browse continuations from navigationEndpoint

* fix: Actions moved to session

This follows commit 1bfe2676d8

* fix: add more typescript config

* chore: use correct spaces and quotes

* feat: Trending API

* feat: reimplement existing channel API

* feat: add base video feed class

* feat: get channel videos

* feat: channel playlists

* feat: get channel community posts

* feat: get channels from channel

* feat: get channel about page data

* feat: add missing channel parsers

this commit also adds regenerated types I've neglected to push

* feat: initial playlist reimplementation

* feat: complete playlist reimplementation

* refactor: change InnertubeError to ES6 class

* fix: some unresolved types

* chore: update types

* feat: wip video details

* feat: get music tracks in video

Possibly an implementation for issue #48

* refactor:  merge parsers (wip)

This is a work in progress.

* fix: add pnpm to ignore

* fix: merge issues

* fix: merge Video and VideoInfo

VideoInfo should be working again.
Also remove the old parsers.

* feat: set matching in Simplify

Still looking into removing Simplify

* fix: ContinuationItem

This `call` method allows for traversal of continuations with the Simplify API
but may be removed in the future

* fix: optionally returned data

* revert: replace ContinuationItem with main

* feat(parser): contents memoization by classname

* feat(channel): working without Simplify

* feat(feed): working continuations

* fix: liniting issues

* feat(feed): filterable feed for home

* feat(feed): tabbed feed for trending & channel

* refactor: remove Simplify completely

* chore: lint

* refactor: alias `items` with `contents`

* refactor: `Search` to extend `Feed`

* fix: Search working

Also added MenuServiceItemDownload

* refactor: move `Channel` and `Playlist`

* fix: pass all tests

* fix: linting errors
2022-06-15 18:31:34 -03:00

154 lines
4.9 KiB
JavaScript

const ResultsParser = require('../parser/contents');
const { InnertubeError } = require('../utils/Utils');
// TODO: add a way subdivide into sections and return subfeeds?
class Feed {
#page;
/**
* @type {import('../parser/contents/classes/ContinuationItem')[]}
*/
#continuation;
/**
* @type {import('../core/Actions')}
*/
#actions;
memo;
constructor(actions, data, already_parsed = false) {
if (data.on_response_received_actions || data.on_response_received_endpoints || already_parsed)
this.#page = data;
else
this.#page = ResultsParser.parseResponse(data);
this.memo =
this.#page.on_response_received_actions ?
this.#page.on_response_received_actions_memo :
this.#page.on_response_received_endpoints ?
this.#page.on_response_received_endpoints_memo :
this.#page.contents_memo;
this.#actions = actions;
}
/**
* Get the original page data
*/
get page() {
return this.#page;
}
get actions() {
return this.#actions;
}
/**
* Get all videos on a given page via memo
*
* @param {Map<string, any[]>} memo
* @returns {Array<import('../parser/contents/classes/Video') | import('../parser/contents/classes/GridVideo') | import('../parser/contents/classes/CompactVideo') | import('../parser/contents/classes/PlaylistVideo') | import('../parser/contents/classes/PlaylistPanelVideo') | import('../parser/contents/classes/WatchCardCompactVideo')>}
*/
static getVideosFromMemo(memo) {
const videos = memo.get('Video') || [];
const grid_videos = memo.get('GridVideo') || [];
const compact_videos = memo.get('CompactVideo') || [];
const playlist_videos = memo.get('PlaylistVideo') || [];
const playlist_panel_videos = memo.get('PlaylistPanelVideo') || [];
const watch_card_compact_videos = memo.get('WatchCardCompactVideo') || [];
return [...videos, ...grid_videos, ...compact_videos, ...playlist_videos, ...playlist_panel_videos, ...watch_card_compact_videos];
}
/**
* Get all playlists on a given page via memo
*
* @param {Map<string, any[]>} memo
* @returns {Array<import('../parser/contents/classes/Playlist') | import('../parser/contents/classes/GridPlaylist')>}
*/
static getPlaylistsFromMemo(memo) {
const playlists = memo.get('Playlist') || [];
const grid_playlists = memo.get('GridPlaylist') || [];
return [...playlists, ...grid_playlists];
}
/**
* Get all the videos in the feed
*/
get videos() {
return Feed.getVideosFromMemo(this.memo);
}
/**
* Get all playlists in the feed
*
* @returns {Array<import('../parser/contents/classes/Playlist') | import('../parser/contents/classes/GridPlaylist')>}
*/
get playlists() {
return Feed.getPlaylistsFromPage(this.memo);
}
/**
* Get all the community posts in the feed
*
* @returns {import('../parser/contents/classes/BackstagePost')[]}
*/
get backstage_posts() {
return this.memo.get('BackstagePost');
}
/**
* Get all the channels in the feed
*
* @returns {Array<import('../parser/contents/Channel') | import('../parser/contents/GridChannel')>}
*/
get channels() {
const channels = this.memo.get('Channel') || [];
const grid_channels = this.memo.get('GridChannel') || [];
return [...channels, ...grid_channels];
}
get has_continuation() {
return (this.memo.get('ContinuationItem') || []).length > 0;
}
async getContinuationData() {
if (this.#continuation) {
if (this.#continuation.length > 1)
throw new InnertubeError('There are too many continuations, you\'ll need to find the correct one yourself in this.page');
if (this.#continuation.length === 0)
throw new InnertubeError('There are no continuations');
const continuation = this.#continuation[0];
return await continuation.endpoint.call(this.#actions);
}
this.#continuation = this.memo.get('ContinuationItem');
if (this.#continuation)
return this.getContinuationData();
return null;
}
get shelves() {
return this.#page.contents_memo.get('Shelf');
}
getShelf(title) {
return this.shelves.find(shelf => shelf.title.toString() === title);
}
get shelf_content() {
return this.shelves.map(shelf => ({
title: shelf.title.toString(),
content: shelf.content.contents,
}));
}
async getContinuation() {
const continuation_data = await this.getContinuationData();
return new Feed(this.actions, continuation_data, true);
}
}
module.exports = Feed;