Skip to content

Commit

Permalink
chore: clean up some classes
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRT committed Nov 22, 2024
1 parent d8dca8c commit cf09f7b
Show file tree
Hide file tree
Showing 34 changed files with 210 additions and 236 deletions.
4 changes: 2 additions & 2 deletions src/core/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export type ParsedResponse<T> =
IParsedResponse;

export default class Actions {
session: Session;
public session: Session;

constructor(session: Session) {
this.session = session;
}

/**
* Mimmics the Axios API using Fetch's Response object.
* Mimics the Axios API using Fetch's Response object.
* @param response - The response object.
*/
async #wrap(response: Response): Promise<ApiResponse> {
Expand Down
10 changes: 5 additions & 5 deletions src/core/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const TAG = 'Player';
* Represents YouTube's player script. This is required to decipher signatures.
*/
export default class Player {
player_id: string;
sts: number;
nsig_sc?: string;
sig_sc?: string;
po_token?: string;
public player_id: string;
public sts: number;
public nsig_sc?: string;
public sig_sc?: string;
public po_token?: string;

constructor(player_id: string, signature_timestamp: number, sig_sc?: string, nsig_sc?: string) {
this.player_id = player_id;
Expand Down
22 changes: 11 additions & 11 deletions src/core/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,17 @@ const TAG = 'Session';
* Represents an InnerTube session. This holds all the data needed to make requests to YouTube.
*/
export default class Session extends EventEmitter {
context: Context;
player?: Player;
oauth: OAuth2;
http: HTTPClient;
logged_in: boolean;
actions: Actions;
cache?: ICache;
key: string;
api_version: string;
account_index: number;
po_token?: string;
public context: Context;
public player?: Player;
public oauth: OAuth2;
public http: HTTPClient;
public logged_in: boolean;
public actions: Actions;
public cache?: ICache;
public key: string;
public api_version: string;
public account_index: number;
public po_token?: string;

constructor(context: Context, api_key: string, api_version: string, account_index: number, player?: Player, cookie?: string, fetch?: FetchFunction, cache?: ICache, po_token?: string) {
super();
Expand Down
21 changes: 11 additions & 10 deletions src/core/mixins/MediaInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
IPlayerResponse,
IStreamingData
} from '../../parser/index.js';

import { Parser } from '../../parser/index.js';
import { TranscriptInfo } from '../../parser/youtube/index.js';
import ContinuationItem from '../../parser/classes/ContinuationItem.js';
Expand All @@ -34,16 +35,16 @@ export default class MediaInfo {
readonly #actions: Actions;
readonly #cpn: string;
readonly #playback_tracking?: IPlaybackTracking;
basic_info;
annotations?: ObservedArray<PlayerAnnotationsExpanded>;
storyboards?: PlayerStoryboardSpec | PlayerLiveStoryboardSpec;
endscreen?: Endscreen;
captions?: PlayerCaptionsTracklist;
cards?: CardCollection;
streaming_data?: IStreamingData;
playability_status?: IPlayabilityStatus;
player_config?: IPlayerConfig;

public basic_info;
public annotations?: ObservedArray<PlayerAnnotationsExpanded>;
public storyboards?: PlayerStoryboardSpec | PlayerLiveStoryboardSpec;
public endscreen?: Endscreen;
public captions?: PlayerCaptionsTracklist;
public cards?: CardCollection;
public streaming_data?: IStreamingData;
public playability_status?: IPlayabilityStatus;
public player_config?: IPlayerConfig;

constructor(data: [ApiResponse, ApiResponse?], actions: Actions, cpn: string) {
this.#actions = actions;
Expand Down
2 changes: 1 addition & 1 deletion src/parser/youtube/AccountInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { IParsedResponse } from '../types/index.js';
import type AccountItemSection from '../classes/AccountItemSection.js';

export default class AccountInfo {
#page: IParsedResponse;
readonly #page: IParsedResponse;

contents: AccountItemSection | null;

Expand Down
36 changes: 14 additions & 22 deletions src/parser/youtube/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import type { ApiResponse, Actions } from '../../core/index.js';
import type { IBrowseResponse } from '../types/index.js';

export default class Channel extends TabbedFeed<IBrowseResponse> {
header?: C4TabbedHeader | CarouselHeader | InteractiveTabbedHeader | PageHeader;
metadata;
subscribe_button?: SubscribeButton;
current_tab?: Tab | ExpandableTab;
public header?: C4TabbedHeader | CarouselHeader | InteractiveTabbedHeader | PageHeader;
public metadata;
public subscribe_button?: SubscribeButton;
public current_tab?: Tab | ExpandableTab;

constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);
Expand All @@ -55,7 +55,8 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {

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

this.current_tab = this.page.contents?.item().as(TwoColumnBrowseResults).tabs.array().filterType(Tab, ExpandableTab).get({ selected: true });
if (this.page.contents)
this.current_tab = this.page.contents.item().as(TwoColumnBrowseResults).tabs.array().filterType(Tab, ExpandableTab).get({ selected: true });
}

/**
Expand All @@ -71,14 +72,14 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
target_filter = filter_chipbar?.contents.get({ text: filter });
if (!target_filter)
throw new InnertubeError(`Filter ${filter} not found`, { available_filters: this.filters });
} else if (filter instanceof ChipCloudChip) {
} else {
target_filter = filter;
}

if (!target_filter)
if (!target_filter.endpoint)
throw new InnertubeError('Invalid filter', filter);

const page = await target_filter.endpoint?.call<IBrowseResponse>(this.actions, { parse: true });
const page = await target_filter.endpoint.call<IBrowseResponse>(this.actions, { parse: true });

if (!page)
throw new InnertubeError('No page returned', { filter: target_filter });
Expand All @@ -93,18 +94,18 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
async applySort(sort: string): Promise<Channel> {
const sort_filter_sub_menu = this.memo.getType(SortFilterSubMenu).first();

if (!sort_filter_sub_menu)
if (!sort_filter_sub_menu || !sort_filter_sub_menu.sub_menu_items)
throw new InnertubeError('No sort filter sub menu found');

const target_sort = sort_filter_sub_menu?.sub_menu_items?.find((item) => item.title === sort);
const target_sort = sort_filter_sub_menu.sub_menu_items.find((item) => item.title === sort);

if (!target_sort)
throw new InnertubeError(`Sort filter ${sort} not found`, { available_sort_filters: this.sort_filters });

if (target_sort.selected)
return this;

const page = await target_sort.endpoint?.call<IBrowseResponse>(this.actions, { parse: true });
const page = await target_sort.endpoint.call<IBrowseResponse>(this.actions, { parse: true });

return new Channel(this.actions, page, true);
}
Expand All @@ -127,7 +128,7 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
if (item.selected)
return this;

const page = await item.endpoint?.call<IBrowseResponse>(this.actions, { parse: true });
const page = await item.endpoint.call<IBrowseResponse>(this.actions, { parse: true });

return new Channel(this.actions, page, true);
}
Expand Down Expand Up @@ -233,7 +234,7 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
if (!tab)
throw new InnertubeError('Search tab not found', this);

const page = await tab.endpoint?.call<IBrowseResponse>(this.actions, { query, parse: true });
const page = await tab.endpoint.call<IBrowseResponse>(this.actions, { query, parse: true });

return new Channel(this.actions, page, true);
}
Expand Down Expand Up @@ -281,9 +282,6 @@ export default class Channel extends TabbedFeed<IBrowseResponse> {
return this.memo.getType(ExpandableTab)?.length > 0;
}

/**
* Retrives list continuation.
*/
async getContinuation(): Promise<ChannelListContinuation> {
const page = await super.getContinuationData();
if (!page)
Expand All @@ -302,9 +300,6 @@ export class ChannelListContinuation extends Feed<IBrowseResponse> {
this.page.on_response_received_endpoints?.first();
}

/**
* Retrieves list continuation.
*/
async getContinuation(): Promise<ChannelListContinuation> {
const page = await super.getContinuationData();
if (!page)
Expand Down Expand Up @@ -342,9 +337,6 @@ export class FilteredChannelList extends FilterableFeed<IBrowseResponse> {
return new FilteredChannelList(this.actions, feed.page, true);
}

/**
* Retrieves list continuation.
*/
async getContinuation(): Promise<FilteredChannelList> {
const page = await super.getContinuationData();

Expand Down
19 changes: 9 additions & 10 deletions src/parser/youtube/Comments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Parser } from '../index.js';
import { InnertubeError } from '../../utils/Utils.js';
import type { ObservedArray } from '../helpers.js';
import { observe } from '../helpers.js';

import CommentsHeader from '../classes/comments/CommentsHeader.js';
Expand All @@ -8,16 +9,15 @@ import CommentThread from '../classes/comments/CommentThread.js';
import ContinuationItem from '../classes/ContinuationItem.js';

import type { Actions, ApiResponse } from '../../core/index.js';
import type { ObservedArray } from '../helpers.js';
import type { INextResponse } from '../types/index.js';

export default class Comments {
#page: INextResponse;
#actions: Actions;
#continuation?: ContinuationItem;
readonly #page: INextResponse;
readonly #actions: Actions;
readonly #continuation?: ContinuationItem;

header?: CommentsHeader;
contents: ObservedArray<CommentThread>;
public header?: CommentsHeader;
public contents: ObservedArray<CommentThread>;

constructor(actions: Actions, data: any, already_parsed = false) {
this.#page = already_parsed ? data : Parser.parseResponse<INextResponse>(data);
Expand All @@ -36,7 +36,8 @@ export default class Comments {
const threads = body_node?.contents?.filterType(CommentThread) || [];

this.contents = observe(threads.map((thread) => {
thread.comment?.setActions(this.#actions);
if (thread.comment)
thread.comment.setActions(this.#actions);
thread.setActions(this.#actions);
return thread;
}));
Expand Down Expand Up @@ -87,9 +88,7 @@ export default class Comments {
if (!button.endpoint)
throw new InnertubeError('Button does not have an endpoint.');

const response = await button.endpoint.call(this.#actions, { commentText: text });

return response;
return await button.endpoint.call(this.#actions, { commentText: text });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/parser/youtube/Guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type { IGuideResponse } from '../types/index.js';
import type { IRawResponse } from '../index.js';

export default class Guide {
#page: IGuideResponse;
contents?: ObservedArray<GuideSection | GuideSubscriptionsSection>;
readonly #page: IGuideResponse;
public contents?: ObservedArray<GuideSection | GuideSubscriptionsSection>;

constructor(data: IRawResponse) {
this.#page = Parser.parseResponse<IGuideResponse>(data);
Expand Down
4 changes: 2 additions & 2 deletions src/parser/youtube/HashtagFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { IBrowseResponse } from '../index.js';
import type ChipCloudChip from '../classes/ChipCloudChip.js';

export default class HashtagFeed extends FilterableFeed<IBrowseResponse> {
header?: HashtagHeader | PageHeader;
contents: RichGrid;
public header?: HashtagHeader | PageHeader;
public contents: RichGrid;

constructor(actions: Actions, response: IBrowseResponse | ApiResponse) {
super(actions, response);
Expand Down
4 changes: 2 additions & 2 deletions src/parser/youtube/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type Video from '../classes/Video.js';

// TODO: make feed actions usable
export default class History extends Feed<IBrowseResponse> {
sections: ItemSection[];
feed_actions: BrowseFeedActions;
public sections: ItemSection[];
public feed_actions: BrowseFeedActions;

constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);
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 @@ -8,8 +8,8 @@ import type { ApiResponse, Actions } from '../../core/index.js';
import type ChipCloudChip from '../classes/ChipCloudChip.js';

export default class HomeFeed extends FilterableFeed<IBrowseResponse> {
contents?: RichGrid | AppendContinuationItemsAction | ReloadContinuationItemsCommand;
header?: FeedTabbedHeader;
public contents?: RichGrid | AppendContinuationItemsAction | ReloadContinuationItemsCommand;
public header?: FeedTabbedHeader;

constructor(actions: Actions, data: ApiResponse | IBrowseResponse, already_parsed = false) {
super(actions, data, already_parsed);
Expand Down
10 changes: 4 additions & 6 deletions src/parser/youtube/ItemMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import type { IParsedResponse } from '../types/index.js';
import type NavigationEndpoint from '../classes/NavigationEndpoint.js';

export default class ItemMenu {
#page: IParsedResponse;
#actions: Actions;
#items: ObservedArray<YTNode>;
readonly #page: IParsedResponse;
readonly #actions: Actions;
readonly #items: ObservedArray<YTNode>;

constructor(data: IParsedResponse, actions: Actions) {
this.#page = data;
Expand Down Expand Up @@ -53,9 +53,7 @@ export default class ItemMenu {
if (!endpoint)
throw new InnertubeError('Target button does not have an endpoint.');

const response = await endpoint.call(this.#actions, { parse: true });

return response;
return await endpoint.call(this.#actions, { parse: true });
}

items(): ObservedArray<YTNode> {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/youtube/Library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type { Actions, ApiResponse } from '../../core/index.js';
import type { IBrowseResponse } from '../types/index.js';

export default class Library extends Feed<IBrowseResponse> {
header: PageHeader | null;
sections;
public header: PageHeader | null;
public sections;

constructor(actions: Actions, data: ApiResponse | IBrowseResponse) {
super(actions, data);
Expand Down
Loading

0 comments on commit cf09f7b

Please sign in to comment.