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:
Patrick Kan
2022-08-18 16:35:24 +08:00
committed by GitHub
parent dba34dc5ae
commit 31326ec9eb
6 changed files with 36 additions and 3 deletions

View File

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

View 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;

View File

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

View File

@@ -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() {

View File

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