mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-23 23:09:28 +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`
150 lines
5.7 KiB
TypeScript
150 lines
5.7 KiB
TypeScript
import { getRandomUserAgent, getStringBetweenStrings, PlayerError } from '../utils/Utils';
|
|
import Constants from '../utils/Constants';
|
|
import Signature from '../deciphers/Signature';
|
|
import NToken from '../deciphers/NToken';
|
|
import UniversalCache from '../utils/Cache';
|
|
import { FetchFunction } from '../utils/HTTPClient';
|
|
|
|
export default class Player {
|
|
#ntoken;
|
|
#signature;
|
|
#signature_timestamp;
|
|
#player_id;
|
|
constructor(signature: Signature, ntoken: NToken, signature_timestamp: number, player_id: string) {
|
|
this.#ntoken = ntoken;
|
|
this.#signature = signature;
|
|
this.#signature_timestamp = signature_timestamp;
|
|
this.#player_id = player_id;
|
|
}
|
|
static async fromCache(cache: UniversalCache, player_id: string) {
|
|
const buffer = await cache.get(player_id);
|
|
if (!buffer)
|
|
return null;
|
|
const view = new DataView(buffer);
|
|
const version = view.getUint32(0, true);
|
|
if (version !== Player.LIBRARY_VERSION)
|
|
return null;
|
|
const sig_timestamp = view.getUint32(4, true);
|
|
const sig_decipher_len = view.getUint32(8, true);
|
|
const sig_decipher_buf = buffer.slice(12, 12 + sig_decipher_len);
|
|
const ntoken_transform_buf = buffer.slice(12 + sig_decipher_len);
|
|
|
|
return new Player(Signature.fromArrayBuffer(sig_decipher_buf), NToken.fromArrayBuffer(ntoken_transform_buf), sig_timestamp, player_id);
|
|
}
|
|
static async fromSource(cache: UniversalCache | undefined, sig_timestamp: number, sig_decipher_sc: string, ntoken_sc: string, player_id: string) {
|
|
const player = new Player(Signature.fromSourceCode(sig_decipher_sc), NToken.fromSourceCode(ntoken_sc), sig_timestamp, player_id);
|
|
await player.cache(cache);
|
|
return player;
|
|
}
|
|
async cache(cache?: UniversalCache) {
|
|
if (!cache)
|
|
return;
|
|
const ntokenBuf = this.#ntoken.toArrayBuffer();
|
|
const sigDecipherBuf = this.#signature.toArrayBuffer();
|
|
const buffer = new ArrayBuffer(12 + sigDecipherBuf.byteLength + ntokenBuf.byteLength);
|
|
const view = new DataView(buffer);
|
|
view.setUint32(0, Player.LIBRARY_VERSION, true);
|
|
view.setUint32(4, this.#signature_timestamp, true);
|
|
view.setUint32(8, sigDecipherBuf.byteLength, true);
|
|
new Uint8Array(buffer).set(new Uint8Array(sigDecipherBuf), 12);
|
|
new Uint8Array(buffer).set(new Uint8Array(ntokenBuf), 12 + sigDecipherBuf.byteLength);
|
|
await cache.set(this.#player_id, new Uint8Array(buffer));
|
|
}
|
|
decipher(url?: string, signature_cipher?: string, cipher?: string) {
|
|
url = url || signature_cipher || cipher;
|
|
if (!url)
|
|
throw new PlayerError('No valid URL to decipher');
|
|
const args = new URLSearchParams(url);
|
|
const url_components = new URL(args.get('url') || url);
|
|
url_components.searchParams.set('ratebypass', 'yes');
|
|
if (signature_cipher || cipher) {
|
|
const signature = this.#signature.decipher(url);
|
|
const sp = args.get('sp');
|
|
sp ?
|
|
url_components.searchParams.set(sp, signature) :
|
|
url_components.searchParams.set('signature', signature);
|
|
}
|
|
const n = url_components.searchParams.get('n');
|
|
if (n) {
|
|
const ntoken = this.#ntoken.transform(n);
|
|
url_components.searchParams.set('n', ntoken);
|
|
}
|
|
return url_components.toString();
|
|
}
|
|
get url() {
|
|
return new URL(`/s/player/${this.#player_id}/player_ias.vflset/en_US/base.js`, Constants.URLS.YT_BASE).toString();
|
|
}
|
|
get sts() {
|
|
return this.#signature_timestamp;
|
|
}
|
|
static async create(cache: UniversalCache | undefined, fetch: FetchFunction = globalThis.fetch) {
|
|
const url = new URL('/iframe_api', Constants.URLS.YT_BASE);
|
|
const res = await fetch(url);
|
|
|
|
if (res.status !== 200)
|
|
throw new PlayerError('Failed to request player id');
|
|
|
|
const js = await res.text();
|
|
|
|
const player_id = getStringBetweenStrings(js, 'player\\/', '\\/');
|
|
|
|
if (!player_id)
|
|
throw new PlayerError('Failed to get player id');
|
|
|
|
// We have the playerID now we can check if we have a cached player
|
|
if (cache) {
|
|
const cachedPlayer = await Player.fromCache(cache, player_id);
|
|
if (cachedPlayer)
|
|
return cachedPlayer;
|
|
}
|
|
|
|
const player_url = new URL(`/s/player/${player_id}/player_ias.vflset/en_US/base.js`, Constants.URLS.YT_BASE);
|
|
|
|
const player_res = await fetch(player_url, {
|
|
headers: {
|
|
'user-agent': getRandomUserAgent('desktop').userAgent
|
|
}
|
|
});
|
|
|
|
if (!player_res.ok) {
|
|
throw new PlayerError(`Failed to get player data: ${player_res.status}`);
|
|
}
|
|
|
|
const player_js = await player_res.text();
|
|
|
|
const sig_timestamp = this.extractSigTimestamp(player_js);
|
|
const sig_decipher_sc = this.extractSigDecipherSc(player_js);
|
|
const ntoken_sc = this.extractNTokenSc(player_js);
|
|
|
|
return await Player.fromSource(cache, sig_timestamp, sig_decipher_sc, ntoken_sc, player_id);
|
|
}
|
|
/**
|
|
* Extracts the signature timestamp from the player source code.
|
|
*/
|
|
static extractSigTimestamp(data: string) {
|
|
return parseInt(getStringBetweenStrings(data, 'signatureTimestamp:', ',') || '0');
|
|
}
|
|
/**
|
|
* Extracts the signature decipher algorithm.
|
|
*/
|
|
static extractSigDecipherSc(data: string) {
|
|
const sig_alg_sc = getStringBetweenStrings(data, 'this.audioTracks};var', '};');
|
|
const sig_data = getStringBetweenStrings(data, 'function(a){a=a.split("")', 'return a.join("")}');
|
|
if (!sig_alg_sc || !sig_data)
|
|
throw new PlayerError('Failed to extract signature decipher algorithm');
|
|
return sig_alg_sc + sig_data;
|
|
}
|
|
/**
|
|
* Extracts the n-token decipher algorithm.
|
|
*/
|
|
static extractNTokenSc(data: string) {
|
|
const sc = `var b=a.split("")${getStringBetweenStrings(data, 'b=a.split("")', '}return b.join("")}')}} return b.join("");`;
|
|
if (!sc)
|
|
throw new PlayerError('Failed to extract n-token decipher algorithm');
|
|
return sc;
|
|
}
|
|
static get LIBRARY_VERSION() {
|
|
return 1;
|
|
}
|
|
}
|