mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-07-04 12:47:04 +00:00
refactor: misc fixes and additions (#142)
* feat: add `header` to `Grid` parser * feat: parse title in `MusicHeader` * feat: improve parsing of artist-type items
This commit is contained in:
@@ -11,6 +11,10 @@ class Grid extends YTNode {
|
|||||||
this.visible_row_count = data.visibleRowCount;
|
this.visible_row_count = data.visibleRowCount;
|
||||||
this.target_id = data.targetId;
|
this.target_id = data.targetId;
|
||||||
this.continuation = data.continuations?.[0]?.nextContinuationData?.continuation || null;
|
this.continuation = data.continuations?.[0]?.nextContinuationData?.continuation || null;
|
||||||
|
|
||||||
|
if (data.header) {
|
||||||
|
this.header = Parser.parse(data.header);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: alias for consistency
|
// XXX: alias for consistency
|
||||||
|
|||||||
15
src/parser/classes/GridHeader.ts
Normal file
15
src/parser/classes/GridHeader.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import Text from './misc/Text';
|
||||||
|
import { YTNode } from '../helpers';
|
||||||
|
|
||||||
|
class GridHeader extends YTNode {
|
||||||
|
static type = 'GridHeader';
|
||||||
|
|
||||||
|
title: Text;
|
||||||
|
|
||||||
|
constructor(data: any) {
|
||||||
|
super();
|
||||||
|
this.title = new Text(data.title);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default GridHeader;
|
||||||
@@ -1,12 +1,20 @@
|
|||||||
import Parser from '../index';
|
import Parser from '../index';
|
||||||
import { YTNode } from '../helpers';
|
import { YTNode } from '../helpers';
|
||||||
|
import Text from './misc/Text';
|
||||||
|
|
||||||
class MusicHeader extends YTNode {
|
class MusicHeader extends YTNode {
|
||||||
static type = 'MusicHeader';
|
static type = 'MusicHeader';
|
||||||
|
|
||||||
constructor(data) {
|
constructor(data) {
|
||||||
super();
|
super();
|
||||||
this.header = Parser.parse(data.header);
|
|
||||||
|
if (data.header) {
|
||||||
|
this.header = Parser.parse(data.header);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.title) {
|
||||||
|
this.title = new Text(data.title);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,9 @@ class MusicResponsiveListItem extends YTNode {
|
|||||||
}[];
|
}[];
|
||||||
|
|
||||||
name?: string;
|
name?: string;
|
||||||
|
subtitle?: Text;
|
||||||
subscribers?: string;
|
subscribers?: string;
|
||||||
|
song_count?: string;
|
||||||
// TODO: these might be replaceable with Author class
|
// TODO: these might be replaceable with Author class
|
||||||
author?: {
|
author?: {
|
||||||
name: string,
|
name: string,
|
||||||
@@ -186,7 +188,9 @@ class MusicResponsiveListItem extends YTNode {
|
|||||||
#parseArtist() {
|
#parseArtist() {
|
||||||
this.id = this.endpoint?.browse?.id;
|
this.id = this.endpoint?.browse?.id;
|
||||||
this.name = this.#flex_columns[0].key('title').instanceof(Text).toString();
|
this.name = this.#flex_columns[0].key('title').instanceof(Text).toString();
|
||||||
this.subscribers = this.#flex_columns[1].key('title').instanceof(Text).runs?.[2]?.text || '';
|
this.subtitle = this.#flex_columns[1].key('title').instanceof(Text);
|
||||||
|
this.subscribers = this.subtitle.runs?.find((run) => (/^(\d*\.)?\d+[M|K]? subscribers?$/i).test(run.text))?.text || '';
|
||||||
|
this.song_count = this.subtitle.runs?.find((run) => (/^\d+(,\d+)? songs?$/i).test(run.text))?.text || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
#parseAlbum() {
|
#parseAlbum() {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class MusicTwoRowItem extends YTNode {
|
|||||||
switch (this.endpoint?.browse?.page_type) {
|
switch (this.endpoint?.browse?.page_type) {
|
||||||
case 'MUSIC_PAGE_TYPE_ARTIST':
|
case 'MUSIC_PAGE_TYPE_ARTIST':
|
||||||
this.item_type = 'artist';
|
this.item_type = 'artist';
|
||||||
this.subscribers = this.subtitle.toString();
|
this.subscribers = this.subtitle.runs?.find((run) => (/^(\d*\.)?\d+[M|K]? subscribers?$/i).test(run.text))?.text || '';
|
||||||
break;
|
break;
|
||||||
case 'MUSIC_PAGE_TYPE_PLAYLIST':
|
case 'MUSIC_PAGE_TYPE_PLAYLIST':
|
||||||
this.item_type = 'playlist';
|
this.item_type = 'playlist';
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ import { default as FeedFilterChipBar } from './classes/FeedFilterChipBar';
|
|||||||
import { default as FeedTabbedHeader } from './classes/FeedTabbedHeader';
|
import { default as FeedTabbedHeader } from './classes/FeedTabbedHeader';
|
||||||
import { default as Grid } from './classes/Grid';
|
import { default as Grid } from './classes/Grid';
|
||||||
import { default as GridChannel } from './classes/GridChannel';
|
import { default as GridChannel } from './classes/GridChannel';
|
||||||
|
import { default as GridHeader } from './classes/GridHeader';
|
||||||
import { default as GridPlaylist } from './classes/GridPlaylist';
|
import { default as GridPlaylist } from './classes/GridPlaylist';
|
||||||
import { default as GridVideo } from './classes/GridVideo';
|
import { default as GridVideo } from './classes/GridVideo';
|
||||||
import { default as HistorySuggestion } from './classes/HistorySuggestion';
|
import { default as HistorySuggestion } from './classes/HistorySuggestion';
|
||||||
@@ -303,6 +304,7 @@ const map: Record<string, YTNodeConstructor> = {
|
|||||||
FeedTabbedHeader,
|
FeedTabbedHeader,
|
||||||
Grid,
|
Grid,
|
||||||
GridChannel,
|
GridChannel,
|
||||||
|
GridHeader,
|
||||||
GridPlaylist,
|
GridPlaylist,
|
||||||
GridVideo,
|
GridVideo,
|
||||||
HistorySuggestion,
|
HistorySuggestion,
|
||||||
|
|||||||
Reference in New Issue
Block a user