style: run deno fmt
This commit is contained in:
parent
9de6fdc399
commit
77a86d4f88
5 changed files with 128 additions and 102 deletions
|
@ -16,11 +16,12 @@ if (Deno.env.get("YT_PLAYER_REQ_LOCATION")) {
|
|||
}
|
||||
const { youtubePlayerReq } = await import(youtubePlayerReqLocation);
|
||||
|
||||
const videoCachePath = Deno.env.get("VIDEO_CACHE_PATH") as string || "/var/tmp/youtubei.js/video_cache"
|
||||
const videoCachePath = Deno.env.get("VIDEO_CACHE_PATH") as string ||
|
||||
"/var/tmp/youtubei.js/video_cache";
|
||||
|
||||
export let kv : Deno.Kv
|
||||
export let kv: Deno.Kv;
|
||||
if ((Deno.env.get("VIDEO_CACHE_ON_DISK")?.toLowerCase() ?? false) == "true") {
|
||||
console.log("[INFO] Storing video cache on disk")
|
||||
console.log("[INFO] Storing video cache on disk");
|
||||
kv = await Deno.openKv(videoCachePath);
|
||||
} else {
|
||||
kv = await Deno.openKv();
|
||||
|
@ -145,13 +146,16 @@ export const youtubePlayerParsing = async (
|
|||
invidiousCompanion: {
|
||||
"baseUrl": Deno.env.get("SERVER_BASE_URL") ||
|
||||
konfigStore.get("server.base_url") as string,
|
||||
"external_videoplayback_proxy": Deno.env.get("EXTERNAL_VIDEOPLAYBACK_PROXY") ||
|
||||
konfigStore.get("networking.external_videoplayback_proxy") as string,
|
||||
"external_videoplayback_proxy":
|
||||
Deno.env.get("EXTERNAL_VIDEOPLAYBACK_PROXY") ||
|
||||
konfigStore.get(
|
||||
"networking.external_videoplayback_proxy",
|
||||
) as string,
|
||||
},
|
||||
}))(videoData);
|
||||
|
||||
if (videoData.playabilityStatus?.status == "OK") {
|
||||
successfulRequests.inc()
|
||||
successfulRequests.inc();
|
||||
if (
|
||||
cacheEnabled == true
|
||||
) {
|
||||
|
@ -170,7 +174,7 @@ export const youtubePlayerParsing = async (
|
|||
})();
|
||||
}
|
||||
} else {
|
||||
failedRequests.inc()
|
||||
failedRequests.inc();
|
||||
}
|
||||
|
||||
return videoOnlyNecessaryInfo;
|
||||
|
|
|
@ -3,7 +3,7 @@ import type { BgConfig } from "bgutils";
|
|||
import { JSDOM } from "jsdom";
|
||||
import { Innertube, UniversalCache } from "youtubei.js";
|
||||
import { Store } from "@willsoto/node-konfig-core";
|
||||
import { poTokenFail, externalTokenGeneratorFail } from "../../routes/index.ts";
|
||||
import { externalTokenGeneratorFail, poTokenFail } from "../../routes/index.ts";
|
||||
let getFetchClientLocation = "getFetchClient";
|
||||
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
|
||||
if (Deno.env.has("DENO_COMPILED")) {
|
||||
|
@ -43,14 +43,18 @@ export const poTokenGenerate = async (
|
|||
externalTokenGeneratorKey != undefined
|
||||
) {
|
||||
response = await fetch(
|
||||
`${externalTokenGenerator}/generate`, {
|
||||
`${externalTokenGenerator}/generate`,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${externalTokenGeneratorKey}`
|
||||
}
|
||||
}
|
||||
"Authorization":
|
||||
`Bearer ${externalTokenGeneratorKey}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
if (response.status == 401) {
|
||||
throw new Error(`Key '${externalTokenGeneratorKey}' is invalid!`)
|
||||
throw new Error(
|
||||
`Key '${externalTokenGeneratorKey}' is invalid!`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
response = await fetch(
|
||||
|
@ -71,7 +75,7 @@ export const poTokenGenerate = async (
|
|||
e,
|
||||
);
|
||||
console.log("poTokenGenerate: Using built-in token generator");
|
||||
externalTokenGeneratorFail.inc()
|
||||
externalTokenGeneratorFail.inc();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,7 +106,7 @@ export const poTokenGenerate = async (
|
|||
const bgChallenge = await BG.Challenge.create(bgConfig);
|
||||
|
||||
if (!bgChallenge) {
|
||||
poTokenFail.inc()
|
||||
poTokenFail.inc();
|
||||
throw new Error("Could not get challenge");
|
||||
}
|
||||
|
||||
|
@ -111,8 +115,8 @@ export const poTokenGenerate = async (
|
|||
|
||||
if (interpreterJavascript) {
|
||||
new Function(interpreterJavascript)();
|
||||
} else {
|
||||
poTokenFail.inc()
|
||||
} else {
|
||||
poTokenFail.inc();
|
||||
throw new Error("Could not load VM");
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import { Hono } from "hono";
|
|||
import { logger } from "hono/logger";
|
||||
import { Store } from "@willsoto/node-konfig-core";
|
||||
import { bearerAuth } from "hono/bearer-auth";
|
||||
import { Registry, Counter, Gauge } from "prom-client"
|
||||
import { Counter, Gauge, Registry } from "prom-client";
|
||||
|
||||
import youtubeApiPlayer from "./youtube_api_routes/player.ts";
|
||||
import invidiousRouteLatestVersion from "./invidious_routes/latestVersion.ts";
|
||||
|
@ -14,58 +14,57 @@ const METRICS_PREFIX = "invidious_companion_";
|
|||
export const register = new Registry();
|
||||
|
||||
export const externalTokenGeneratorFail = new Counter({
|
||||
name: `${METRICS_PREFIX}externaltokengenerator_fail`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
});
|
||||
name: `${METRICS_PREFIX}externaltokengenerator_fail`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const cachedEntries = new Gauge({
|
||||
name: `${METRICS_PREFIX}cached_entries`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
});
|
||||
name: `${METRICS_PREFIX}cached_entries`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const subreasonProtectCommunity = new Counter({
|
||||
name: `${METRICS_PREFIX}subreason_protect_community`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
});
|
||||
name: `${METRICS_PREFIX}subreason_protect_community`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const poTokenFail = new Counter({
|
||||
name: `${METRICS_PREFIX}potoken_fail`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
});
|
||||
name: `${METRICS_PREFIX}potoken_fail`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const reasonBot = new Counter({
|
||||
name: `${METRICS_PREFIX}reason_bot`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
});
|
||||
name: `${METRICS_PREFIX}reason_bot`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const videoUnavailable = new Counter({
|
||||
name: `${METRICS_PREFIX}video_unavailable`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
});
|
||||
name: `${METRICS_PREFIX}video_unavailable`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const videoRestricted = new Counter({
|
||||
name: `${METRICS_PREFIX}video_restricted`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
});
|
||||
|
||||
name: `${METRICS_PREFIX}video_restricted`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const failedRequests = new Counter({
|
||||
name: `${METRICS_PREFIX}failed_requests`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
name: `${METRICS_PREFIX}failed_requests`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const successfulRequests = new Counter({
|
||||
name: `${METRICS_PREFIX}successful_requests`,
|
||||
help: 'TODO',
|
||||
registers: [register]
|
||||
name: `${METRICS_PREFIX}successful_requests`,
|
||||
help: "TODO",
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
import health from "./health.ts";
|
||||
|
@ -88,6 +87,6 @@ export const routes = (
|
|||
app.route("/latest_version", invidiousRouteLatestVersion);
|
||||
app.route("/api/manifest/dash/id", invidiousRouteDashManifest);
|
||||
app.route("/videoplayback", videoPlaybackProxy);
|
||||
app.route("/metrics", metrics)
|
||||
app.route("/metrics", metrics);
|
||||
app.route("/healthz", health);
|
||||
};
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
import { Hono } from "hono";
|
||||
import { register, cachedEntries } from "./index.ts";
|
||||
import { kv } from "../lib/helpers/youtubePlayerHandling.ts"
|
||||
import { cachedEntries, register } from "./index.ts";
|
||||
import { kv } from "../lib/helpers/youtubePlayerHandling.ts";
|
||||
|
||||
const metrics = new Hono();
|
||||
|
||||
|
||||
metrics.get("/", async () => {
|
||||
let i = 0;
|
||||
const entries = kv.list({ prefix: ["video_cache"] })
|
||||
for await (const _ of entries) {
|
||||
i += 1
|
||||
}
|
||||
cachedEntries.set(i)
|
||||
return new Response(await register.metrics(), {
|
||||
headers: { "Content-Type": "text/plain" },
|
||||
});
|
||||
let i = 0;
|
||||
const entries = kv.list({ prefix: ["video_cache"] });
|
||||
for await (const _ of entries) {
|
||||
i += 1;
|
||||
}
|
||||
cachedEntries.set(i);
|
||||
return new Response(await register.metrics(), {
|
||||
headers: { "Content-Type": "text/plain" },
|
||||
});
|
||||
});
|
||||
|
||||
export default metrics;
|
||||
|
|
|
@ -3,49 +3,68 @@ import { youtubePlayerParsing } from "../../lib/helpers/youtubePlayerHandling.ts
|
|||
import { HonoVariables } from "../../lib/types/HonoVariables.ts";
|
||||
import { Innertube } from "youtubei.js";
|
||||
import { Store } from "@willsoto/node-konfig-core";
|
||||
import { reasonBot, subreasonProtectCommunity, videoRestricted, videoUnavailable } from "../index.ts";
|
||||
import {
|
||||
reasonBot,
|
||||
subreasonProtectCommunity,
|
||||
videoRestricted,
|
||||
videoUnavailable,
|
||||
} from "../index.ts";
|
||||
|
||||
const player = new Hono<{ Variables: HonoVariables }>();
|
||||
|
||||
const errors = [
|
||||
{
|
||||
// @ts-ignore: Property 'playabilityStatus' does not exist on type 'object'
|
||||
check: (yt: object) => yt.playabilityStatus?.reason?.includes("Sign in to confirm you’re not a bot"),
|
||||
action: () => reasonBot.inc(),
|
||||
},
|
||||
{
|
||||
// @ts-ignore: Property 'playabilityStatus' does not exist on type 'object'
|
||||
check: (yt: object) => yt.playabilityStatus?.errorScreen?.playerErrorMessageRenderer?.subreason?.runs?.[0]?.text?.includes("This helps protect our community"),
|
||||
action: () => subreasonProtectCommunity.inc(),
|
||||
},
|
||||
{
|
||||
// @ts-ignore: Property 'playabilityStatus' does not exist on type 'object'
|
||||
check: (yt: object) => yt.playabilityStatus?.errorScreen?.playerErrorMessageRenderer?.subreason?.runs?.[0]?.text?.includes("Video unavailable"),
|
||||
action: () => videoUnavailable.inc(),
|
||||
},
|
||||
{
|
||||
// @ts-ignore: Property 'playabilityStatus' does not exist on type 'object'
|
||||
check: (yt: object) => yt.playabilityStatus?.reason?.includes("This video is restricted"),
|
||||
action: () => videoRestricted.inc(),
|
||||
},
|
||||
{
|
||||
check: (yt: object) =>
|
||||
// @ts-ignore: Property 'playabilityStatus' does not exist on type 'object'
|
||||
yt.playabilityStatus?.reason?.includes(
|
||||
"Sign in to confirm you’re not a bot",
|
||||
),
|
||||
action: () => reasonBot.inc(),
|
||||
},
|
||||
{
|
||||
check: (yt: object) =>
|
||||
// @ts-ignore: Property 'playabilityStatus' does not exist on type 'object'
|
||||
yt.playabilityStatus?.errorScreen?.playerErrorMessageRenderer
|
||||
?.subreason?.runs?.[0]?.text?.includes(
|
||||
"This helps protect our community",
|
||||
),
|
||||
action: () => subreasonProtectCommunity.inc(),
|
||||
},
|
||||
{
|
||||
check: (yt: object) =>
|
||||
// @ts-ignore: Property 'playabilityStatus' does not exist on type 'object'
|
||||
yt.playabilityStatus?.errorScreen?.playerErrorMessageRenderer
|
||||
?.subreason?.runs?.[0]?.text?.includes("Video unavailable"),
|
||||
action: () => videoUnavailable.inc(),
|
||||
},
|
||||
{
|
||||
check: (yt: object) =>
|
||||
// @ts-ignore: Property 'playabilityStatus' does not exist on type 'object'
|
||||
yt.playabilityStatus?.reason?.includes("This video is restricted"),
|
||||
action: () => videoRestricted.inc(),
|
||||
},
|
||||
];
|
||||
|
||||
player.post("/player", async (c) => {
|
||||
const jsonReq = await c.req.json();
|
||||
const innertubeClient = await c.get("innertubeClient") as Innertube;
|
||||
// @ts-ignore Do not understand how to fix this error.
|
||||
const konfigStore = await c.get("konfigStore") as Store<
|
||||
Record<string, unknown>
|
||||
>;
|
||||
if (jsonReq.videoId) {
|
||||
const yt = await youtubePlayerParsing(innertubeClient, jsonReq.videoId, konfigStore)
|
||||
errors.forEach((error) => {
|
||||
if (error.check(yt)) {
|
||||
error.action()
|
||||
}
|
||||
})
|
||||
return c.json(yt);
|
||||
}
|
||||
const jsonReq = await c.req.json();
|
||||
const innertubeClient = await c.get("innertubeClient") as Innertube;
|
||||
// @ts-ignore Do not understand how to fix this error.
|
||||
const konfigStore = await c.get("konfigStore") as Store<
|
||||
Record<string, unknown>
|
||||
>;
|
||||
if (jsonReq.videoId) {
|
||||
const yt = await youtubePlayerParsing(
|
||||
innertubeClient,
|
||||
jsonReq.videoId,
|
||||
konfigStore,
|
||||
);
|
||||
errors.forEach((error) => {
|
||||
if (error.check(yt)) {
|
||||
error.action();
|
||||
}
|
||||
});
|
||||
return c.json(yt);
|
||||
}
|
||||
});
|
||||
|
||||
export default player;
|
||||
|
|
Reference in a new issue