Skip to content

Commit

Permalink
feat(YouTube/Search): add SearchSubMenu node (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRT authored Mar 7, 2023
1 parent cf8a33c commit a511608
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/parser/classes/SearchFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import Text from './misc/Text.js';
import NavigationEndpoint from './NavigationEndpoint.js';

class SearchFilter extends YTNode {
static type = 'SearchFilter';

label: Text;
endpoint: NavigationEndpoint;
tooltip: string;

constructor(data: RawNode) {
super();

this.label = new Text(data.label);
this.endpoint = new NavigationEndpoint(data.endpoint);
this.tooltip = data.tooltip;
}
}

export default SearchFilter;
20 changes: 20 additions & 0 deletions src/parser/classes/SearchFilterGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ObservedArray, YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import { Parser, YTNodes } from '../index.js';
import Text from './misc/Text.js';

class SearchFilterGroup extends YTNode {
static type = 'SearchFilterGroup';

title: Text;
filters: ObservedArray<YTNodes.SearchFilter> | null;

constructor(data: RawNode) {
super();

this.title = new Text(data.title);
this.filters = Parser.parseArray(data.filters, YTNodes.SearchFilter);
}
}

export default SearchFilterGroup;
21 changes: 21 additions & 0 deletions src/parser/classes/SearchSubMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ObservedArray, YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import Parser, { YTNodes } from '../index.js';
import Text from './misc/Text.js';

class SearchSubMenu extends YTNode {
static type = 'SearchSubMenu';

title: Text;
groups: ObservedArray<YTNodes.SearchFilterGroup> | null;
button: YTNodes.ToggleButton | null;

constructor(data: RawNode) {
super();
this.title = new Text(data.title);
this.groups = Parser.parseArray(data.groups, YTNodes.SearchFilterGroup);
this.button = Parser.parseItem(data.button, YTNodes.ToggleButton);
}
}

export default SearchSubMenu;
9 changes: 9 additions & 0 deletions src/parser/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,14 @@ import { default as RichShelf } from './classes/RichShelf.js';
export { RichShelf };
import { default as SearchBox } from './classes/SearchBox.js';
export { SearchBox };
import { default as SearchFilter } from './classes/SearchFilter.js';
export { SearchFilter };
import { default as SearchFilterGroup } from './classes/SearchFilterGroup.js';
export { SearchFilterGroup };
import { default as SearchRefinementCard } from './classes/SearchRefinementCard.js';
export { SearchRefinementCard };
import { default as SearchSubMenu } from './classes/SearchSubMenu.js';
export { SearchSubMenu };
import { default as SearchSuggestion } from './classes/SearchSuggestion.js';
export { SearchSuggestion };
import { default as SearchSuggestionsSection } from './classes/SearchSuggestionsSection.js';
Expand Down Expand Up @@ -930,7 +936,10 @@ const map: Record<string, YTNodeConstructor> = {
RichSection,
RichShelf,
SearchBox,
SearchFilter,
SearchFilterGroup,
SearchRefinementCard,
SearchSubMenu,
SearchSuggestion,
SearchSuggestionsSection,
SecondarySearchContainer,
Expand Down
1 change: 0 additions & 1 deletion src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ export default class Parser {
'RunAttestationCommand',
'CompactPromotedVideo',
'StatementBanner',
'SearchSubMenu',
'GuideSigninPromo'
]);

Expand Down
9 changes: 6 additions & 3 deletions src/parser/youtube/Search.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import Feed from '../../core/Feed.js';
import { InnertubeError } from '../../utils/Utils.js';
import HorizontalCardList from '../classes/HorizontalCardList.js';
import ItemSection from '../classes/ItemSection.js';
import SearchRefinementCard from '../classes/SearchRefinementCard.js';
import SearchSubMenu from '../classes/SearchSubMenu.js';
import SectionList from '../classes/SectionList.js';
import UniversalWatchCard from '../classes/UniversalWatchCard.js';
import { InnertubeError } from '../../utils/Utils.js';

import type Actions from '../../core/Actions.js';
import type { ApiResponse } from '../../core/Actions.js';
import type { ObservedArray, YTNode } from '../helpers.js';
import type { ISearchResponse } from '../types/ParsedResponse.js';
import type { ApiResponse } from '../../core/Actions.js';

class Search extends Feed<ISearchResponse> {
results?: ObservedArray<YTNode> | null;
refinements: string[];
estimated_results: number;
sub_menu?: SearchSubMenu;
watch_card?: UniversalWatchCard;
refinement_cards?: HorizontalCardList | null;

Expand All @@ -33,8 +35,9 @@ class Search extends Feed<ISearchResponse> {
this.refinements = this.page.refinements || [];
this.estimated_results = this.page.estimated_results;

this.sub_menu = this.page.contents_memo?.getType(SearchSubMenu).first();
this.watch_card = this.page.contents_memo?.getType(UniversalWatchCard).first();
this.refinement_cards = this.results?.get({ type: 'HorizontalCardList' }, true)?.as(HorizontalCardList);
this.refinement_cards = this.results?.firstOfType(HorizontalCardList);
}

/**
Expand Down

0 comments on commit a511608

Please sign in to comment.