Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bring back Video#is_live and add ExpandableMetadata #256

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/parser/classes/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import { YTNode } from '../helpers';
class Button extends YTNode {
static type = 'Button';

text: string;
text?: string;

label;
tooltip;
icon_type;
label?: string;
tooltip?: string;
icon_type?: string;
is_disabled?: boolean;

endpoint: NavigationEndpoint;

constructor(data: any) {
super();
this.text = new Text(data.text).toString();

if (data.text) {
this.text = new Text(data.text).toString();
}

if (data.accessibility?.label) {
this.label = data.accessibility?.label;
Expand All @@ -30,6 +34,10 @@ class Button extends YTNode {
this.icon_type = data.icon?.iconType;
}

if (Reflect.has(data, 'isDisabled')) {
this.is_disabled = data.isDisabled;
}

this.endpoint = new NavigationEndpoint(data.navigationEndpoint || data.serviceEndpoint || data.command);
}
}
Expand Down
41 changes: 41 additions & 0 deletions src/parser/classes/ExpandableMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Parser from '..';

import Text from './misc/Text';
import Thumbnail from './misc/Thumbnail';

import Button from './Button';
import HorizontalCardList from './HorizontalCardList';

import { YTNode } from '../helpers';

class ExpandableMetadata extends YTNode {
static type = 'ExpandableMetadata';

header: {
collapsed_title: Text;
collapsed_thumbnail: Thumbnail[];
collapsed_label: Text;
expanded_title: Text;
};

expanded_content: HorizontalCardList | null;
expand_button: Button | null;
collapse_button: Button | null;

constructor(data: any) {
super();

this.header = {
collapsed_title: new Text(data.header.collapsedTitle),
collapsed_thumbnail: Thumbnail.fromResponse(data.header.collapsedThumbnail),
collapsed_label: new Text(data.header.collapsedLabel),
expanded_title: new Text(data.header.expandedTitle)
};

this.expanded_content = Parser.parseItem<HorizontalCardList>(data.expandedContent);
this.expand_button = Parser.parseItem<Button>(data.expandButton);
this.collapse_button = Parser.parseItem<Button>(data.collapseButton);
}
}

export default ExpandableMetadata;
3 changes: 2 additions & 1 deletion src/parser/classes/HorizontalCardList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Parser from '../index';
import { YTNode } from '../helpers';
import SearchRefinementCard from './SearchRefinementCard';
import Button from './Button';
import MacroMarkersListItem from './MacroMarkersListItem';

class HorizontalCardList extends YTNode {
static type = 'HorizontalCardList';
Expand All @@ -13,7 +14,7 @@ class HorizontalCardList extends YTNode {

constructor(data: any) {
super();
this.cards = Parser.parseArray<SearchRefinementCard>(data.cards, SearchRefinementCard);
this.cards = Parser.parseArray<SearchRefinementCard | MacroMarkersListItem>(data.cards);
this.header = Parser.parseItem(data.header);
this.previous_button = Parser.parseItem<Button>(data.previousButton, Button);
this.next_button = Parser.parseItem<Button>(data.nextButton, Button);
Expand Down
29 changes: 29 additions & 0 deletions src/parser/classes/MacroMarkersListItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Text from './misc/Text';
import Thumbnail from './misc/Thumbnail';
import NavigationEndpoint from './NavigationEndpoint';

import { YTNode } from '../helpers';

class MacroMarkersListItem extends YTNode {
static type = 'MacroMarkersListItem';

title: Text;
time_description: Text;
thumbnail: Thumbnail[];
on_tap_endpoint: NavigationEndpoint;
layout: string;
is_highlighted: boolean;

constructor(data: any) {
super();

this.title = new Text(data.title);
this.time_description = new Text(data.timeDescription);
this.thumbnail = Thumbnail.fromResponse(data.thumbnail);
this.on_tap_endpoint = new NavigationEndpoint(data.onTap);
this.layout = data.layout;
this.is_highlighted = data.isHighlighted;
}
}

export default MacroMarkersListItem;
11 changes: 9 additions & 2 deletions src/parser/classes/MetadataBadge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class MetadataBadge extends YTNode {

icon_type?: string;
style?: string;
tooltip: string | null;
label?: string;
tooltip?: string;

constructor(data: any) {
super();
Expand All @@ -18,7 +19,13 @@ class MetadataBadge extends YTNode {
this.style = data.style;
}

this.tooltip = data?.tooltip || data?.iconTooltip || null;
if (data?.label) {
this.style = data.label;
}

if (data?.tooltip || data?.iconTooltip) {
this.tooltip = data.tooltip || data.iconTooltip;
}
}
}

Expand Down
37 changes: 27 additions & 10 deletions src/parser/classes/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Author from './misc/Author';
import Menu from './menus/Menu';
import Thumbnail from './misc/Thumbnail';
import NavigationEndpoint from './NavigationEndpoint';
import MetadataBadge from './MetadataBadge';
import ExpandableMetadata from './ExpandableMetadata';

import { timeToSeconds } from '../../utils/Utils';
import { YTNode } from '../helpers';
Expand All @@ -18,11 +20,13 @@ class Video extends YTNode {
text: Text;
hover_text: Text;
}[];
expandable_metadata: ExpandableMetadata | null;

thumbnails: Thumbnail[];
thumbnail_overlays;
rich_thumbnail;
author: Author;
badges: MetadataBadge[];
endpoint: NavigationEndpoint;
published: Text;
view_count: Text;
Expand All @@ -36,7 +40,8 @@ class Video extends YTNode {

show_action_menu: boolean;
is_watched: boolean;
menu;
menu: Menu | null;
search_video_result_entity_key: string;

constructor(data: any) {
super();
Expand All @@ -54,10 +59,13 @@ class Video extends YTNode {
hover_text: new Text(snippet.snippetHoverText)
})) || [];

this.expandable_metadata = Parser.parseItem<ExpandableMetadata>(data.expandableMetadata);

this.thumbnails = Thumbnail.fromResponse(data.thumbnail);
this.thumbnail_overlays = Parser.parseArray(data.thumbnailOverlays);
this.rich_thumbnail = data.richThumbnail ? Parser.parseItem(data.richThumbnail) : null;
this.author = new Author(data.ownerText, data.ownerBadges, data.channelThumbnailSupportedRenderers?.channelThumbnailWithLinkRenderer?.thumbnail);
this.badges = Parser.parseArray(data.badges, MetadataBadge);
this.endpoint = new NavigationEndpoint(data.navigationEndpoint);
this.published = new Text(data.publishedTimeText);
this.view_count = new Text(data.viewCountText);
Expand All @@ -76,6 +84,7 @@ class Video extends YTNode {
this.show_action_menu = data.showActionMenu;
this.is_watched = data.isWatched || false;
this.menu = Parser.parseItem<Menu>(data.menu, Menu);
this.search_video_result_entity_key = data.searchVideoResultEntityKey;
}

get description(): string {
Expand All @@ -85,22 +94,30 @@ class Video extends YTNode {
return this.description_snippet?.toString() || '';
}

/*
Get is_live() {
return this.badges.some((badge) => badge.style === 'BADGE_STYLE_TYPE_LIVE_NOW');
get is_live(): boolean {
return this.badges.some((badge) => {
if (badge.label === 'BADGE_STYLE_TYPE_LIVE_NOW' || badge.style === 'LIVE')
return true;
});
}
*/

get is_upcoming(): boolean | undefined {
return this.upcoming && this.upcoming > new Date();
}

/*
Get has_captions() {
return this.badges.some((badge) => badge.label === 'CC');
}*/
get is_premiere(): boolean {
return this.badges.some((badge) => badge.style === 'PREMIERE');
}

get is_4k(): boolean {
return this.badges.some((badge) => badge.style === '4K');
}

get has_captions(): boolean {
return this.badges.some((badge) => badge.style === 'CC');
}

get best_thumbnail(): Thumbnail | undefined{
get best_thumbnail(): Thumbnail | undefined {
return this.thumbnails[0];
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/parser/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { default as Endscreen } from './classes/Endscreen';
import { default as EndscreenElement } from './classes/EndscreenElement';
import { default as EndScreenPlaylist } from './classes/EndScreenPlaylist';
import { default as EndScreenVideo } from './classes/EndScreenVideo';
import { default as ExpandableMetadata } from './classes/ExpandableMetadata';
import { default as ExpandableTab } from './classes/ExpandableTab';
import { default as ExpandedShelfContents } from './classes/ExpandedShelfContents';
import { default as FeedFilterChipBar } from './classes/FeedFilterChipBar';
Expand Down Expand Up @@ -137,6 +138,7 @@ import { default as LiveChatItemList } from './classes/LiveChatItemList';
import { default as LiveChatMessageInput } from './classes/LiveChatMessageInput';
import { default as LiveChatParticipant } from './classes/LiveChatParticipant';
import { default as LiveChatParticipantsList } from './classes/LiveChatParticipantsList';
import { default as MacroMarkersListItem } from './classes/MacroMarkersListItem';
import { default as Menu } from './classes/menus/Menu';
import { default as MenuNavigationItem } from './classes/menus/MenuNavigationItem';
import { default as MenuServiceItem } from './classes/menus/MenuServiceItem';
Expand Down Expand Up @@ -363,6 +365,7 @@ export const YTNodes = {
EndscreenElement,
EndScreenPlaylist,
EndScreenVideo,
ExpandableMetadata,
ExpandableTab,
ExpandedShelfContents,
FeedFilterChipBar,
Expand Down Expand Up @@ -427,6 +430,7 @@ export const YTNodes = {
LiveChatMessageInput,
LiveChatParticipant,
LiveChatParticipantsList,
MacroMarkersListItem,
Menu,
MenuNavigationItem,
MenuServiceItem,
Expand Down
4 changes: 2 additions & 2 deletions src/parser/youtube/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Search extends Feed {

if (typeof card === 'string') {
if (!this.refinement_cards) throw new InnertubeError('No refinement cards found.');
target_card = this.refinement_cards?.cards.get({ query: card });
target_card = this.refinement_cards?.cards.get({ query: card })?.as(SearchRefinementCard);
if (!target_card)
throw new InnertubeError(`Refinement card "${card}" not found`, { available_cards: this.refinement_card_queries });
} else if (card.type === 'SearchRefinementCard') {
Expand All @@ -58,7 +58,7 @@ export default class Search extends Feed {
* Returns a list of refinement card queries.
*/
get refinement_card_queries() {
return this.refinement_cards?.cards.map((card) => card.query);
return this.refinement_cards?.cards.as(SearchRefinementCard).map((card) => card.query);
}

/**
Expand Down