mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-23 23:09:28 +00:00
feat(Parser): just-in-time YTNode generation (#310)
* refactor: merge NavigatableText into Text * fix(Text): data might not be object * refactor: remove GetParserByName from map * feat(Parser): just-in-time YTNode generation * refactor: cleanup YTNodeGenerator * fix: YTNode map imports * feat(YTNodeGenerator): primative types Add support for inferring primatives types * fix(YTNodeGenerator): NavigationEndpoint detection * fix(YTNodeGenerator): fix generated typescript Correct types and linting for generated typescript class * chore: update parsers after merge * feat: add support for object type inference * fix: object type def * docs: basic YTNodeGenerator explanation * docs: tsdoc for YTNodeGenerator * docs: update parser updating guide * fix: apply suggested changes * docs: accessing generated nodes
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import Parser from '../../index.js';
|
||||
import NavigatableText from './NavigatableText.js';
|
||||
import NavigationEndpoint from '../NavigationEndpoint.js';
|
||||
import TextRun from './TextRun.js';
|
||||
import Thumbnail from './Thumbnail.js';
|
||||
import Constants from '../../../utils/Constants.js';
|
||||
import Text from './Text.js';
|
||||
|
||||
class Author {
|
||||
#nav_text;
|
||||
@@ -11,14 +11,14 @@ class Author {
|
||||
id: string;
|
||||
name: string;
|
||||
thumbnails: Thumbnail[];
|
||||
endpoint: NavigationEndpoint | null;
|
||||
endpoint?: NavigationEndpoint;
|
||||
badges?: any;
|
||||
is_verified?: boolean | null;
|
||||
is_verified_artist?: boolean | null;
|
||||
url: string | null;
|
||||
|
||||
constructor(item: any, badges?: any, thumbs?: any) {
|
||||
this.#nav_text = new NavigatableText(item);
|
||||
this.#nav_text = new Text(item);
|
||||
|
||||
this.id =
|
||||
(this.#nav_text.runs?.[0] as TextRun)?.endpoint?.payload?.browseId ||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import Text from './Text.js';
|
||||
import NavigationEndpoint from '../NavigationEndpoint.js';
|
||||
import type { RawNode } from '../../index.js';
|
||||
|
||||
class NavigatableText extends Text {
|
||||
static type = 'NavigatableText';
|
||||
|
||||
endpoint: NavigationEndpoint | null;
|
||||
|
||||
constructor(node: RawNode) {
|
||||
super(node);
|
||||
// TODO: is this needed? Text now supports this itself
|
||||
this.endpoint =
|
||||
node?.runs?.[0]?.navigationEndpoint ?
|
||||
new NavigationEndpoint(node?.runs[0].navigationEndpoint) :
|
||||
node?.navigationEndpoint ?
|
||||
new NavigationEndpoint(node?.navigationEndpoint) :
|
||||
node?.titleNavigationEndpoint ?
|
||||
new NavigationEndpoint(node?.titleNavigationEndpoint) : null;
|
||||
}
|
||||
|
||||
toJSON(): NavigatableText {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export default NavigatableText;
|
||||
@@ -1,5 +1,6 @@
|
||||
import TextRun from './TextRun.js';
|
||||
import EmojiRun from './EmojiRun.js';
|
||||
import NavigationEndpoint from '../NavigationEndpoint.js';
|
||||
import type { RawNode } from '../../index.js';
|
||||
|
||||
export interface Run {
|
||||
@@ -18,8 +19,9 @@ export function escape(text: string) {
|
||||
}
|
||||
|
||||
class Text {
|
||||
text: string;
|
||||
text?: string;
|
||||
runs;
|
||||
endpoint?: NavigationEndpoint;
|
||||
|
||||
constructor(data: RawNode) {
|
||||
if (data?.hasOwnProperty('runs') && Array.isArray(data.runs)) {
|
||||
@@ -29,16 +31,28 @@ class Text {
|
||||
);
|
||||
this.text = this.runs.map((run) => run.text).join('');
|
||||
} else {
|
||||
this.text = data?.simpleText || 'N/A';
|
||||
this.text = data?.simpleText;
|
||||
}
|
||||
if (typeof data === 'object' && data !== null && Reflect.has(data, 'navigationEndpoint')) {
|
||||
this.endpoint = new NavigationEndpoint(data.navigationEndpoint);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
toHTML() {
|
||||
return this.runs ? this.runs.map((run) => run.toHTML()).join('') : this.text;
|
||||
}
|
||||
|
||||
isEmpty() {
|
||||
return this.text === undefined;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.text;
|
||||
return this.text || 'N/A';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user