From 8ef4b42d444c4fbe5cd65a55c0e0e7aa31738755 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Wed, 15 Mar 2023 05:15:16 -0300 Subject: [PATCH] feat(parser): add `GridShow` and `ShowCustomThumbnail` Closes #459 --- src/parser/classes/GridShow.ts | 29 +++++++++++++++++++ src/parser/classes/ShowCustomThumbnail.ts | 14 +++++++++ .../classes/ThumbnailOverlayBottomPanel.ts | 15 ++++++++-- src/parser/classes/misc/Text.ts | 13 +++++---- src/parser/nodes.ts | 2 ++ 5 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 src/parser/classes/GridShow.ts create mode 100644 src/parser/classes/ShowCustomThumbnail.ts diff --git a/src/parser/classes/GridShow.ts b/src/parser/classes/GridShow.ts new file mode 100644 index 000000000..0169b1c76 --- /dev/null +++ b/src/parser/classes/GridShow.ts @@ -0,0 +1,29 @@ +import { ObservedArray, YTNode } from '../helpers.js'; +import { RawNode } from '../index.js'; +import Parser from '../parser.js'; +import Author from './misc/Author.js'; +import Text from './misc/Text.js'; +import NavigationEndpoint from './NavigationEndpoint.js'; +import ShowCustomThumbnail from './ShowCustomThumbnail.js'; +import ThumbnailOverlayBottomPanel from './ThumbnailOverlayBottomPanel.js'; + +export default class GridShow extends YTNode { + static type = 'GridShow'; + + title: Text; + thumbnail_renderer: ShowCustomThumbnail | null; + endpoint: NavigationEndpoint; + long_byline_text: Text; + thumbnail_overlays: ObservedArray | null; + author: Author; + + constructor(data: RawNode) { + super(); + this.title = new Text(data.title); + this.thumbnail_renderer = Parser.parseItem(data.thumbnailRenderer, ShowCustomThumbnail); + this.endpoint = new NavigationEndpoint(data.navigationEndpoint); + this.long_byline_text = new Text(data.longBylineText); + this.thumbnail_overlays = Parser.parseArray(data.thumbnailOverlays, ThumbnailOverlayBottomPanel); + this.author = new Author(data.shortBylineText, undefined); + } +} \ No newline at end of file diff --git a/src/parser/classes/ShowCustomThumbnail.ts b/src/parser/classes/ShowCustomThumbnail.ts new file mode 100644 index 000000000..0ed946a79 --- /dev/null +++ b/src/parser/classes/ShowCustomThumbnail.ts @@ -0,0 +1,14 @@ +import { YTNode } from '../helpers.js'; +import { RawNode } from '../index.js'; +import Thumbnail from './misc/Thumbnail.js'; + +export default class ShowCustomThumbnail extends YTNode { + static type = 'ShowCustomThumbnail'; + + thumbnail: Thumbnail[]; + + constructor(data: RawNode) { + super(); + this.thumbnail = Thumbnail.fromResponse(data.thumbnail); + } +} \ No newline at end of file diff --git a/src/parser/classes/ThumbnailOverlayBottomPanel.ts b/src/parser/classes/ThumbnailOverlayBottomPanel.ts index 6c0cd1155..41ea727fd 100644 --- a/src/parser/classes/ThumbnailOverlayBottomPanel.ts +++ b/src/parser/classes/ThumbnailOverlayBottomPanel.ts @@ -1,13 +1,22 @@ import { YTNode } from '../helpers.js'; +import { RawNode } from '../index.js'; +import Text from './misc/Text.js'; class ThumbnailOverlayBottomPanel extends YTNode { static type = 'ThumbnailOverlayBottomPanel'; - icon_type: string; + text?: Text; + icon_type?: string; - constructor(data: any) { + constructor(data: RawNode) { super(); - this.icon_type = data.icon.iconType; + if (Reflect.has(data, 'text')) { + this.text = new Text(data.text); + } + + if (Reflect.has(data, 'icon') && Reflect.has(data.icon, 'iconType')) { + this.icon_type = data.icon.iconType; + } } } diff --git a/src/parser/classes/misc/Text.ts b/src/parser/classes/misc/Text.ts index 282484950..0c8d55200 100644 --- a/src/parser/classes/misc/Text.ts +++ b/src/parser/classes/misc/Text.ts @@ -18,7 +18,7 @@ export function escape(text: string) { .replace(/'/g, '''); } -class Text { +export default class Text { text?: string; runs; endpoint?: NavigationEndpoint; @@ -39,8 +39,11 @@ class Text { if (typeof data === 'object' && data !== null && Reflect.has(data, 'titleNavigationEndpoint')) { this.endpoint = new NavigationEndpoint(data.titleNavigationEndpoint); } - if (!this.endpoint) - this.endpoint = (this.runs?.[0] as TextRun)?.endpoint; + if (!this.endpoint) { + if ((this.runs?.[0] as TextRun)?.endpoint) { + this.endpoint = (this.runs?.[0] as TextRun)?.endpoint; + } + } } toHTML() { @@ -54,6 +57,4 @@ class Text { toString() { return this.text || 'N/A'; } -} - -export default Text; \ No newline at end of file +} \ No newline at end of file diff --git a/src/parser/nodes.ts b/src/parser/nodes.ts index 053d4b00e..eae6286a8 100644 --- a/src/parser/nodes.ts +++ b/src/parser/nodes.ts @@ -100,6 +100,7 @@ export { default as GridHeader } from './classes/GridHeader.js'; export { default as GridMix } from './classes/GridMix.js'; export { default as GridMovie } from './classes/GridMovie.js'; export { default as GridPlaylist } from './classes/GridPlaylist.js'; +export { default as GridShow } from './classes/GridShow.js'; export { default as GridVideo } from './classes/GridVideo.js'; export { default as GuideCollapsibleEntry } from './classes/GuideCollapsibleEntry.js'; export { default as GuideCollapsibleSectionEntry } from './classes/GuideCollapsibleSectionEntry.js'; @@ -283,6 +284,7 @@ export { default as SettingsSidebar } from './classes/SettingsSidebar.js'; export { default as SettingsSwitch } from './classes/SettingsSwitch.js'; export { default as SharedPost } from './classes/SharedPost.js'; export { default as Shelf } from './classes/Shelf.js'; +export { default as ShowCustomThumbnail } from './classes/ShowCustomThumbnail.js'; export { default as ShowingResultsFor } from './classes/ShowingResultsFor.js'; export { default as SimpleCardContent } from './classes/SimpleCardContent.js'; export { default as SimpleCardTeaser } from './classes/SimpleCardTeaser.js';