feat(parser): add GridShow and ShowCustomThumbnail

Closes #459
This commit is contained in:
LuanRT
2023-03-15 05:15:16 -03:00
parent b71f03caf2
commit 8ef4b42d44
5 changed files with 64 additions and 9 deletions

View File

@@ -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<ThumbnailOverlayBottomPanel> | 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);
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}
}

View File

@@ -18,7 +18,7 @@ export function escape(text: string) {
.replace(/'/g, '&#039;');
}
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;
}