From 58ca41be9bbb267ae6f829324f611c021e0814e1 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Mon, 24 Mar 2025 20:34:33 -0300 Subject: [PATCH 06/17] feat: add option to disable potoken generation check --- config/config.example.toml | 1 + src/lib/helpers/config.ts | 9 +++++ src/lib/jobs/potoken.ts | 80 ++++++++++++++++++++------------------ 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/config/config.example.toml b/config/config.example.toml index 8e2af1c..b1cb4ee 100644 --- a/config/config.example.toml +++ b/config/config.example.toml @@ -53,6 +53,7 @@ # [jobs.youtube_session] # po_token_enabled = true # whether to generate PO tokens # frequency = "*/5 * * * *" # frequency of PO token refresh in cron format +# po_token_check = false # whether to check if the PO token is valid or not # [youtube_session] # oauth_enabled = false diff --git a/src/lib/helpers/config.ts b/src/lib/helpers/config.ts index cd0489d..6004753 100644 --- a/src/lib/helpers/config.ts +++ b/src/lib/helpers/config.ts @@ -45,6 +45,15 @@ export const ConfigSchema = z.object({ youtube_session: z.object({ po_token_enabled: z.boolean().default(true), frequency: z.string().default("*/5 * * * *"), + po_token_check: z + .boolean() + .default( + Deno.env.get("PO_TOKEN_CHECK") === "true" + ? true + : Deno.env.get("PO_TOKEN_CHECK") === "false" + ? false + : true, + ), }).strict().default({}), }).strict().default({}), youtube_session: z.object({ diff --git a/src/lib/jobs/potoken.ts b/src/lib/jobs/potoken.ts index c2b9441..9fd4101 100644 --- a/src/lib/jobs/potoken.ts +++ b/src/lib/jobs/potoken.ts @@ -145,46 +145,50 @@ async function checkToken({ }) { const fetchImpl = getFetchClient(config); - try { - const feed = await instantiatedInnertubeClient.getTrending(); - // get all videos and shuffle them randomly to avoid using the same trending video over and over - const videos = feed.videos - .filter((video) => video.type === "Video") - .map((value) => ({ value, sort: Math.random() })) - .sort((a, b) => a.sort - b.sort) - .map(({ value }) => value); - - const video = videos.find((video) => "id" in video); - if (!video) { - throw new Error("no videos with id found in trending"); - } + if (config.jobs.youtube_session.po_token_check) { + try { + const feed = await instantiatedInnertubeClient.getTrending(); + // get all videos and shuffle them randomly to avoid using the same trending video over and over + const videos = feed.videos + .filter((video) => video.type === "Video") + .map((value) => ({ value, sort: Math.random() })) + .sort((a, b) => a.sort - b.sort) + .map(({ value }) => value); + + const video = videos.find((video) => "id" in video); + if (!video) { + throw new Error("no videos with id found in trending"); + } - const youtubePlayerResponseJson = await youtubePlayerParsing({ - innertubeClient: instantiatedInnertubeClient, - videoId: video.id, - config, - tokenMinter: integrityTokenBasedMinter, - metrics, - overrideCache: true, - }); - const videoInfo = youtubeVideoInfo( - instantiatedInnertubeClient, - youtubePlayerResponseJson, - ); - const validFormat = videoInfo.streaming_data?.adaptive_formats[0]; - if (!validFormat) { - throw new Error( - "failed to find valid video with adaptive format to check token against", - ); - } - const result = await fetchImpl(validFormat?.url, { method: "HEAD" }); - if (result.status !== 200) { - throw new Error( - `did not get a 200 when checking video, got ${result.status} instead`, + const youtubePlayerResponseJson = await youtubePlayerParsing({ + innertubeClient: instantiatedInnertubeClient, + videoId: video.id, + config, + tokenMinter: integrityTokenBasedMinter, + metrics, + overrideCache: true, + }); + const videoInfo = youtubeVideoInfo( + instantiatedInnertubeClient, + youtubePlayerResponseJson, ); + const validFormat = videoInfo.streaming_data?.adaptive_formats[0]; + if (!validFormat) { + throw new Error( + "failed to find valid video with adaptive format to check token against", + ); + } + const result = await fetchImpl(validFormat?.url, { + method: "HEAD", + }); + if (result.status !== 200) { + throw new Error( + `did not get a 200 when checking video, got ${result.status} instead`, + ); + } + } catch (err) { + console.log("Failed to get valid PO token, will retry", { err }); + throw err; } - } catch (err) { - console.log("Failed to get valid PO token, will retry", { err }); - throw err; } } -- 2.49.0