refactor: clean up, fix & remove outdated code (#228)

* dev: refactor and remove redundant code

* docs(music): update `Library` API ref

* docs: update examples

* chore: update lock file
This commit is contained in:
LuanRT
2022-11-06 03:32:16 -03:00
committed by GitHub
parent 1eda93ee08
commit aa334aacbd
50 changed files with 2006 additions and 2403 deletions

View File

@@ -1,5 +1,5 @@
import Parser, { ParsedResponse } from '..';
import { AxioslikeResponse } from '../../core/Actions';
import { ApiResponse } from '../../core/Actions';
import AccountSectionList from '../classes/AccountSectionList';
import AccountItemSection from '../classes/AccountItemSection';
@@ -11,7 +11,7 @@ class AccountInfo {
contents: AccountItemSection | null;
footers: AccountChannel | null;
constructor(response: AxioslikeResponse) {
constructor(response: ApiResponse) {
this.#page = Parser.parseResponse(response.data);
const account_section_list = this.#page.contents.array().as(AccountSectionList)[0];

View File

@@ -1,12 +1,12 @@
import Parser, { ParsedResponse } from '..';
import { AxioslikeResponse } from '../../core/Actions';
import { ApiResponse } from '../../core/Actions';
import Element from '../classes/Element';
class Analytics {
#page;
sections;
constructor(response: AxioslikeResponse) {
constructor(response: ApiResponse) {
this.#page = Parser.parseResponse(response.data);
this.sections = this.#page.contents_memo?.get('Element')
?.map((el) => el.as(Element).model?.item());

View File

@@ -16,13 +16,13 @@ class Channel extends TabbedFeed {
constructor(actions: Actions, data: any, already_parsed = false) {
super(actions, data, already_parsed);
this.header = this.page.header.item().as(C4TabbedHeader);
this.header = this.page.header?.item().as(C4TabbedHeader);
const metadata = this.page.metadata.item().as(ChannelMetadata);
const microformat = this.page.microformat?.as(MicroformatData);
this.metadata = { ...metadata, ...(microformat || {}) };
this.sponsor_button = this.header.sponsor_button;
this.subscribe_button = this.header.subscribe_button;
this.sponsor_button = this.header?.sponsor_button;
this.subscribe_button = this.header?.subscribe_button;
const tab = this.page.contents.item().key('tabs').parsed().array().filterType(Tab).get({ selected: true });

View File

@@ -40,19 +40,18 @@ class Comments {
/**
* Creates a top-level comment.
* @param text - Comment text.
*/
async createComment(text: string): Promise<ActionsResponse> {
if (!this.header)
throw new InnertubeError('Page header is missing.');
throw new InnertubeError('Page header is missing. Cannot create comment.');
const button = this.header.create_renderer?.as(CommentSimplebox).submit_button.item().as(Button);
const button = this.header.create_renderer?.as(CommentSimplebox).submit_button?.item().as(Button);
if (!button)
throw new InnertubeError('Could not find target button.');
throw new InnertubeError('Could not find target button. You are probably not logged in.');
const response = await button.endpoint.callTest(this.#actions, {
commentText: text
});
const response = await button.endpoint.call(this.#actions, { commentText: text });
return response;
}
@@ -64,13 +63,13 @@ class Comments {
if (!this.#continuation)
throw new InnertubeError('Continuation not found');
const data = await this.#continuation.endpoint.callTest(this.#actions, { parse: true });
const data = await this.#continuation.endpoint.call(this.#actions, { parse: true });
// Copy the previous page so we can keep the header.
const page = Object.assign({}, this.#page);
if (!page.on_response_received_endpoints || !data.on_response_received_endpoints)
throw new InnertubeError('Invalid reponse format, missing on_response_received_endpoints');
throw new InnertubeError('Invalid reponse format, missing on_response_received_endpoints.');
// Remove previous items and append the continuation.
page.on_response_received_endpoints.pop();

View File

@@ -48,7 +48,7 @@ class ItemMenu {
endpoint = button.as(MenuServiceItem).endpoint;
}
const response = await endpoint.callTest(this.#actions, { parse: true });
const response = await endpoint.call(this.#actions, { parse: true });
return response;
}

View File

@@ -1,5 +1,5 @@
import Parser, { ParsedResponse } from '..';
import Actions, { AxioslikeResponse } from '../../core/Actions';
import Actions, { ApiResponse } from '../../core/Actions';
import { InnertubeError } from '../../utils/Utils';
import Feed from '../../core/Feed';
@@ -25,7 +25,7 @@ class Library {
profile;
sections;
constructor(response: AxioslikeResponse, actions: Actions) {
constructor(response: ApiResponse, actions: Actions) {
this.#actions = actions;
this.#page = Parser.parseResponse(response);
@@ -66,7 +66,7 @@ class Library {
if (!button)
throw new InnertubeError('Did not find target button.');
const page = await button.as(Button).endpoint.callTest(this.#actions, { parse: true });
const page = await button.as(Button).endpoint.call(this.#actions, { parse: true });
switch (shelf.icon_type) {
case 'LIKE':

View File

@@ -192,7 +192,7 @@ class LiveChat extends EventEmitter {
if (!item.menu_endpoint)
throw new InnertubeError('This item does not have a menu.', item);
const response = await item.menu_endpoint.call(this.#actions, undefined, true);
const response = await item.menu_endpoint.call(this.#actions, { parse: true });
if (!response)
throw new InnertubeError('Could not retrieve item menu.', item);
@@ -204,7 +204,7 @@ class LiveChat extends EventEmitter {
* Equivalent to "clicking" a button.
*/
async selectButton(button: Button): Promise<ParsedResponse> {
const response = await button.endpoint.callTest(this.#actions, { parse: true });
const response = await button.endpoint.call(this.#actions, { parse: true });
return response;
}

View File

@@ -1,5 +1,5 @@
import Parser from '..';
import Actions, { AxioslikeResponse } from '../../core/Actions';
import Actions, { ApiResponse } from '../../core/Actions';
import { InnertubeError } from '../../utils/Utils';
import Notification from '../classes/Notification';
@@ -13,7 +13,7 @@ class NotificationsMenu {
header;
contents;
constructor(actions: Actions, response: AxioslikeResponse) {
constructor(actions: Actions, response: ApiResponse) {
this.#actions = actions;
this.#page = Parser.parseResponse(response.data);
@@ -27,7 +27,7 @@ class NotificationsMenu {
if (!continuation)
throw new InnertubeError('Continuation not found');
const response = await continuation.endpoint.callTest(this.#actions, { parse: false });
const response = await continuation.endpoint.call(this.#actions, { parse: false });
return new NotificationsMenu(this.#actions, response);
}
}

View File

@@ -4,13 +4,14 @@ import Feed from '../../core/Feed';
import Thumbnail from '../classes/misc/Thumbnail';
import VideoOwner from '../classes/VideoOwner';
import PlaylistSidebar from '../classes/PlaylistSidebar';
import PlaylistMetadata from '../classes/PlaylistMetadata';
import PlaylistSidebarPrimaryInfo from '../classes/PlaylistSidebarPrimaryInfo';
import PlaylistSidebarSecondaryInfo from '../classes/PlaylistSidebarSecondaryInfo';
import PlaylistVideoThumbnail from '../classes/PlaylistVideoThumbnail';
import PlaylistHeader from '../classes/PlaylistHeader';
import { InnertubeError } from '../../utils/Utils';
class Playlist extends Feed {
info;
menu;
@@ -19,9 +20,12 @@ class Playlist extends Feed {
constructor(actions: Actions, data: any, already_parsed = false) {
super(actions, data, already_parsed);
const header = this.page.header.item().as(PlaylistHeader);
const primary_info = this.page.sidebar?.as(PlaylistSidebar).contents.array().firstOfType(PlaylistSidebarPrimaryInfo);
const secondary_info = this.page.sidebar?.as(PlaylistSidebar).contents.array().firstOfType(PlaylistSidebarSecondaryInfo);
const header = this.memo.getType(PlaylistHeader)?.[0];
const primary_info = this.memo.getType(PlaylistSidebarPrimaryInfo)?.[0];
const secondary_info = this.memo.getType(PlaylistSidebarSecondaryInfo)?.[0];
if (!primary_info && !secondary_info)
throw new InnertubeError('This playlist does not exist');
this.info = {
...this.page.metadata.item().as(PlaylistMetadata),
@@ -31,14 +35,14 @@ class Playlist extends Feed {
total_items: this.#getStat(0, primary_info),
views: this.#getStat(1, primary_info),
last_updated: this.#getStat(2, primary_info),
can_share: header.can_share,
can_delete: header.can_delete,
is_editable: header.is_editable,
privacy: header.privacy
can_share: header?.can_share,
can_delete: header?.can_delete,
is_editable: header?.is_editable,
privacy: header?.privacy
}
};
this.menu = primary_info?.menu;
this.menu = primary_info?.menu.item();
this.endpoint = primary_info?.endpoint;
}
@@ -50,6 +54,11 @@ class Playlist extends Feed {
get items() {
return this.videos;
}
async getContinuation(): Promise<Playlist> {
const response = await this.getContinuationData();
return new Playlist(this.actions, response);
}
}
export default Playlist;

View File

@@ -66,7 +66,7 @@ class Search extends Feed {
throw new InnertubeError('Invalid refinement card!');
}
const page = await target_card.endpoint.call(this.actions, undefined, true);
const page = await target_card.endpoint.call(this.actions, { parse: true });
return new Search(this.actions, page, true);
}

View File

@@ -1,5 +1,5 @@
import Parser from '..';
import Actions, { AxioslikeResponse } from '../../core/Actions';
import Actions, { ApiResponse } from '../../core/Actions';
import { InnertubeError } from '../../utils/Utils';
import Tab from '../classes/Tab';
@@ -20,7 +20,7 @@ class Settings {
introduction: PageIntroduction | null | undefined;
sections;
constructor(actions: Actions, response: AxioslikeResponse) {
constructor(actions: Actions, response: ApiResponse) {
this.#actions = actions;
this.#page = Parser.parseResponse(response.data);
@@ -53,7 +53,7 @@ class Settings {
if (!item)
throw new InnertubeError(`Item "${name}" not found`, { available_items: this.sidebar_items });
const response = await item.endpoint.callTest(this.#actions, { parse: false });
const response = await item.endpoint.call(this.#actions, { parse: false });
return new Settings(this.#actions, response);
}

View File

@@ -1,5 +1,5 @@
import Parser, { ParsedResponse } from '..';
import { AxioslikeResponse } from '../../core/Actions';
import { ApiResponse } from '../../core/Actions';
import ItemSection from '../classes/ItemSection';
import SingleColumnBrowseResults from '../classes/SingleColumnBrowseResults';
@@ -11,7 +11,7 @@ class TimeWatched {
#page;
contents;
constructor(response: AxioslikeResponse) {
constructor(response: ApiResponse) {
this.#page = Parser.parseResponse(response.data);
const tab = this.#page.contents.item().as(SingleColumnBrowseResults).tabs.get({ selected: true });

View File

@@ -1,6 +1,6 @@
import Parser, { ParsedResponse } from '../index';
import Constants from '../../utils/Constants';
import Actions, { AxioslikeResponse } from '../../core/Actions';
import Actions, { ApiResponse } from '../../core/Actions';
import Player from '../../core/Player';
import TwoColumnWatchNextResults from '../classes/TwoColumnWatchNextResults';
@@ -92,7 +92,7 @@ class VideoInfo {
* @param data - API response.
* @param cpn - Client Playback Nonce
*/
constructor(data: [AxioslikeResponse, AxioslikeResponse?], actions: Actions, player: Player, cpn: string) {
constructor(data: [ApiResponse, ApiResponse?], actions: Actions, player: Player, cpn: string) {
this.#actions = actions;
this.#player = player;
this.#cpn = cpn;
@@ -177,7 +177,7 @@ class VideoInfo {
const filter = this.related_chip_cloud?.chips?.get({ text: name });
if (filter?.is_selected) return this;
const response = await filter?.endpoint?.call(this.#actions, undefined, true);
const response = await filter?.endpoint?.call(this.#actions, { parse: true });
const data = response?.on_response_received_endpoints?.get({ target_id: 'watch-next-feed' });
this.watch_next_feed = data?.contents;
@@ -213,7 +213,7 @@ class VideoInfo {
* Retrieves watch next feed continuation.
*/
async getWatchNextContinuation() {
const response = await this.#watch_next_continuation?.endpoint.call(this.#actions, undefined, true);
const response = await this.#watch_next_continuation?.endpoint.call(this.#actions, { parse: true });
const data = response?.on_response_received_endpoints?.get({ type: 'appendContinuationItemsAction' });
if (!data)