mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-07-03 09:35:05 +00:00
feat: tidy things up and implement more renderers
- Finished Library parser - Fixed search continuations - Improved channel parser - Improved playlist parser - Added support for posts of type poll - Improved History parser - Removed redundant code
This commit is contained in:
162
lib/core/Feed.js
162
lib/core/Feed.js
@@ -13,8 +13,8 @@ class Feed {
|
||||
|
||||
/** @type {import('../core/Actions')} */
|
||||
#actions;
|
||||
|
||||
memo;
|
||||
|
||||
#memo;
|
||||
|
||||
constructor(actions, data, already_parsed = false) {
|
||||
if (data.on_response_received_actions || data.on_response_received_endpoints || already_parsed) {
|
||||
@@ -23,30 +23,23 @@ class Feed {
|
||||
this.#page = ResultsParser.parseResponse(data);
|
||||
}
|
||||
|
||||
this.memo =
|
||||
this.#memo =
|
||||
|
||||
this.#page.on_response_received_commands ?
|
||||
this.#page.on_response_received_commands_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.#page.contents ?
|
||||
this.#page.contents_memo:
|
||||
|
||||
this.#page.on_response_received_actions ?
|
||||
this.#page.on_response_received_actions_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
|
||||
*
|
||||
@@ -87,25 +80,16 @@ class Feed {
|
||||
* 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);
|
||||
return Feed.getVideosFromMemo(this.#memo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the community posts in the feed
|
||||
*
|
||||
* @returns {import('../parser/contents/classes/BackstagePost')[]}
|
||||
* @returns {import('../parser/contents/classes/BackstagePost')[] | import('../parser/contents/classes/Post')[]}
|
||||
*/
|
||||
get backstage_posts() {
|
||||
return this.memo.get('BackstagePost');
|
||||
get posts() {
|
||||
return this.#memo.get('BackstagePost') || this.#memo.get('Post') || [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,15 +98,93 @@ class 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') || [];
|
||||
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;
|
||||
|
||||
/**
|
||||
* Get all playlists in the feed
|
||||
*
|
||||
* @returns {Array<import('../parser/contents/classes/Playlist') | import('../parser/contents/classes/GridPlaylist')>}
|
||||
*/
|
||||
get playlists() {
|
||||
return Feed.getPlaylistsFromMemo(this.#memo);
|
||||
}
|
||||
|
||||
|
||||
get memo() {
|
||||
return this.#memo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns contents from the page.
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
get contents() {
|
||||
const tab_content = this.#memo.get('Tab')?.[0]?.content;
|
||||
const reload_continuation_items = this.#memo.get('reloadContinuationItemsCommand')?.[0];
|
||||
const append_continuation_items = this.#memo.get('appendContinuationItemsAction')?.[0];
|
||||
|
||||
return tab_content || reload_continuation_items || append_continuation_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all segments/sections from the page.
|
||||
*
|
||||
* @returns {import('../parser/contents/Shelf')[] | import('../parser/contents/RichShelf')[] | import('../parser/contents/ReelShelf')[]}
|
||||
*/
|
||||
get shelves() {
|
||||
const shelf = this.#page.contents_memo.get('Shelf') || [];
|
||||
const rich_shelf = this.#page.contents_memo.get('RichShelf') || [];
|
||||
const reel_shelf = this.#page.contents_memo.get('ReelShelf') || [];
|
||||
|
||||
return [ ...shelf, ...rich_shelf, ...reel_shelf ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds shelf by title.
|
||||
*
|
||||
* @param {string} title
|
||||
*/
|
||||
getShelf(title) {
|
||||
return this.shelves.find(shelf => shelf.title.toString() === title);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns secondary contents from the page.
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
get secondary_contents() {
|
||||
return this.page.contents?.secondary_contents;
|
||||
}
|
||||
|
||||
get actions() {
|
||||
return this.#actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the original page data
|
||||
*/
|
||||
get page() {
|
||||
return this.#page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the feed has continuation.
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
get has_continuation() {
|
||||
return (this.#memo.get('ContinuationItem') || []).length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves continuation data as it is.
|
||||
*
|
||||
* @returns {Promise.<any>}
|
||||
*/
|
||||
async getContinuationData() {
|
||||
if (this.#continuation) {
|
||||
if (this.#continuation.length > 1)
|
||||
@@ -135,29 +197,19 @@ class Feed {
|
||||
return response;
|
||||
}
|
||||
|
||||
this.#continuation = this.memo.get('ContinuationItem');
|
||||
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,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves next batch of contents and returns a new {@link Feed} object.
|
||||
*
|
||||
* @returns {Promise.<Feed>}
|
||||
*/
|
||||
async getContinuation() {
|
||||
const continuation_data = await this.getContinuationData();
|
||||
return new Feed(this.actions, continuation_data, true);
|
||||
|
||||
Reference in New Issue
Block a user