invidious-companion-patches/patches/0009-feat-add-option-to-disable-potoken-generation-check.patch
Fijxu 73fc2e6ab9
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m1s
add patch for deno compiling overflow
2025-03-25 00:24:34 -03:00

139 lines
5.5 KiB
Diff

From 613a99c7a63ae08c1e11b6c286e5af1000867166 Mon Sep 17 00:00:00 2001
From: Fijxu <fijxu@nadeko.net>
Date: Mon, 24 Mar 2025 20:34:33 -0300
Subject: [PATCH 09/14] 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 a3c73be..4c04ee3 100644
--- a/config/config.example.toml
+++ b/config/config.example.toml
@@ -51,6 +51,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 05748f0..a2f6be0 100644
--- a/src/lib/helpers/config.ts
+++ b/src/lib/helpers/config.ts
@@ -39,6 +39,15 @@ 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 233ac32..c7994c5 100644
--- a/src/lib/jobs/potoken.ts
+++ b/src/lib/jobs/potoken.ts
@@ -140,46 +140,50 @@ export const poTokenGenerate = async (
instantiatedInnertubeClient.session.player = player;
instantiatedInnertubeClient.session.player.po_token = sessionPoToken;
- 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,
- 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`,
+ 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,
+ 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;
}
return {
--
2.49.0