mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-27 16:48:55 +00:00
feat(VideoInfo): support get by endpoint + more info (#342)
* feat(VideoInfo): get by endpoint + more info * chore: fix param description for `getInfo()`
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
import Parser from '../index.js';
|
||||
import { YTNode } from '../helpers.js';
|
||||
import Text from './misc/Text.js';
|
||||
import PlaylistAuthor from './misc/PlaylistAuthor.js';
|
||||
import NavigationEndpoint from './NavigationEndpoint.js';
|
||||
|
||||
import type Menu from './menus/Menu.js';
|
||||
|
||||
type AutoplaySet = {
|
||||
autoplay_video: NavigationEndpoint,
|
||||
next_button_video?: NavigationEndpoint
|
||||
};
|
||||
|
||||
class TwoColumnWatchNextResults extends YTNode {
|
||||
static type = 'TwoColumnWatchNextResults';
|
||||
@@ -7,12 +17,66 @@ class TwoColumnWatchNextResults extends YTNode {
|
||||
results;
|
||||
secondary_results;
|
||||
conversation_bar;
|
||||
playlist?: {
|
||||
id: string,
|
||||
title: string,
|
||||
author: Text | PlaylistAuthor,
|
||||
contents: YTNode[],
|
||||
current_index: number,
|
||||
is_infinite: boolean,
|
||||
menu: Menu | null
|
||||
};
|
||||
autoplay?: {
|
||||
sets: AutoplaySet[],
|
||||
modified_sets?: AutoplaySet[],
|
||||
count_down_secs?: number
|
||||
};
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this.results = Parser.parseArray(data.results?.results.contents);
|
||||
this.secondary_results = Parser.parseArray(data.secondaryResults?.secondaryResults.results);
|
||||
this.conversation_bar = Parser.parseItem(data?.conversationBar);
|
||||
|
||||
const playlistData = data.playlist?.playlist;
|
||||
if (playlistData) {
|
||||
this.playlist = {
|
||||
id: playlistData.playlistId,
|
||||
title: playlistData.title,
|
||||
author: playlistData.shortBylineText?.simpleText ?
|
||||
new Text(playlistData.shortBylineText) :
|
||||
new PlaylistAuthor(playlistData.longBylineText),
|
||||
contents: Parser.parseArray(playlistData.contents),
|
||||
current_index: playlistData.currentIndex,
|
||||
is_infinite: !!playlistData.isInfinite,
|
||||
menu: Parser.parseItem<Menu>(playlistData.menu)
|
||||
};
|
||||
}
|
||||
|
||||
const autoplayData = data.autoplay?.autoplay;
|
||||
if (autoplayData) {
|
||||
this.autoplay = {
|
||||
sets: autoplayData.sets.map((set: any) => this.#parseAutoplaySet(set))
|
||||
};
|
||||
if (autoplayData.modifiedSets) {
|
||||
this.autoplay.modified_sets = autoplayData.modifiedSets.map((set: any) => this.#parseAutoplaySet(set));
|
||||
}
|
||||
if (autoplayData.countDownSecs) {
|
||||
this.autoplay.count_down_secs = autoplayData.countDownSecs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#parseAutoplaySet(data: any): AutoplaySet {
|
||||
const result = {
|
||||
autoplay_video: new NavigationEndpoint(data.autoplayVideo)
|
||||
} as AutoplaySet;
|
||||
|
||||
if (data.nextButtonVideo) {
|
||||
result.next_button_video = new NavigationEndpoint(data.nextButtonVideo);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import TwoColumnWatchNextResults from '../classes/TwoColumnWatchNextResults.js';
|
||||
import VideoPrimaryInfo from '../classes/VideoPrimaryInfo.js';
|
||||
import VideoSecondaryInfo from '../classes/VideoSecondaryInfo.js';
|
||||
import LiveChatWrap from './LiveChat.js';
|
||||
import NavigationEndpoint from '../classes/NavigationEndpoint.js';
|
||||
|
||||
import type CardCollection from '../classes/CardCollection.js';
|
||||
import type Endscreen from '../classes/Endscreen.js';
|
||||
@@ -60,6 +61,7 @@ class VideoInfo {
|
||||
|
||||
primary_info?: VideoPrimaryInfo | null;
|
||||
secondary_info?: VideoSecondaryInfo | null;
|
||||
playlist?;
|
||||
game_info?;
|
||||
merchandise?: MerchandiseShelf | null;
|
||||
related_chip_cloud?: ChipCloud | null;
|
||||
@@ -67,6 +69,7 @@ class VideoInfo {
|
||||
player_overlays?: PlayerOverlay | null;
|
||||
comments_entry_point_header?: CommentsEntryPointHeader | null;
|
||||
livechat?: LiveChat | null;
|
||||
autoplay?;
|
||||
|
||||
/**
|
||||
* @param data - API response.
|
||||
@@ -141,6 +144,10 @@ class VideoInfo {
|
||||
this.merchandise = results.firstOfType(MerchandiseShelf);
|
||||
this.related_chip_cloud = secondary_results.firstOfType(RelatedChipCloud)?.content.item().as(ChipCloud);
|
||||
|
||||
if (two_col?.playlist) {
|
||||
this.playlist = two_col.playlist;
|
||||
}
|
||||
|
||||
this.watch_next_feed = secondary_results.firstOfType(ItemSection)?.contents || secondary_results;
|
||||
|
||||
if (this.watch_next_feed && Array.isArray(this.watch_next_feed) && this.watch_next_feed.at(-1)?.is(ContinuationItem))
|
||||
@@ -148,6 +155,10 @@ class VideoInfo {
|
||||
|
||||
this.player_overlays = next?.player_overlays?.item().as(PlayerOverlay);
|
||||
|
||||
if (two_col?.autoplay) {
|
||||
this.autoplay = two_col.autoplay;
|
||||
}
|
||||
|
||||
const segmented_like_dislike_button = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButton);
|
||||
|
||||
if (segmented_like_dislike_button?.like_button?.is(ToggleButton) && segmented_like_dislike_button?.dislike_button?.is(ToggleButton)) {
|
||||
@@ -377,6 +388,13 @@ class VideoInfo {
|
||||
return !!this.#watch_next_continuation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the endpoint of the autoplay video
|
||||
*/
|
||||
get autoplay_video_endpoint(): NavigationEndpoint | null {
|
||||
return this.autoplay?.sets?.[0]?.autoplay_video || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get songs used in the video.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user