trying to improve again dynamic import

This commit is contained in:
Emilien 2024-12-12 23:28:49 +01:00
parent 57f4da848b
commit f3fff5cc09
5 changed files with 103 additions and 77 deletions

1
compile.env Normal file
View file

@ -0,0 +1 @@
DENO_COMPILED=true

View file

@ -4,8 +4,14 @@ import { compress, decompress } from "https://deno.land/x/brotli@0.1.7/mod.ts";
import { Store } from "@willsoto/node-konfig-core";
let youtubePlayerReqLocation = "youtubePlayerReq";
if (Deno.env.get("YT_PLAYER_REQ_LOCATION")) {
youtubePlayerReqLocation = import.meta.dirname + "/" +
Deno.env.get("YT_PLAYER_REQ_LOCATION");
if (Deno.env.has("DENO_COMPILED")) {
youtubePlayerReqLocation = Deno.mainModule.replace("main.ts", "") +
Deno.env.get("YT_PLAYER_REQ_LOCATION");
} else {
youtubePlayerReqLocation = Deno.env.get(
"YT_PLAYER_REQ_LOCATION",
) as string;
}
}
const { youtubePlayerReq } = await import(youtubePlayerReqLocation);

View file

@ -5,8 +5,14 @@ import { Innertube, UniversalCache } from "youtubei.js";
import { Store } from "@willsoto/node-konfig-core";
let getFetchClientLocation = "getFetchClient";
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
getFetchClientLocation = import.meta.dirname + "/" +
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
if (Deno.env.has("DENO_COMPILED")) {
getFetchClientLocation = Deno.mainModule.replace("main.ts", "") +
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
} else {
getFetchClientLocation = Deno.env.get(
"GET_FETCH_CLIENT_LOCATION",
) as string;
}
}
const { getFetchClient } = await import(getFetchClientLocation);
@ -14,7 +20,7 @@ const { getFetchClient } = await import(getFetchClientLocation);
export const poTokenGenerate = async (
innertubeClient: Innertube,
konfigStore: Store<Record<string, unknown>>,
innertubeClientCache: UniversalCache
innertubeClientCache: UniversalCache,
): Promise<Innertube> => {
const requestKey = "O43z0dpjhgX20SCx4KAo";
@ -62,7 +68,7 @@ export const poTokenGenerate = async (
bgConfig,
});
await BG.PoToken.generatePlaceholder(visitorData);;
await BG.PoToken.generatePlaceholder(visitorData);
return (await Innertube.create({
po_token: poTokenResult.poToken,

View file

@ -5,8 +5,14 @@ import { poTokenGenerate } from "./lib/jobs/potoken.ts";
import { konfigLoader } from "./lib/helpers/konfigLoader.ts";
let getFetchClientLocation = "getFetchClient";
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
getFetchClientLocation = import.meta.dirname + "/" +
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
if (Deno.env.has("DENO_COMPILED")) {
getFetchClientLocation = Deno.mainModule.replace("main.ts", "") +
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
} else {
getFetchClientLocation = Deno.env.get(
"GET_FETCH_CLIENT_LOCATION",
) as string;
}
}
const { getFetchClient } = await import(getFetchClientLocation);
@ -16,100 +22,101 @@ const konfigStore = await konfigLoader();
let innertubeClient: Innertube;
let innertubeClientFetchPlayer = true;
const innertubeClientOauthEnabled = konfigStore.get(
"youtube_session.oauth_enabled",
"youtube_session.oauth_enabled",
) as boolean;
const innertubeClientJobPoTokenEnabled = konfigStore.get(
"jobs.youtube_session.po_token_enabled",
"jobs.youtube_session.po_token_enabled",
) as boolean;
const innertubeClientCookies = konfigStore.get(
"jobs.youtube_session.cookies",
"jobs.youtube_session.cookies",
) as string;
let innertubeClientCache = new UniversalCache(
true,
konfigStore.get('cache.directory') as string + "/youtubei.js/",
true,
konfigStore.get("cache.directory") as string + "/youtubei.js/",
) as UniversalCache;
Deno.env.set('TMPDIR', konfigStore.get("cache.directory") as string)
Deno.env.set("TMPDIR", konfigStore.get("cache.directory") as string);
if (!innertubeClientOauthEnabled) {
if (innertubeClientJobPoTokenEnabled) {
console.log("[INFO] job po_token is active.");
// Don't fetch fetch player yet for po_token
innertubeClientFetchPlayer = false;
} else if (!innertubeClientJobPoTokenEnabled) {
console.log("[INFO] job po_token is NOT active.");
}
if (innertubeClientJobPoTokenEnabled) {
console.log("[INFO] job po_token is active.");
// Don't fetch fetch player yet for po_token
innertubeClientFetchPlayer = false;
} else if (!innertubeClientJobPoTokenEnabled) {
console.log("[INFO] job po_token is NOT active.");
}
} else if (innertubeClientOauthEnabled) {
// Can't use cache if using OAuth#cacheCredentials
innertubeClientCache = new UniversalCache(false);
// Can't use cache if using OAuth#cacheCredentials
innertubeClientCache = new UniversalCache(false);
}
innertubeClient = await Innertube.create({
cache: innertubeClientCache,
retrieve_player: innertubeClientFetchPlayer,
fetch: getFetchClient(konfigStore),
cookie: innertubeClientCookies || undefined,
cache: innertubeClientCache,
retrieve_player: innertubeClientFetchPlayer,
fetch: getFetchClient(konfigStore),
cookie: innertubeClientCookies || undefined,
});
if (!innertubeClientOauthEnabled) {
if (innertubeClientJobPoTokenEnabled) {
innertubeClient = await poTokenGenerate(
innertubeClient,
konfigStore,
innertubeClientCache as UniversalCache,
);
}
Deno.cron(
"regenerate youtube session",
konfigStore.get("jobs.youtube_session.frequency") as string,
async () => {
if (innertubeClientJobPoTokenEnabled) {
if (innertubeClientJobPoTokenEnabled) {
innertubeClient = await poTokenGenerate(
innertubeClient,
konfigStore,
innertubeClientCache,
innertubeClient,
konfigStore,
innertubeClientCache as UniversalCache,
);
} else {
innertubeClient = await Innertube.create({
cache: innertubeClientCache,
retrieve_player: innertubeClientFetchPlayer,
});
}
},
);
} else if (innertubeClientOauthEnabled) {
// Fired when waiting for the user to authorize the sign in attempt.
innertubeClient.session.on("auth-pending", (data) => {
console.log(
`[INFO] [OAUTH] Go to ${data.verification_url} in your browser and enter code ${data.user_code} to authenticate.`,
}
Deno.cron(
"regenerate youtube session",
konfigStore.get("jobs.youtube_session.frequency") as string,
async () => {
if (innertubeClientJobPoTokenEnabled) {
innertubeClient = await poTokenGenerate(
innertubeClient,
konfigStore,
innertubeClientCache,
);
} else {
innertubeClient = await Innertube.create({
cache: innertubeClientCache,
retrieve_player: innertubeClientFetchPlayer,
});
}
},
);
});
// Fired when authentication is successful.
innertubeClient.session.on("auth", () => {
console.log("[INFO] [OAUTH] Sign in successful!");
});
// Fired when the access token expires.
innertubeClient.session.on("update-credentials", async () => {
console.log("[INFO] [OAUTH] Credentials updated.");
await innertubeClient.session.oauth.cacheCredentials();
});
} else if (innertubeClientOauthEnabled) {
// Fired when waiting for the user to authorize the sign in attempt.
innertubeClient.session.on("auth-pending", (data) => {
console.log(
`[INFO] [OAUTH] Go to ${data.verification_url} in your browser and enter code ${data.user_code} to authenticate.`,
);
});
// Fired when authentication is successful.
innertubeClient.session.on("auth", () => {
console.log("[INFO] [OAUTH] Sign in successful!");
});
// Fired when the access token expires.
innertubeClient.session.on("update-credentials", async () => {
console.log("[INFO] [OAUTH] Credentials updated.");
await innertubeClient.session.oauth.cacheCredentials();
});
// Attempt to sign in and then cache the credentials
await innertubeClient.session.signIn();
await innertubeClient.session.oauth.cacheCredentials();
// Attempt to sign in and then cache the credentials
await innertubeClient.session.signIn();
await innertubeClient.session.oauth.cacheCredentials();
}
app.use("*", async (c, next) => {
// @ts-ignore Do not understand how to fix this error.
c.set("innertubeClient", innertubeClient);
// @ts-ignore Do not understand how to fix this error.
c.set("konfigStore", konfigStore);
await next();
// @ts-ignore Do not understand how to fix this error.
c.set("innertubeClient", innertubeClient);
// @ts-ignore Do not understand how to fix this error.
c.set("konfigStore", konfigStore);
await next();
});
routes(app, konfigStore);
Deno.serve({
port: Number(Deno.env.get("PORT")) || konfigStore.get("server.port") as number,
hostname: Deno.env.get("HOST") || konfigStore.get("server.host") as string,
port: Number(Deno.env.get("PORT")) ||
konfigStore.get("server.port") as number,
hostname: Deno.env.get("HOST") || konfigStore.get("server.host") as string,
}, app.fetch);

View file

@ -3,8 +3,14 @@ import { Store } from "@willsoto/node-konfig-core";
import { HTTPException } from "hono/http-exception";
let getFetchClientLocation = "getFetchClient";
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
getFetchClientLocation = import.meta.dirname + "/" +
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
if (Deno.env.has("DENO_COMPILED")) {
getFetchClientLocation = Deno.mainModule.replace("main.ts", "") +
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
} else {
getFetchClientLocation = Deno.env.get(
"GET_FETCH_CLIENT_LOCATION",
) as string;
}
}
const { getFetchClient } = await import(getFetchClientLocation);