Skip to content

Commit

Permalink
chore: clean up & remove unneeded code (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRT authored Dec 31, 2022
1 parent 6a4b4f3 commit 9ac5043
Show file tree
Hide file tree
Showing 47 changed files with 915 additions and 731 deletions.
92 changes: 51 additions & 41 deletions src/Innertube.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@

import Session, { SessionOptions } from './core/Session';
import type { ParsedResponse } from './parser';
import type { ActionsResponse } from './core/Actions';

import Search from './parser/youtube/Search';
import NavigationEndpoint from './parser/classes/NavigationEndpoint';
import Channel from './parser/youtube/Channel';
import Playlist from './parser/youtube/Playlist';
import Library from './parser/youtube/Library';
import History from './parser/youtube/History';
import Comments from './parser/youtube/Comments';
import History from './parser/youtube/History';
import Library from './parser/youtube/Library';
import NotificationsMenu from './parser/youtube/NotificationsMenu';
import Playlist from './parser/youtube/Playlist';
import Search from './parser/youtube/Search';
import VideoInfo, { DownloadOptions, FormatOptions } from './parser/youtube/VideoInfo';
import NavigationEndpoint from './parser/classes/NavigationEndpoint';

import { ParsedResponse } from './parser';
import { ActionsResponse } from './core/Actions';

import AccountManager from './core/AccountManager';
import Feed from './core/Feed';
import InteractionManager from './core/InteractionManager';
import YTMusic from './core/Music';
import Studio from './core/Studio';
import HomeFeed from './parser/youtube/HomeFeed';
import AccountManager from './core/AccountManager';
import PlaylistManager from './core/PlaylistManager';
import InteractionManager from './core/InteractionManager';
import Studio from './core/Studio';
import TabbedFeed from './core/TabbedFeed';
import Constants from './utils/Constants';
import HomeFeed from './parser/youtube/HomeFeed';
import Proto from './proto/index';
import Constants from './utils/Constants';

import type Actions from './core/Actions';
import type Format from './parser/classes/misc/Format';

import { throwIfMissing, generateRandomString } from './utils/Utils';
import { generateRandomString, throwIfMissing } from './utils/Utils';

export type InnertubeConfig = SessionOptions;

Expand All @@ -36,16 +38,16 @@ export interface SearchFilters {
sort_by?: 'relevance' | 'rating' | 'upload_date' | 'view_count'
}

export type InnerTubeClient = 'WEB' | 'ANDROID' | 'YTMUSIC_ANDROID' | 'YTMUSIC' | 'TV_EMBEDDED';
export type InnerTubeClient = 'WEB' | 'ANDROID' | 'YTMUSIC_ANDROID' | 'YTMUSIC' | 'YTSTUDIO_ANDROID' | 'TV_EMBEDDED';

class Innertube {
session;
account;
playlist;
interact;
music;
studio;
actions;
session: Session;
account: AccountManager;
playlist: PlaylistManager;
interact: InteractionManager;
music: YTMusic;
studio: Studio;
actions: Actions;

constructor(session: Session) {
this.session = session;
Expand All @@ -57,7 +59,7 @@ class Innertube {
this.actions = this.session.actions;
}

static async create(config: InnertubeConfig = {}) {
static async create(config: InnertubeConfig = {}): Promise<Innertube> {
return new Innertube(await Session.create(config));
}

Expand All @@ -66,7 +68,9 @@ class Innertube {
* @param video_id - The video id.
* @param client - The client to use.
*/
async getInfo(video_id: string, client?: InnerTubeClient) {
async getInfo(video_id: string, client?: InnerTubeClient): Promise<VideoInfo> {
throwIfMissing({ video_id });

const cpn = generateRandomString(16);

const initial_info = this.actions.getVideoInfo(video_id, cpn, client);
Expand All @@ -81,7 +85,9 @@ class Innertube {
* @param video_id - The video id.
* @param client - The client to use.
*/
async getBasicInfo(video_id: string, client?: InnerTubeClient) {
async getBasicInfo(video_id: string, client?: InnerTubeClient): Promise<VideoInfo> {
throwIfMissing({ video_id });

const cpn = generateRandomString(16);
const response = await this.actions.getVideoInfo(video_id, cpn, client);

Expand All @@ -93,7 +99,7 @@ class Innertube {
* @param query - The search query.
* @param filters - Search filters.
*/
async search(query: string, filters: SearchFilters = {}) {
async search(query: string, filters: SearchFilters = {}): Promise<Search> {
throwIfMissing({ query });

const args = {
Expand Down Expand Up @@ -138,7 +144,7 @@ class Innertube {
* @param video_id - The video id.
* @param sort_by - Sorting options.
*/
async getComments(video_id: string, sort_by?: 'TOP_COMMENTS' | 'NEWEST_FIRST') {
async getComments(video_id: string, sort_by?: 'TOP_COMMENTS' | 'NEWEST_FIRST'): Promise<Comments> {
throwIfMissing({ video_id });

const payload = Proto.encodeCommentsSectionParams(video_id, {
Expand All @@ -153,15 +159,15 @@ class Innertube {
/**
* Retrieves YouTube's home feed (aka recommendations).
*/
async getHomeFeed() {
async getHomeFeed(): Promise<HomeFeed> {
const response = await this.actions.execute('/browse', { browseId: 'FEwhat_to_watch' });
return new HomeFeed(this.actions, response.data);
}

/**
* Returns the account's library.
*/
async getLibrary() {
async getLibrary(): Promise<Library> {
const response = await this.actions.execute('/browse', { browseId: 'FElibrary' });
return new Library(response.data, this.actions);
}
Expand All @@ -170,32 +176,32 @@ class Innertube {
* Retrieves watch history.
* Which can also be achieved with {@link getLibrary}.
*/
async getHistory() {
async getHistory(): Promise<History> {
const response = await this.actions.execute('/browse', { browseId: 'FEhistory' });
return new History(this.actions, response.data);
}

/**
* Retrieves trending content.
*/
async getTrending() {
async getTrending(): Promise<TabbedFeed> {
const response = await this.actions.execute('/browse', { browseId: 'FEtrending' });
return new TabbedFeed(this.actions, response.data);
}

/**
* Retrieves subscriptions feed.
*/
async getSubscriptionsFeed() {
async getSubscriptionsFeed(): Promise<Feed> {
const response = await this.actions.execute('/browse', { browseId: 'FEsubscriptions' });
return new Feed(this.actions, response.data);
}

/**
* Retrieves contents for a given channel.
* @param id - channel id
* @param id - Channel id
*/
async getChannel(id: string) {
async getChannel(id: string): Promise<Channel> {
throwIfMissing({ id });
const response = await this.actions.execute('/browse', { browseId: id });
return new Channel(this.actions, response.data);
Expand All @@ -204,7 +210,7 @@ class Innertube {
/**
* Retrieves notifications.
*/
async getNotifications() {
async getNotifications(): Promise<NotificationsMenu> {
const response = await this.actions.execute('/notification/get_notification_menu', { notificationsMenuRequestType: 'NOTIFICATIONS_MENU_REQUEST_TYPE_INBOX' });
return new NotificationsMenu(this.actions, response);
}
Expand All @@ -220,8 +226,9 @@ class Innertube {

/**
* Retrieves playlist contents.
* @param id - Playlist id
*/
async getPlaylist(id: string) {
async getPlaylist(id: string): Promise<Playlist> {
throwIfMissing({ id });

if (!id.startsWith('VL')) {
Expand All @@ -237,18 +244,21 @@ class Innertube {
* Returns deciphered streaming data.
*
* If you wish to retrieve the video info too, have a look at {@link getBasicInfo} or {@link getInfo}.
* @param video_id - The video id.
* @param options - Format options.
*/
async getStreamingData(video_id: string, options: FormatOptions = {}) {
async getStreamingData(video_id: string, options: FormatOptions = {}): Promise<Format> {
const info = await this.getBasicInfo(video_id);
return info.chooseFormat(options);
}

/**
* Downloads a given video. If you only need the direct download link see {@link getStreamingData}.
*
* If you wish to retrieve the video info too, have a look at {@link getBasicInfo} or {@link getInfo}.
* @param video_id - The video id.
* @param options - Download options.
*/
async download(video_id: string, options?: DownloadOptions) {
async download(video_id: string, options?: DownloadOptions): Promise<ReadableStream<Uint8Array>> {
const info = await this.getBasicInfo(video_id, options?.client);
return info.download(options);
}
Expand All @@ -258,8 +268,8 @@ class Innertube {
* @param endpoint -The endpoint to call.
* @param args - Call arguments.
*/
call(endpoint: NavigationEndpoint, args: { [ key: string ]: any; parse: true }): Promise<ParsedResponse>;
call(endpoint: NavigationEndpoint, args?: { [ key: string ]: any; parse?: false }): Promise<ActionsResponse>;
call(endpoint: NavigationEndpoint, args: { [key: string]: any; parse: true }): Promise<ParsedResponse>;
call(endpoint: NavigationEndpoint, args?: { [key: string]: any; parse?: false }): Promise<ActionsResponse>;
call(endpoint: NavigationEndpoint, args?: object): Promise<ActionsResponse | ParsedResponse> {
return endpoint.call(this.actions, args);
}
Expand Down
21 changes: 14 additions & 7 deletions src/core/AccountManager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import Proto from '../proto/index';
import Actions from './Actions';
import type Actions from './Actions';
import type { ActionsResponse } from './Actions';

import Analytics from '../parser/youtube/Analytics';
import TimeWatched from '../parser/youtube/TimeWatched';
import AccountInfo from '../parser/youtube/AccountInfo';
import Settings from '../parser/youtube/Settings';

import { InnertubeError } from '../utils/Utils';

class AccountManager {
#actions;
channel;
#actions: Actions;

channel: {
editName: (new_name: string) => Promise<ActionsResponse>;
editDescription: (new_description: string) => Promise<ActionsResponse>;
getBasicAnalytics: () => Promise<Analytics>;
};

constructor(actions: Actions) {
this.#actions = actions;
Expand Down Expand Up @@ -51,7 +58,7 @@ class AccountManager {
/**
* Retrieves channel info.
*/
async getInfo() {
async getInfo(): Promise<AccountInfo> {
if (!this.#actions.session.logged_in)
throw new InnertubeError('You must be signed in to perform this operation.');

Expand All @@ -62,7 +69,7 @@ class AccountManager {
/**
* Retrieves time watched statistics.
*/
async getTimeWatched() {
async getTimeWatched(): Promise<TimeWatched> {
const response = await this.#actions.execute('/browse', {
browseId: 'SPtime_watched',
client: 'ANDROID'
Expand All @@ -74,7 +81,7 @@ class AccountManager {
/**
* Opens YouTube settings.
*/
async getSettings() {
async getSettings(): Promise<Settings> {
const response = await this.#actions.execute('/browse', {
browseId: 'SPaccount_overview'
});
Expand All @@ -85,7 +92,7 @@ class AccountManager {
/**
* Retrieves basic channel analytics.
*/
async getAnalytics() {
async getAnalytics(): Promise<Analytics> {
const info = await this.getInfo();

const params = Proto.encodeChannelAnalyticsParams(info.footers?.endpoint.payload.browseId);
Expand Down
12 changes: 6 additions & 6 deletions src/core/Actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Session from './Session';
import Parser, { ParsedResponse } from '../parser/index';
import { InnertubeError } from '../utils/Utils';
import type Session from './Session';

export interface ApiResponse {
success: boolean;
Expand All @@ -11,21 +11,21 @@ export interface ApiResponse {
export type ActionsResponse = Promise<ApiResponse>;

class Actions {
#session;
#session: Session;

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

get session() {
get session(): Session {
return this.#session;
}

/**
* Mimmics the Axios API using Fetch's Response object.
* @param response - The response object.
*/
async #wrap(response: Response) {
async #wrap(response: Response): Promise<ApiResponse> {
return {
success: response.ok,
status_code: response.status,
Expand All @@ -40,7 +40,7 @@ class Actions {
* @param client - The client to use.
* @param playlist_id - The playlist ID.
*/
async getVideoInfo(id: string, cpn?: string, client?: string, playlist_id?: string) {
async getVideoInfo(id: string, cpn?: string, client?: string, playlist_id?: string): Promise<ActionsResponse> {
const data: Record<string, any> = {
playbackContext: {
contentPlaybackContext: {
Expand Down Expand Up @@ -90,7 +90,7 @@ class Actions {
* @param client - The client to use.
* @param params - Call parameters.
*/
async stats(url: string, client: { client_name: string; client_version: string }, params: { [key: string]: any }) {
async stats(url: string, client: { client_name: string; client_version: string }, params: { [key: string]: any }): Promise<Response> {
const s_url = new URL(url);

s_url.searchParams.set('ver', '2');
Expand Down
Loading

0 comments on commit 9ac5043

Please sign in to comment.