From 31326ec9eb2e4cf5f07e216f68475fb380599c88 Mon Sep 17 00:00:00 2001 From: Patrick Kan <55383971+patrickkfkan@users.noreply.github.com> Date: Thu, 18 Aug 2022 16:35:24 +0800 Subject: [PATCH] refactor: misc fixes and additions (#142) * feat: add `header` to `Grid` parser * feat: parse title in `MusicHeader` * feat: improve parsing of artist-type items --- src/parser/classes/Grid.js | 4 ++++ src/parser/classes/GridHeader.ts | 15 +++++++++++++++ src/parser/classes/MusicHeader.js | 10 +++++++++- src/parser/classes/MusicResponsiveListItem.ts | 6 +++++- src/parser/classes/MusicTwoRowItem.js | 2 +- src/parser/map.ts | 2 ++ 6 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/parser/classes/GridHeader.ts diff --git a/src/parser/classes/Grid.js b/src/parser/classes/Grid.js index d158050e..70cf0e08 100644 --- a/src/parser/classes/Grid.js +++ b/src/parser/classes/Grid.js @@ -11,6 +11,10 @@ class Grid extends YTNode { this.visible_row_count = data.visibleRowCount; this.target_id = data.targetId; this.continuation = data.continuations?.[0]?.nextContinuationData?.continuation || null; + + if (data.header) { + this.header = Parser.parse(data.header); + } } // XXX: alias for consistency diff --git a/src/parser/classes/GridHeader.ts b/src/parser/classes/GridHeader.ts new file mode 100644 index 00000000..b16769f8 --- /dev/null +++ b/src/parser/classes/GridHeader.ts @@ -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; \ No newline at end of file diff --git a/src/parser/classes/MusicHeader.js b/src/parser/classes/MusicHeader.js index 1ddcfa7b..6091f652 100644 --- a/src/parser/classes/MusicHeader.js +++ b/src/parser/classes/MusicHeader.js @@ -1,12 +1,20 @@ import Parser from '../index'; import { YTNode } from '../helpers'; +import Text from './misc/Text'; class MusicHeader extends YTNode { static type = 'MusicHeader'; constructor(data) { 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); + } } } diff --git a/src/parser/classes/MusicResponsiveListItem.ts b/src/parser/classes/MusicResponsiveListItem.ts index 7378f932..61181941 100644 --- a/src/parser/classes/MusicResponsiveListItem.ts +++ b/src/parser/classes/MusicResponsiveListItem.ts @@ -52,7 +52,9 @@ class MusicResponsiveListItem extends YTNode { }[]; name?: string; + subtitle?: Text; subscribers?: string; + song_count?: string; // TODO: these might be replaceable with Author class author?: { name: string, @@ -186,7 +188,9 @@ class MusicResponsiveListItem extends YTNode { #parseArtist() { this.id = this.endpoint?.browse?.id; 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() { diff --git a/src/parser/classes/MusicTwoRowItem.js b/src/parser/classes/MusicTwoRowItem.js index 6d86d767..3c5ee90a 100644 --- a/src/parser/classes/MusicTwoRowItem.js +++ b/src/parser/classes/MusicTwoRowItem.js @@ -22,7 +22,7 @@ class MusicTwoRowItem extends YTNode { switch (this.endpoint?.browse?.page_type) { case 'MUSIC_PAGE_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; case 'MUSIC_PAGE_TYPE_PLAYLIST': this.item_type = 'playlist'; diff --git a/src/parser/map.ts b/src/parser/map.ts index 5b11a1c3..c57aed8d 100644 --- a/src/parser/map.ts +++ b/src/parser/map.ts @@ -64,6 +64,7 @@ import { default as FeedFilterChipBar } from './classes/FeedFilterChipBar'; import { default as FeedTabbedHeader } from './classes/FeedTabbedHeader'; import { default as Grid } from './classes/Grid'; import { default as GridChannel } from './classes/GridChannel'; +import { default as GridHeader } from './classes/GridHeader'; import { default as GridPlaylist } from './classes/GridPlaylist'; import { default as GridVideo } from './classes/GridVideo'; import { default as HistorySuggestion } from './classes/HistorySuggestion'; @@ -303,6 +304,7 @@ const map: Record = { FeedTabbedHeader, Grid, GridChannel, + GridHeader, GridPlaylist, GridVideo, HistorySuggestion,