diff --git a/.npmignore b/.npmignore index 7dac574..2bceece 100644 --- a/.npmignore +++ b/.npmignore @@ -8,4 +8,5 @@ CHANGELOG.md Rotator.md docs examples/ -.prettierignore \ No newline at end of file +.prettierignore +.github/ \ No newline at end of file diff --git a/lib/Extractor/Youtube.ts b/lib/Extractor/Youtube.ts index b3002ae..81d0a21 100644 --- a/lib/Extractor/Youtube.ts +++ b/lib/Extractor/Youtube.ts @@ -30,8 +30,7 @@ import type { import { streamFromYT } from "../common/generateYTStream"; import { AsyncLocalStorage } from "node:async_hooks"; import { tokenToObject } from "../common/tokenUtils"; -import { createReadableFromWeb } from "../common/webToReadable"; -import { type GeneratorReturnData } from "../utils"; +import { createReadableFromWeb } from "../common/webToReadable" export interface StreamOptions { useClient?: InnerTubeClient; @@ -43,7 +42,10 @@ export interface RefreshInnertubeOptions { interval?: number; } -export type TrustedTokenConfig = GeneratorReturnData; +export type TrustedTokenConfig = { + poToken: string, + visitorData: string +}; export interface YoutubeiOptions { authentication?: string; diff --git a/lib/utils/index.ts b/lib/utils/index.ts index af2d952..b925980 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -1,5 +1,3 @@ export * from "./live/getLiveChat"; export { LiveChatEvents } from "./live/LiveChat"; export { ChatMessageType } from "./live/LiveChatMessage"; -export * from "./poTokenGenerator/generatePoToken"; -export * from "./poTokenGenerator/generatePoTokenInterval"; diff --git a/lib/utils/poTokenGenerator/generatePoToken.ts b/lib/utils/poTokenGenerator/generatePoToken.ts deleted file mode 100644 index df3b8f4..0000000 --- a/lib/utils/poTokenGenerator/generatePoToken.ts +++ /dev/null @@ -1,90 +0,0 @@ -import type { PuppeteerLaunchOptions } from "puppeteer"; - -async function hasPuppeteer() { - try { - await import("puppeteer"); - return true; - } catch { - return false; - } -} - -export interface GeneratorReturnData { - visitorData: string; - poToken: string; -} - -export interface GeneratorOptions { - puppeteerOptions?: Omit; - timeout?: number; - embeddedVideoUrl?: string; - skipPuppeteerCheck?: boolean; -} - -export async function generateTrustedToken(options: GeneratorOptions = {}) { - if (!options.skipPuppeteerCheck && !(await hasPuppeteer())) - throw new Error("ERR_NO_DEP: Puppeteer not found"); - if ( - options.embeddedVideoUrl && - !options.embeddedVideoUrl.startsWith("https://www.youtube.com/embed/") - ) - throw new Error("ERR_INVALID_YT_EMBED: That is not a valid youtube embed"); - - return new Promise(async (resolve, reject) => { - const puppet = await import("puppeteer"); - - const browser = await puppet.launch({ - ...options.puppeteerOptions, - headless: false, - }); - - const page = await browser.newPage(); - - const client = await page.createCDPSession(); - await client.send("Debugger.enable"); - await client.send("Debugger.setAsyncCallStackDepth", { maxDepth: 32 }); - await client.send("Network.enable"); - - const timeout = setTimeout(() => { - client.removeAllListeners(); - reject( - "ERR_PUPPETEER_TIMEOUT: Timeout exceeded. Use GeneratorOptions.timeout to increase it", - ); - }, options.timeout ?? 10_000).unref(); - - client.on("Network.requestWillBeSent", (e) => { - if (e.request.url.includes("/youtubei/v1/player")) { - const jsonData = JSON.parse(e.request.postData!); - - // cleanup - browser.close(); - client.removeAllListeners(); - - clearTimeout(timeout); - - if ( - !jsonData["serviceIntegrityDimensions"]["poToken"] || - !jsonData["context"]["client"]["visitorData"] - ) - reject("Unable to get poToken or visitorData"); - - resolve({ - poToken: jsonData["serviceIntegrityDimensions"]["poToken"], - visitorData: jsonData["context"]["client"]["visitorData"], - }); - } - }); - - await page.goto( - options.embeddedVideoUrl ?? "https://www.youtube.com/embed/jNQXAC9IVRw", - { - waitUntil: "networkidle2", - }, - ); - - // Start playing the video - const playButton = (await page.$("#movie_player"))!; - - await playButton.click(); - }); -} diff --git a/lib/utils/poTokenGenerator/generatePoTokenInterval.ts b/lib/utils/poTokenGenerator/generatePoTokenInterval.ts deleted file mode 100644 index a238093..0000000 --- a/lib/utils/poTokenGenerator/generatePoTokenInterval.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { - type GeneratorOptions, - type GeneratorReturnData, - generateTrustedToken, -} from "./generatePoToken"; - -export interface IGeneratorOptions { - interval?: number; - onGenerate: (data: GeneratorReturnData) => any; - generateInstant?: boolean; - onError: (e: any) => any; -} - -export type IntervalGeneratorOptions = IGeneratorOptions & - Omit; - -export async function generateTrustedTokenInterval( - options: IntervalGeneratorOptions, -) { - if (!options.interval) options.interval = 6.048e8; // 1 week in ms - - if (options.generateInstant) { - const tokens = await generateTrustedToken({ - ...options, - skipPuppeteerCheck: false, - }).catch((e) => e); - - if (tokens instanceof Error) { - if (!options.onError) throw tokens; - else options.onError(tokens); - } else { - options.onGenerate(tokens); - } - } - - // non blocking interval - const interval = setInterval(async () => { - const tokens = await generateTrustedToken({ - ...options, - skipPuppeteerCheck: true, - }).catch((e) => e); - - if (tokens instanceof Error) { - if (!options.onError) throw tokens; - else options.onError(tokens); - } else { - options.onGenerate(tokens); - } - }, options.interval); - - const background = () => interval.unref(); - const foreground = () => interval.ref(); - const stop = () => clearInterval(interval); - - return { - background, - foreground, - stop, - }; -} diff --git a/package.json b/package.json index e22906a..f832fc4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-player-youtubei", - "version": "1.3.2", + "version": "1.3.3-beta.1", "description": "An unofficial package to test the use of youtubei in discord-player v6.", "main": "dist/index.js", "repository": "https://github.com/retrouser955/discord-player-youtubei",