mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-19 12:31:17 +00:00
* refactor: remove dependancies removes node-forge and uuid in favor of Web APIs * refactor!: commonjs to es6 To aid with #93 I will make all my changes in TypeScript instead. This is the first step into making that happen. Used: https://github.com/wessberg/cjstoesm * refactor!: NToken and Signature TS files Bring this PR up to speed with #93 * feat: cross platform cache (WIP) this is untested! should remove idb as dependecy. * feat: EventEmitter polyfill * refactor: remove events * feat: HTTPClient based on Fetch API (WIP) * refactor!: parsers refactor (WIP) Initial TS support for parsers as per #93 This adds several type safety checks to the parser which'll help to ensure valid data is returned by the parser. * refactor!: parsers refactor (WIP) Bring more in line with the existing implementations & make less verbose * refactor!: parser refactor I was overcomplicating things, this is much simpler and compatible with the existing JS API * fix: some missed parsers while refactoring * fix: better type inferance for parseResponse * feat(TS): typesafe YTNode casts * feat: more type safety in YTNode and Parser * refactor: VideoInfo download with fetch & TS (WIP) Again, this also does some work for #93 * fix: LiveChat in VideoInfo * refactor!: more typesafety in parser * refactor!: VideoInfo almost completed * refactor!: player and session refactors - Remove the Player class' dependance on Session. - Add additional context to the Session. * refactor!: move auth logic to Session (WIP) * refactor: TS port for Actions and Innertube My fingers hurt from typing out all those types :-P * refactor: NavigationEndpoint TS this is still a WIP and should be improved. NavigationEndpoint should probably be refactored further. * refactor!: VideoInfo compiles without errors * chore: delete old player * fix: import errors It compiles and runs!! * fix: Utils import fixes * fix: several runtime errors * fix: video streaming * chore: remove console.log debugging Whoops, forgot to remove these before I pushed the previous commit * chore: remove old unused dependencies * fix: typescript errors Now emitting declarations and source maps * refactor: TS feed * chore: delete old Feed * refactor: move streamToIterable into Utils * refactor: AccountManager TS * refactor: FilterableFeed to TS * refactor: InteractionManager to TS * refactor: PlaylistManager to TS * refactor: TabbedFeed to TS * refactor: Music to TS (WIP) more work to be done, see TODO comments * fix: getting the tests to pass (6/12) YouTube.js Tests Search ✓ Should search on YouTube (1152 ms) ✕ Should search on YouTube Music (705 ms) ✕ Should retrieve YouTube search suggestions (722 ms) ✓ Should retrieve YouTube Music search suggestions (233 ms) Comments ✓ Should retrieve comments (585 ms) ✕ Should retrieve next batch of comments (221 ms) ✕ Should retrieve comment replies (1 ms) General ✕ Should retrieve playlist with YouTube (732 ms) ✓ Should retrieve home feed (838 ms) ✓ Should retrieve trending content (543 ms) ✓ Should retrieve video info (639 ms) ✕ Should download video (5 ms) * fix: tests (7/12) YouTube.js Tests Search ✓ Should search on YouTube (1984 ms) ✕ Should search on YouTube Music (1139 ms) ✕ Should retrieve YouTube search suggestions (1433 ms) ✓ Should retrieve YouTube Music search suggestions (529 ms) Comments ✓ Should retrieve comments (324 ms) ✓ Should retrieve next batch of comments (395 ms) ✕ Should retrieve comment replies General ✕ Should retrieve playlist with YouTube (653 ms) ✓ Should retrieve home feed (1085 ms) ✓ Should retrieve trending content (513 ms) ✓ Should retrieve video info (921 ms) ✕ Should download video (3 ms) * fix: download tests (8/12) YouTube.js Tests Search ✓ Should search on YouTube (1293 ms) ✕ Should search on YouTube Music (927 ms) ✕ Should retrieve YouTube search suggestions (1250 ms) ✓ Should retrieve YouTube Music search suggestions (258 ms) Comments ✓ Should retrieve comments (803 ms) ✓ Should retrieve next batch of comments (511 ms) ✕ Should retrieve comment replies General ✕ Should retrieve playlist with YouTube (528 ms) ✓ Should retrieve home feed (1047 ms) ✓ Should retrieve trending content (548 ms) ✓ Should retrieve video info (825 ms) ✓ Should download video (1779 ms) * fix: tests (9/12) YouTube.js Tests Search ✓ Should search on YouTube (1276 ms) ✕ Should search on YouTube Music (955 ms) ✓ Should retrieve YouTube search suggestions (661 ms) ✓ Should retrieve YouTube Music search suggestions (491 ms) Comments ✓ Should retrieve comments (624 ms) ✓ Should retrieve next batch of comments (353 ms) ✕ Should retrieve comment replies General ✕ Should retrieve playlist with YouTube (672 ms) ✓ Should retrieve home feed (1277 ms) ✓ Should retrieve trending content (999 ms) ✓ Should retrieve video info (1106 ms) ✓ Should download video (2514 ms) * feat: key based type validation for parsers * fix: comments tests pass (10/12) YouTube.js Tests Search ✓ Should search on YouTube (938 ms) ✕ Should search on YouTube Music (850 ms) ✓ Should retrieve YouTube search suggestions (528 ms) ✓ Should retrieve YouTube Music search suggestions (224 ms) Comments ✓ Should retrieve comments (518 ms) ✓ Should retrieve next batch of comments (337 ms) ✓ Should retrieve comment replies (358 ms) General ✕ Should retrieve playlist with YouTube (466 ms) ✓ Should retrieve home feed (1051 ms) ✓ Should retrieve trending content (623 ms) ✓ Should retrieve video info (863 ms) ✓ Should download video (2656 ms) * refactor: type safety checks removing @ts-ignore * fix: playlist tests pass (11/12) YouTube.js Tests Search ✓ Should search on YouTube (991 ms) ✕ Should search on YouTube Music (924 ms) ✓ Should retrieve YouTube search suggestions (606 ms) ✓ Should retrieve YouTube Music search suggestions (225 ms) Comments ✓ Should retrieve comments (393 ms) ✓ Should retrieve next batch of comments (284 ms) ✓ Should retrieve comment replies (252 ms) General ✓ Should retrieve playlist with YouTube (578 ms) ✓ Should retrieve home feed (1148 ms) ✓ Should retrieve trending content (541 ms) ✓ Should retrieve video info (799 ms) ✓ Should download video (1419 ms) * fix: all tests pass for node 🎉 YouTube.js Tests Search ✓ Should search on YouTube (1053 ms) ✓ Should search on YouTube Music (761 ms) ✓ Should retrieve YouTube search suggestions (453 ms) ✓ Should retrieve YouTube Music search suggestions (221 ms) Comments ✓ Should retrieve comments (627 ms) ✓ Should retrieve next batch of comments (412 ms) ✓ Should retrieve comment replies (268 ms) General ✓ Should retrieve playlist with YouTube (565 ms) ✓ Should retrieve home feed (775 ms) ✓ Should retrieve trending content (498 ms) ✓ Should retrieve video info (875 ms) ✓ Should download video (1364 ms) * build: working Deno bundle Still need to test whether this bundle works in the browser * docs: update deno example to download video * refactor: MusicResponsiveListItem to TS * docs: TSDoc for Parser helpers * docs: Parser documentation for TS * docs: add note about parseItem and parseArray * test: remove browser tests since they're identical * feat: browser support and proxy example * fix: PlaylistManager TS after merge * feat: in-browser video streaming * refactor: cleanup the Dash example * feat: allow custom fetch implementations * feat: fetch debugger * fix: OAuth login * refactor: remove file extensions from imports * refactor: build scripts * fix: CustomEvent on node * fix: LiveChat * fix: linting * fix: liniting in build-parser-json * chore: update test workflow * fix: NToken errors after lint fixes * fix: codacy complaints * docs: update to reflect changes Definitly needs more work but its a start * refactor: cleanup imports/exports * fix: browser example - Remove user-agent before making request. - Fix cache on browsers * fix: cache on node * fix: stupid mistake * refactor: Session#signIn to wait untill success This also splits the 'auth' event up into 3 distinct events: - 'auth' -> fired on success - 'auth-pending' -> fired when pending authentication - 'auth-error' -> fired when an error occurred * refactor: freeze Constants * refactor: cleanup HTTPClient Request * refactor: debugFetch readability * chore: lint * refactor: replace jsdoc with tsdoc eslint plugin remove @param annotations without descriptions * fix: bunch of liniting warnings * refactor: better inference on YTNode#is As suggested by @MasterOfBob777 * fix: linting warnings * revert: undici import * refactor: rename `list_type` to `item_type`
221 lines
6.4 KiB
TypeScript
221 lines
6.4 KiB
TypeScript
import Player from './Player';
|
|
import Proto from '../proto/index';
|
|
import { DeviceCategory, generateRandomString, getRandomUserAgent, InnertubeError, SessionError } from '../utils/Utils';
|
|
import Constants from '../utils/Constants';
|
|
import UniversalCache from '../utils/Cache';
|
|
import OAuth, { Credentials, OAuthAuthErrorEventHandler, OAuthAuthEventHandler, OAuthAuthPendingEventHandler } from './OAuth';
|
|
import EventEmitterLike from '../utils/EventEmitterLike';
|
|
import HTTPClient, { FetchFunction } from '../utils/HTTPClient';
|
|
import Actions from './Actions';
|
|
|
|
export interface Context {
|
|
client: {
|
|
hl: string;
|
|
gl: string;
|
|
remoteHost: string;
|
|
visitorData: string;
|
|
userAgent: string;
|
|
clientName: string;
|
|
clientVersion: string;
|
|
osName: string;
|
|
osVersion: string;
|
|
platform: string;
|
|
clientFormFactor: string;
|
|
userInterfaceTheme: string;
|
|
timeZone: string;
|
|
browserName: string;
|
|
browserVersion: string;
|
|
originalUrl: string;
|
|
deviceMake: string;
|
|
deviceModel: string;
|
|
utcOffsetMinutes: number;
|
|
};
|
|
user: {
|
|
lockedSafetyMode: false;
|
|
};
|
|
request: {
|
|
useSsl: true;
|
|
};
|
|
}
|
|
|
|
export enum ClientType {
|
|
WEB = 'WEB',
|
|
MUSIC = 'WEB_REMIX',
|
|
ANDROID = 'ANDROID',
|
|
}
|
|
|
|
export interface SessionOptions {
|
|
lang?: string;
|
|
device_category?: DeviceCategory;
|
|
client_type?: ClientType;
|
|
timezone?: string;
|
|
cache?: UniversalCache;
|
|
cookie?: string;
|
|
fetch?: FetchFunction;
|
|
}
|
|
|
|
export default class Session extends EventEmitterLike {
|
|
#api_version;
|
|
#key;
|
|
#context;
|
|
#player;
|
|
oauth;
|
|
http;
|
|
logged_in;
|
|
actions;
|
|
constructor(context: Context, api_key: string, api_version: string, player: Player, cookie?: string, fetch?: FetchFunction) {
|
|
super();
|
|
this.#context = context;
|
|
this.#key = api_key;
|
|
this.#api_version = api_version;
|
|
this.#player = player;
|
|
this.http = new HTTPClient(this, cookie, fetch);
|
|
this.actions = new Actions(this);
|
|
this.oauth = new OAuth(this);
|
|
this.logged_in = !!cookie;
|
|
}
|
|
on(type: 'auth', listener: OAuthAuthEventHandler): void;
|
|
on(type: 'auth-pending', listener: OAuthAuthPendingEventHandler): void;
|
|
on(type: 'auth-error', listener: OAuthAuthErrorEventHandler): void;
|
|
on(type: string, listener: (...args: any[]) => void): void {
|
|
super.on(type, listener);
|
|
}
|
|
once(type: 'auth', listener: OAuthAuthEventHandler): void;
|
|
once(type: 'auth-pending', listener: OAuthAuthPendingEventHandler): void;
|
|
once(type: 'auth-error', listener: OAuthAuthErrorEventHandler): void;
|
|
once(type: string, listener: (...args: any[]) => void): void {
|
|
super.once(type, listener);
|
|
}
|
|
async signIn(credentials?: Credentials): Promise<void> {
|
|
return new Promise(async (resolve, reject) => {
|
|
const error_handler: OAuthAuthErrorEventHandler = (err) => {
|
|
reject(err);
|
|
};
|
|
this.once('auth', (data) => {
|
|
this.off('auth-error', error_handler);
|
|
if (data.status === 'SUCCESS') {
|
|
this.logged_in = true;
|
|
resolve();
|
|
} else
|
|
reject(data);
|
|
});
|
|
this.once('auth-error', error_handler);
|
|
try {
|
|
await this.oauth.init(credentials);
|
|
if (this.oauth.validateCredentials()) {
|
|
await this.oauth.checkAccessTokenValidity();
|
|
this.logged_in = true;
|
|
resolve();
|
|
}
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
});
|
|
}
|
|
async signOut() {
|
|
if (!this.logged_in)
|
|
throw new InnertubeError('You are not signed in');
|
|
const response = await this.oauth.revokeCredentials();
|
|
this.logged_in = false;
|
|
return response;
|
|
}
|
|
static async create(options: SessionOptions = {}) {
|
|
const { context, api_key, api_version } = await Session.getSessionData(options.lang, options.device_category, options.client_type, options.timezone, options.fetch);
|
|
return new Session(context, api_key, api_version, await Player.create(options.cache, options.fetch), options.cookie, options.fetch);
|
|
}
|
|
static async getSessionData(
|
|
lang = 'en-US',
|
|
deviceCategory: DeviceCategory = 'desktop',
|
|
clientName: ClientType = ClientType.WEB,
|
|
tz: string = Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
fetch: FetchFunction = globalThis.fetch
|
|
) {
|
|
const url = new URL('/sw.js_data', Constants.URLS.YT_BASE);
|
|
|
|
const res = await fetch(url, {
|
|
headers: {
|
|
'accept-language': lang,
|
|
'user-agent': getRandomUserAgent('desktop').userAgent,
|
|
'accept': '*/*',
|
|
'referer': 'https://www.youtube.com/sw.js',
|
|
'cookie': `PREF=tz=${tz.replace('/', '.')}`
|
|
}
|
|
});
|
|
|
|
if (!res.ok) {
|
|
throw new SessionError(`Failed to get session data: ${res.status}`);
|
|
}
|
|
|
|
const text = await res.text();
|
|
|
|
const data = JSON.parse(text.replace(/^\)\]\}'/, ''));
|
|
|
|
const ytcfg = data[0][2];
|
|
|
|
const api_version = `v${ytcfg[0][0][6]}`;
|
|
|
|
const [ [ device_info ], api_key ] = ytcfg;
|
|
|
|
const id = generateRandomString(11);
|
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
const visitor_data = Proto.encodeVisitorData(id, timestamp);
|
|
|
|
const context: Context = {
|
|
client: {
|
|
hl: device_info[0],
|
|
gl: device_info[2],
|
|
remoteHost: device_info[3],
|
|
visitorData: visitor_data,
|
|
userAgent: device_info[14],
|
|
clientName,
|
|
clientVersion: device_info[16],
|
|
osName: device_info[17],
|
|
osVersion: device_info[18],
|
|
platform: deviceCategory.toUpperCase(),
|
|
clientFormFactor: 'UNKNOWN_FORM_FACTOR',
|
|
userInterfaceTheme: 'USER_INTERFACE_THEME_LIGHT',
|
|
timeZone: device_info[79],
|
|
browserName: device_info[86],
|
|
browserVersion: device_info[87],
|
|
originalUrl: Constants.URLS.API.BASE,
|
|
deviceMake: device_info[11],
|
|
deviceModel: device_info[12],
|
|
utcOffsetMinutes: new Date().getTimezoneOffset()
|
|
},
|
|
user: {
|
|
lockedSafetyMode: false
|
|
},
|
|
request: {
|
|
useSsl: true
|
|
}
|
|
};
|
|
|
|
return {
|
|
context,
|
|
api_key,
|
|
api_version
|
|
};
|
|
}
|
|
get key() {
|
|
return this.#key;
|
|
}
|
|
get api_version() {
|
|
return this.#api_version;
|
|
}
|
|
get client_version() {
|
|
return this.#context.client.clientVersion;
|
|
}
|
|
get client_name() {
|
|
return this.#context.client.clientName;
|
|
}
|
|
get context() {
|
|
return this.#context;
|
|
}
|
|
get player() {
|
|
return this.#player;
|
|
}
|
|
get lang() {
|
|
return this.#context.client.hl;
|
|
}
|
|
}
|