Skip to content

Commit

Permalink
refactor: Remove internal uses of the .first() and .matchCondition() …
Browse files Browse the repository at this point in the history
…helpers (#889)
  • Loading branch information
absidue authored Feb 8, 2025
1 parent d0d48bf commit 0e86902
Show file tree
Hide file tree
Showing 24 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/core/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default class Actions {
let parsed_response = Parser.parseResponse<ParsedResponse<T>>(await response.json());

// Handle redirects
if (this.#isBrowse(parsed_response) && parsed_response.on_response_received_actions?.first()?.type === 'navigateAction') {
if (this.#isBrowse(parsed_response) && parsed_response.on_response_received_actions?.[0]?.type === 'navigateAction') {
const navigate_action = parsed_response.on_response_received_actions.firstOfType(NavigateAction);
if (navigate_action) {
parsed_response = await navigate_action.endpoint.call(this, { parse: true });
Expand Down
8 changes: 4 additions & 4 deletions src/core/clients/Music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default class Music {
const response = await watch_next_endpoint.call(this.#actions, { client: 'YTMUSIC', parse: true });

const tabs = response.contents_memo?.getType(Tab);
const tab = tabs?.first();
const tab = tabs?.[0];

if (!tab)
throw new InnertubeError('Could not find target tab.');
Expand All @@ -228,7 +228,7 @@ export default class Music {
if (!page || !page.contents_memo)
throw new InnertubeError('Could not fetch automix');

return page.contents_memo.getType(PlaylistPanel).first();
return page.contents_memo.getType(PlaylistPanel)[0];
}

return playlist_panel;
Expand All @@ -242,7 +242,7 @@ export default class Music {

const tabs = response.contents_memo?.getType(Tab);

const tab = tabs?.matchCondition((tab) => tab.endpoint.payload.browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType === 'MUSIC_PAGE_TYPE_TRACK_RELATED');
const tab = tabs?.find((tab) => tab.endpoint.payload.browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType === 'MUSIC_PAGE_TYPE_TRACK_RELATED');

if (!tab)
throw new InnertubeError('Could not find target tab.');
Expand All @@ -263,7 +263,7 @@ export default class Music {

const tabs = response.contents_memo?.getType(Tab);

const tab = tabs?.matchCondition((tab) => tab.endpoint.payload.browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType === 'MUSIC_PAGE_TYPE_TRACK_LYRICS');
const tab = tabs?.find((tab) => tab.endpoint.payload.browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType === 'MUSIC_PAGE_TYPE_TRACK_LYRICS');

if (!tab)
throw new InnertubeError('Could not find target tab.');
Expand Down
6 changes: 3 additions & 3 deletions src/core/mixins/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ export default class Feed<T extends IParsedResponse = IParsedResponse> {
* Returns contents from the page.
*/
get page_contents(): SectionList | MusicQueue | RichGrid | ReloadContinuationItemsCommand {
const tab_content = this.#memo.getType(Tab)?.first().content;
const reload_continuation_items = this.#memo.getType(ReloadContinuationItemsCommand).first();
const append_continuation_items = this.#memo.getType(AppendContinuationItemsAction).first();
const tab_content = this.#memo.getType(Tab)?.[0].content;
const reload_continuation_items = this.#memo.getType(ReloadContinuationItemsCommand)[0];
const append_continuation_items = this.#memo.getType(AppendContinuationItemsAction)[0];

return tab_content || reload_continuation_items || append_continuation_items;
}
Expand Down
22 changes: 11 additions & 11 deletions src/parser/classes/MusicResponsiveListItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default class MusicResponsiveListItem extends YTNode {
}

#parseOther() {
this.title = this.flex_columns.first().title.toString();
this.title = this.flex_columns[0].title.toString();

if (this.endpoint) {
this.item_type = 'endpoint';
Expand Down Expand Up @@ -183,10 +183,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().title.toString();
this.title = this.flex_columns[0].title.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();
(run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text || this.fixed_columns[0]?.title?.toString();

if (duration_text) {
this.duration = {
Expand Down Expand Up @@ -230,7 +230,7 @@ export default class MusicResponsiveListItem extends YTNode {

#parseVideo() {
this.id = this.#playlist_item_data.video_id;
this.title = this.flex_columns.first().title.toString();
this.title = this.flex_columns[0].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)?.title.runs?.filter(
Expand All @@ -250,7 +250,7 @@ export default class MusicResponsiveListItem extends YTNode {
}

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;
(run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text || this.fixed_columns[0]?.title.runs?.find((run) => (/^\d+$/).test(run.text.replace(/:/g, '')))?.text;

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

#parseArtist() {
this.id = this.endpoint?.payload?.browseId;
this.name = this.flex_columns.first().title.toString();
this.name = this.flex_columns[0].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().title.toString();
this.name = this.flex_columns[0].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 || '';
}

#parseNonMusicTrack() {
this.id = this.#playlist_item_data.video_id || this.endpoint?.payload?.videoId;
this.title = this.flex_columns.first().title.toString();
this.title = this.flex_columns[0].title.toString();
}

#parsePodcastShow() {
this.id = this.endpoint?.payload?.browseId;
this.title = this.flex_columns.first().title.toString();
this.title = this.flex_columns[0].title.toString();
}

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

const author_run = this.flex_columns.at(1)?.title.runs?.find(
(run) =>
Expand All @@ -308,7 +308,7 @@ export default class MusicResponsiveListItem extends YTNode {

#parsePlaylist() {
this.id = this.endpoint?.payload?.browseId;
this.title = this.flex_columns.first().title.toString();
this.title = this.flex_columns[0].title.toString();

const item_count_run = this.flex_columns.at(1)?.title
.runs?.find((run) => run.text.match(/\d+ (song|songs)/));
Expand Down
4 changes: 2 additions & 2 deletions src/parser/classes/comments/CommentThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class CommentThread extends YTNode {
throw new InnertubeError('Unexpected response.', response);

this.replies = this.#getPatchedReplies(response.on_response_received_endpoints_memo);
this.#continuation = response.on_response_received_endpoints_memo.getType(ContinuationItem).first();
this.#continuation = response.on_response_received_endpoints_memo.getType(ContinuationItem)[0];

return this;
}
Expand Down Expand Up @@ -87,7 +87,7 @@ export default class CommentThread extends YTNode {
throw new InnertubeError('Unexpected response.', response);

this.replies = this.#getPatchedReplies(response.on_response_received_endpoints_memo);
this.#continuation = response.on_response_received_endpoints_memo.getType(ContinuationItem).first();
this.#continuation = response.on_response_received_endpoints_memo.getType(ContinuationItem)[0];

return this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/youtube/AccountInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export default class AccountInfo {
if (!this.#page.contents)
throw new InnertubeError('Page contents not found');

const account_section_list = this.#page.contents.array().as(AccountSectionList).first();
const account_section_list = this.#page.contents.array().as(AccountSectionList)[0];

if (!account_section_list)
throw new InnertubeError('Account section list not found');

this.contents = account_section_list.contents.first();
this.contents = account_section_list.contents[0];
}

get page(): IParsedResponse {
Expand Down
16 changes: 8 additions & 8 deletions src/parser/youtube/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
const microformat = this.page.microformat?.as(MicroformatData);

if (this.page.alerts) {
const alert = this.page.alerts.first();
const alert = this.page.alerts[0];
if (alert?.alert_type === 'ERROR') {
throw new ChannelError(alert.text.toString());
}
Expand All @@ -59,7 +59,7 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {

this.metadata = { ...metadata, ...(microformat || {}) };

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

if (this.page.contents)
this.current_tab = this.page.contents.item().as(TwoColumnBrowseResults).tabs.array().filterType(Tab, ExpandableTab).get({ selected: true });
Expand All @@ -72,7 +72,7 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
async applyFilter(filter: string | ChipCloudChip): Promise<FilteredChannelList> {
let target_filter: ChipCloudChip | undefined;

const filter_chipbar = this.memo.getType(FeedFilterChipBar).first();
const filter_chipbar = this.memo.getType(FeedFilterChipBar)[0];

if (typeof filter === 'string') {
target_filter = filter_chipbar?.contents.get({ text: filter });
Expand All @@ -98,7 +98,7 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
* @param sort - The sort filter to apply
*/
async applySort(sort: string): Promise<Channel> {
const sort_filter_sub_menu = this.memo.getType(SortFilterSubMenu).first();
const sort_filter_sub_menu = this.memo.getType(SortFilterSubMenu)[0];

if (!sort_filter_sub_menu || !sort_filter_sub_menu.sub_menu_items)
throw new InnertubeError('No sort filter sub menu found');
Expand Down Expand Up @@ -144,7 +144,7 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
}

get sort_filters(): string[] {
const sort_filter_sub_menu = this.memo.getType(SortFilterSubMenu).first();
const sort_filter_sub_menu = this.memo.getType(SortFilterSubMenu)[0];
return sort_filter_sub_menu?.sub_menu_items?.map((item) => item.title) || [];
}

Expand Down Expand Up @@ -302,8 +302,8 @@ export class ChannelListContinuation extends Feed<IBrowseResponse> {
constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);
this.contents =
this.page.on_response_received_actions?.first() ||
this.page.on_response_received_endpoints?.first();
this.page.on_response_received_actions?.[0] ||
this.page.on_response_received_endpoints?.[0];
}

async getContinuation(): Promise<ChannelListContinuation> {
Expand Down Expand Up @@ -331,7 +331,7 @@ export class FilteredChannelList extends FilterableFeed<IBrowseResponse> {
this.page.on_response_received_actions.shift();
}

this.contents = this.page.on_response_received_actions?.first();
this.contents = this.page.on_response_received_actions?.[0];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parser/youtube/HashtagFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class HashtagFeed extends FilterableFeed<IBrowseResponse> {
if (!this.page.contents_memo)
throw new InnertubeError('Unexpected response', this.page);

const tab = this.page.contents_memo.getType(Tab).first();
const tab = this.page.contents_memo.getType(Tab)[0];

if (!tab.content)
throw new InnertubeError('Content tab has no content', tab);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/youtube/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class History extends Feed<IBrowseResponse> {
constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);
this.sections = this.memo.getType(ItemSection);
this.feed_actions = this.memo.getType(BrowseFeedActions).first();
this.feed_actions = this.memo.getType(BrowseFeedActions)[0];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/parser/youtube/HomeFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default class HomeFeed extends FilterableFeed<IBrowseResponse> {

constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);
this.header = this.memo.getType(FeedTabbedHeader).first();
this.contents = this.memo.getType(RichGrid).first() || this.page.on_response_received_actions?.first();
this.header = this.memo.getType(FeedTabbedHeader)[0];
this.contents = this.memo.getType(RichGrid)[0] || this.page.on_response_received_actions?.[0];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parser/youtube/Library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class Library extends Feed<IBrowseResponse> {
if (!this.page.contents_memo)
throw new InnertubeError('Page contents not found');

this.header = this.memo.getType(PageHeader).first();
this.header = this.memo.getType(PageHeader)[0];

const shelves = this.page.contents_memo.getType(Shelf);

Expand Down
4 changes: 2 additions & 2 deletions src/parser/youtube/NotificationsMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export default class NotificationsMenu {
if (!this.#page.actions_memo)
throw new InnertubeError('Page actions not found');

this.header = this.#page.actions_memo.getType(SimpleMenuHeader).first();
this.header = this.#page.actions_memo.getType(SimpleMenuHeader)[0];
this.contents = this.#page.actions_memo.getType(Notification);
}

async getContinuation(): Promise<NotificationsMenu> {
const continuation = this.#page.actions_memo?.getType(ContinuationItem).first();
const continuation = this.#page.actions_memo?.getType(ContinuationItem)[0];

if (!continuation)
throw new InnertubeError('Continuation not found');
Expand Down
10 changes: 5 additions & 5 deletions src/parser/youtube/Playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export default class Playlist extends Feed<IBrowseResponse> {
constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);

const header = this.memo.getType(PlaylistHeader).first();
const primary_info = this.memo.getType(PlaylistSidebarPrimaryInfo).first();
const secondary_info = this.memo.getType(PlaylistSidebarSecondaryInfo).first();
const header = this.memo.getType(PlaylistHeader)[0];
const primary_info = this.memo.getType(PlaylistSidebarPrimaryInfo)[0];
const secondary_info = this.memo.getType(PlaylistSidebarSecondaryInfo)[0];
const alert = this.page.alerts?.firstOfType(Alert);

if (alert && alert.alert_type === 'ERROR')
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class Playlist extends Feed<IBrowseResponse> {
}

get has_continuation() {
const section_list = this.memo.getType(SectionList).first();
const section_list = this.memo.getType(SectionList)[0];

if (!section_list)
return super.has_continuation;
Expand All @@ -77,7 +77,7 @@ export default class Playlist extends Feed<IBrowseResponse> {
}

async getContinuationData(): Promise<IBrowseResponse | undefined> {
const section_list = this.memo.getType(SectionList).first();
const section_list = this.memo.getType(SectionList)[0];

/**
* No section list means there can't be additional continuation nodes here,
Expand Down
8 changes: 4 additions & 4 deletions src/parser/youtube/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export default class Search extends Feed<ISearchResponse> {
super(actions, data, already_parsed);

const contents =
this.page.contents_memo?.getType(SectionList).first().contents ||
this.page.on_response_received_commands?.first().as(AppendContinuationItemsAction, ReloadContinuationItemsCommand).contents;
this.page.contents_memo?.getType(SectionList)[0].contents ||
this.page.on_response_received_commands?.[0].as(AppendContinuationItemsAction, ReloadContinuationItemsCommand).contents;

if (!contents)
throw new InnertubeError('No contents found in search response');
Expand All @@ -44,8 +44,8 @@ export default class Search extends Feed<ISearchResponse> {
this.estimated_results = this.page.estimated_results || 0;

if (this.page.contents_memo) {
this.sub_menu = this.page.contents_memo.getType(SearchSubMenu).first();
this.watch_card = this.page.contents_memo.getType(UniversalWatchCard).first();
this.sub_menu = this.page.contents_memo.getType(SearchSubMenu)[0];
this.watch_card = this.page.contents_memo.getType(UniversalWatchCard)[0];
}

this.refinement_cards = this.results?.firstOfType(HorizontalCardList);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/youtube/TranscriptInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class TranscriptInfo {
if (!this.#page.actions_memo)
throw new Error('Page actions not found');

this.transcript = this.#page.actions_memo.getType(Transcript).first();
this.transcript = this.#page.actions_memo.getType(Transcript)[0];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parser/youtube/VideoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class VideoInfo extends MediaInfo {
const comments_entry_point = results.get({ target_id: 'comments-entry-point' })?.as(ItemSection);

this.comments_entry_point_header = comments_entry_point?.contents?.firstOfType(CommentsEntryPointHeader);
this.livechat = next?.contents_memo?.getType(LiveChat).first();
this.livechat = next?.contents_memo?.getType(LiveChat)[0];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/ytkids/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Channel extends Feed<IBrowseResponse> {
constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);
this.header = this.page.header?.item().as(C4TabbedHeader);
this.contents = this.memo.getType(ItemSection).first() || this.page.continuation_contents?.as(ItemSectionContinuation);
this.contents = this.memo.getType(ItemSection)[0] || this.page.continuation_contents?.as(ItemSectionContinuation);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parser/ytkids/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Search extends Feed<ISearchResponse> {
super(actions, data);
this.estimated_results = this.page.estimated_results;

const item_section = this.memo.getType(ItemSection).first();
const item_section = this.memo.getType(ItemSection)[0];

if (!item_section)
throw new InnertubeError('No item section found in search response.');
Expand Down
4 changes: 2 additions & 2 deletions src/parser/ytmusic/Album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export default class Album {
if (!this.#page.contents_memo)
throw new Error('No contents found in the response');

this.header = this.#page.contents_memo.getType(MusicDetailHeader, MusicResponsiveHeader)?.first();
this.contents = this.#page.contents_memo.getType(MusicShelf)?.first().contents || observe([]);
this.header = this.#page.contents_memo.getType(MusicDetailHeader, MusicResponsiveHeader)?.[0];
this.contents = this.#page.contents_memo.getType(MusicShelf)?.[0].contents || observe([]);
this.sections = this.#page.contents_memo.getType(MusicCarouselShelf) || observe([]);
this.background = this.#page.background;
this.url = this.#page.microformat?.as(MicroformatData).url_canonical;
Expand Down
Loading

0 comments on commit 0e86902

Please sign in to comment.