Skip to content

Commit

Permalink
perf: Cleanup some unnecessary uses of YTNode#key and Maybe (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
absidue authored Aug 6, 2023
1 parent e370116 commit 0dda97e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/core/clients/Music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export default class Music {
if (!page.contents)
throw new InnertubeError('Unexpected response', page);

if (page.contents.item().key('type').string() === 'Message')
if (page.contents.item().type === 'Message')
throw new InnertubeError(page.contents.item().as(Message).text.toString(), video_id);

const section_list = page.contents.item().as(SectionList).contents;
Expand Down
44 changes: 22 additions & 22 deletions src/parser/classes/MusicResponsiveListItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class MusicResponsiveListItem extends YTNode {
}

#parseOther() {
this.title = this.flex_columns.first().key('title').instanceof(Text).toString();
this.title = this.flex_columns.first().title.toString();

if (this.endpoint) {
this.item_type = 'endpoint';
Expand All @@ -134,7 +134,7 @@ export default class MusicResponsiveListItem extends YTNode {
}

#parseVideoOrSong() {
const is_video = this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.some((run) => run.text.match(/(.*?) views/));
const is_video = this.flex_columns.at(1)?.title.runs?.some((run) => run.text.match(/(.*?) views/));
if (is_video) {
this.item_type = 'video';
this.#parseVideo();
Expand All @@ -146,10 +146,10 @@ export default class MusicResponsiveListItem extends YTNode {

#parseSong() {
this.id = this.#playlist_item_data.video_id || this.endpoint?.payload?.videoId;
this.title = this.flex_columns.first().key('title').instanceof(Text).toString();
this.title = this.flex_columns.first().title.toString();

const duration_text = this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.find(
(run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text || this.fixed_columns.first()?.key('title').instanceof(Text)?.toString();
const duration_text = this.flex_columns.at(1)?.title.runs?.find(
(run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text || this.fixed_columns.first()?.title?.toString();

if (duration_text) {
this.duration = {
Expand All @@ -159,12 +159,12 @@ export default class MusicResponsiveListItem extends YTNode {
}

const album_run =
this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.find(
this.flex_columns.at(1)?.title.runs?.find(
(run) =>
(isTextRun(run) && run.endpoint) &&
run.endpoint.payload.browseId.startsWith('MPR')
) ||
this.flex_columns.at(2)?.key('title').instanceof(Text).runs?.find(
this.flex_columns.at(2)?.title.runs?.find(
(run) =>
(isTextRun(run) && run.endpoint) &&
run.endpoint.payload.browseId.startsWith('MPR')
Expand All @@ -178,7 +178,7 @@ export default class MusicResponsiveListItem extends YTNode {
};
}

const artist_runs = this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.filter(
const artist_runs = this.flex_columns.at(1)?.title.runs?.filter(
(run) => (isTextRun(run) && run.endpoint) && run.endpoint.payload.browseId.startsWith('UC')
);

Expand All @@ -193,10 +193,10 @@ export default class MusicResponsiveListItem extends YTNode {

#parseVideo() {
this.id = this.#playlist_item_data.video_id;
this.title = this.flex_columns.first().key('title').instanceof(Text).toString();
this.views = this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.find((run) => run.text.match(/(.*?) views/))?.toString();
this.title = this.flex_columns.first().title.toString();
this.views = this.flex_columns.at(1)?.title.runs?.find((run) => run.text.match(/(.*?) views/))?.toString();

const author_runs = this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.filter(
const author_runs = this.flex_columns.at(1)?.title.runs?.filter(
(run) =>
(isTextRun(run) && run.endpoint) &&
run.endpoint.payload.browseId.startsWith('UC')
Expand All @@ -212,8 +212,8 @@ export default class MusicResponsiveListItem extends YTNode {
});
}

const duration_text = this.flex_columns[1].key('title').instanceof(Text).runs?.find(
(run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text || this.fixed_columns.first()?.key('title').instanceof(Text).runs?.find((run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text;
const duration_text = this.flex_columns[1].title.runs?.find(
(run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text || this.fixed_columns.first()?.title.runs?.find((run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text;

if (duration_text) {
this.duration = {
Expand All @@ -225,22 +225,22 @@ export default class MusicResponsiveListItem extends YTNode {

#parseArtist() {
this.id = this.endpoint?.payload?.browseId;
this.name = this.flex_columns.first().key('title').instanceof(Text).toString();
this.subtitle = this.flex_columns.at(1)?.key('title').instanceof(Text);
this.name = this.flex_columns.first().title.toString();
this.subtitle = this.flex_columns.at(1)?.title;
this.subscribers = this.subtitle?.runs?.find((run) => (/^(\d*\.)?\d+[M|K]? subscribers?$/i).test(run.text))?.text || '';
}

#parseLibraryArtist() {
this.name = this.flex_columns.first().key('title').instanceof(Text).toString();
this.subtitle = this.flex_columns.at(1)?.key('title').instanceof(Text);
this.name = this.flex_columns.first().title.toString();
this.subtitle = this.flex_columns.at(1)?.title;
this.song_count = this.subtitle?.runs?.find((run) => (/^\d+(,\d+)? songs?$/i).test(run.text))?.text || '';
}

#parseAlbum() {
this.id = this.endpoint?.payload?.browseId;
this.title = this.flex_columns.first().title.toString();

const author_run = this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.find(
const author_run = this.flex_columns.at(1)?.title.runs?.find(
(run) =>
(isTextRun(run) && run.endpoint) &&
run.endpoint.payload.browseId.startsWith('UC')
Expand All @@ -254,7 +254,7 @@ export default class MusicResponsiveListItem extends YTNode {
};
}

this.year = this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.find(
this.year = this.flex_columns.at(1)?.title.runs?.find(
(run) => (/^[12][0-9]{3}$/).test(run.text)
)?.text;
}
Expand All @@ -263,12 +263,12 @@ export default class MusicResponsiveListItem extends YTNode {
this.id = this.endpoint?.payload?.browseId;
this.title = this.flex_columns.first().title.toString();

const item_count_run = this.flex_columns.at(1)?.key('title')
.instanceof(Text).runs?.find((run) => run.text.match(/\d+ (song|songs)/));
const item_count_run = this.flex_columns.at(1)?.title
.runs?.find((run) => run.text.match(/\d+ (song|songs)/));

this.item_count = item_count_run ? item_count_run.text : undefined;

const author_run = this.flex_columns.at(1)?.key('title').instanceof(Text).runs?.find(
const author_run = this.flex_columns.at(1)?.title.runs?.find(
(run) =>
(isTextRun(run) && run.endpoint) &&
run.endpoint.payload.browseId.startsWith('UC')
Expand Down
3 changes: 2 additions & 1 deletion src/parser/youtube/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ExpandableTab from '../classes/ExpandableTab.js';
import SectionList from '../classes/SectionList.js';
import Tab from '../classes/Tab.js';
import PageHeader from '../classes/PageHeader.js';
import TwoColumnBrowseResults from '../classes/TwoColumnBrowseResults.js';

import Feed from '../../core/mixins/Feed.js';
import FilterableFeed from '../../core/mixins/FilterableFeed.js';
Expand Down Expand Up @@ -53,7 +54,7 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {

this.subscribe_button = this.page.header_memo?.getType(SubscribeButton).first();

this.current_tab = this.page.contents?.item().key('tabs').parsed().array().filterType(Tab, ExpandableTab).get({ selected: true });
this.current_tab = this.page.contents?.item().as(TwoColumnBrowseResults).tabs.array().filterType(Tab, ExpandableTab).get({ selected: true });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parser/ytmusic/HomeFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class HomeFeed {
if (!tab)
throw new InnertubeError('Could not find Home tab.');

if (tab.key('content').isNull()) {
if (tab.content === null) {
if (!this.#page.continuation_contents)
throw new InnertubeError('Continuation did not have any content.');

Expand Down
3 changes: 1 addition & 2 deletions src/parser/ytmusic/Library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ class LibraryContinuation {

this.contents = this.#page.continuation_contents.as(MusicShelfContinuation, GridContinuation);

this.#continuation = this.#page.continuation_contents?.key('continuation').isNull()
? null : this.#page.continuation_contents?.key('continuation').string();
this.#continuation = this.contents.continuation || null;
}

async getContinuation(): Promise<LibraryContinuation> {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/ytmusic/TrackInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class TrackInfo extends MediaInfo {

const page = await target_tab.endpoint.call(this.actions, { client: 'YTMUSIC', parse: true });

if (page.contents?.item().key('type').string() === 'Message')
if (page.contents?.item().type === 'Message')
return page.contents.item().as(Message);

if (!page.contents)
Expand Down

0 comments on commit 0dda97e

Please sign in to comment.