From 95ff1e6c5efcd23ff98852a5e06f6c6fc37f6d46 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Fri, 11 Nov 2022 19:00:12 -0300 Subject: [PATCH] refactor(Library): use memo to get target YTNodes --- src/parser/youtube/Library.ts | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/src/parser/youtube/Library.ts b/src/parser/youtube/Library.ts index 6b91c505..c0c468fc 100644 --- a/src/parser/youtube/Library.ts +++ b/src/parser/youtube/Library.ts @@ -5,16 +5,10 @@ import { InnertubeError } from '../../utils/Utils'; import Feed from '../../core/Feed'; import History from './History'; import Playlist from './Playlist'; - -import Tab from '../classes/Tab'; import Menu from '../classes/menus/Menu'; import Shelf from '../classes/Shelf'; import Button from '../classes/Button'; -import SectionList from '../classes/SectionList'; -import ItemSection from '../classes/ItemSection'; -import TwoColumnBrowseResults from '../classes/TwoColumnBrowseResults'; -import ProfileColumn from '../classes/ProfileColumn'; import ProfileColumnStats from '../classes/ProfileColumnStats'; import ProfileColumnUserInfo from '../classes/ProfileColumnUserInfo'; @@ -29,30 +23,17 @@ class Library { this.#actions = actions; this.#page = Parser.parseResponse(response); - const two_col = this.#page.contents.item().as(TwoColumnBrowseResults); - - if (!two_col) - throw new InnertubeError('Response did not have a TwoColumnBrowseResults.'); - - const tab = two_col.tabs.array().as(Tab).get({ selected: true }); - - if (!tab) - throw new InnertubeError('Could not find target tab.'); - - const stats = two_col.secondary_contents.item().as(ProfileColumn).items.array().get({ type: 'ProfileColumnStats' })?.as(ProfileColumnStats) || null; - const user_info = two_col.secondary_contents.item().as(ProfileColumn).items.array().get({ type: 'ProfileColumnUserInfo' })?.as(ProfileColumnUserInfo) || null; + const stats = this.#page.contents_memo.getType(ProfileColumnStats)?.[0]; + const user_info = this.#page.contents_memo.getType(ProfileColumnUserInfo)?.[0]; this.profile = { stats, user_info }; - if (!tab.content) - throw new InnertubeError('Target tab did not have any content.'); - - const shelves = tab.content.as(SectionList).contents.array().as(ItemSection).map((is: ItemSection) => is.contents?.firstOfType(Shelf)); + const shelves = this.#page.contents_memo.getType(Shelf); this.sections = shelves.map((shelf: any) => ({ type: shelf.icon_type, title: shelf.title, - contents: shelf.content?.item().items.array() || [], + contents: shelf.content?.item().items || [], getAll: () => this.#getAll(shelf) })); } @@ -61,7 +42,7 @@ class Library { if (!shelf.menu?.item().as(Menu).hasKey('top_level_buttons')) throw new InnertubeError(`The ${shelf.title.text} shelf doesn't have more items`); - const button = await shelf.menu.item().as(Menu).top_level_buttons.get({ text: 'See all' }); + const button = shelf.menu.item().as(Menu).top_level_buttons.get({ text: 'See all' }); if (!button) throw new InnertubeError('Did not find target button.');