Files
YouTube.js/src/parser/ytkids/HomeFeed.ts
LuanRT 95e0294eab refactor!: overhaul core classes and remove redundant code (#388)
* feat(Player.ts): append `cver` to deciphered URLs

* refactor(Actions.ts): remove redundant `getVideoInfo` function

This is leftover code from previous versions. It had many problems and it is no longer required.

* fix(Kids.ts): remove unneeded `await` keywords

* dev: add more endpoints

* chore: update deps

* refactor: separate endpoints into files

* dev: improve types

* dev: add more endpoints

* refactor: put clients in a separate directory inside `core`

* chore: lint

* refactor: move mixins and managers to separate folders

* chore: fix tests

* dev: add `CreateVideoEndpoint`

* chore: clean up

* chore: lint

* chore: add some comments

* chore: remove unnecessary test

* dev: add `playlist/CreateEndpoint`

* dev: add `playlist/DeleteEndpoint`

* dev: add `browse/EditPlaylistEndpoint`

* fix(parser): add a few checks to avoid parsing errors
2023-04-28 19:01:04 -03:00

51 lines
1.9 KiB
TypeScript

import Feed from '../../core/mixins/Feed.js';
import KidsCategoriesHeader from '../classes/ytkids/KidsCategoriesHeader.js';
import KidsCategoryTab from '../classes/ytkids/KidsCategoryTab.js';
import KidsHomeScreen from '../classes/ytkids/KidsHomeScreen.js';
import { InnertubeError } from '../../utils/Utils.js';
import type Actions from '../../core/Actions.js';
import type { ApiResponse } from '../../core/Actions.js';
import type { IBrowseResponse } from '../types/ParsedResponse.js';
class HomeFeed extends Feed<IBrowseResponse> {
header?: KidsCategoriesHeader;
contents?: KidsHomeScreen;
constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);
this.header = this.page.header?.item().as(KidsCategoriesHeader);
this.contents = this.page.contents?.item().as(KidsHomeScreen);
}
/**
* Retrieves the contents of the given category tab. Use {@link HomeFeed.categories} to get a list of available categories.
* @param tab - The tab to select
*/
async selectCategoryTab(tab: string | KidsCategoryTab): Promise<HomeFeed> {
let target_tab: KidsCategoryTab | undefined;
if (typeof tab === 'string') {
target_tab = this.header?.category_tabs.find((t) => t.title.toString() === tab);
} else if (tab?.is(KidsCategoryTab)) {
target_tab = tab;
}
if (!target_tab)
throw new InnertubeError(`Tab "${tab}" not found`);
const page = await target_tab.endpoint.call<IBrowseResponse>(this.actions, { client: 'YTKIDS', parse: true });
// Copy over the header and header memo
page.header = this.page.header;
page.header_memo = this.page.header_memo;
return new HomeFeed(this.actions, page, true);
}
get categories(): string[] {
return this.header?.category_tabs.map((tab) => tab.title.toString()) || [];
}
}
export default HomeFeed;