trying to improve again dynamic import
This commit is contained in:
parent
57f4da848b
commit
f3fff5cc09
5 changed files with 103 additions and 77 deletions
1
compile.env
Normal file
1
compile.env
Normal file
|
@ -0,0 +1 @@
|
||||||
|
DENO_COMPILED=true
|
|
@ -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";
|
import { Store } from "@willsoto/node-konfig-core";
|
||||||
let youtubePlayerReqLocation = "youtubePlayerReq";
|
let youtubePlayerReqLocation = "youtubePlayerReq";
|
||||||
if (Deno.env.get("YT_PLAYER_REQ_LOCATION")) {
|
if (Deno.env.get("YT_PLAYER_REQ_LOCATION")) {
|
||||||
youtubePlayerReqLocation = import.meta.dirname + "/" +
|
if (Deno.env.has("DENO_COMPILED")) {
|
||||||
Deno.env.get("YT_PLAYER_REQ_LOCATION");
|
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);
|
const { youtubePlayerReq } = await import(youtubePlayerReqLocation);
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,14 @@ import { Innertube, UniversalCache } from "youtubei.js";
|
||||||
import { Store } from "@willsoto/node-konfig-core";
|
import { Store } from "@willsoto/node-konfig-core";
|
||||||
let getFetchClientLocation = "getFetchClient";
|
let getFetchClientLocation = "getFetchClient";
|
||||||
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
|
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
|
||||||
getFetchClientLocation = import.meta.dirname + "/" +
|
if (Deno.env.has("DENO_COMPILED")) {
|
||||||
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
|
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);
|
const { getFetchClient } = await import(getFetchClientLocation);
|
||||||
|
|
||||||
|
@ -14,7 +20,7 @@ const { getFetchClient } = await import(getFetchClientLocation);
|
||||||
export const poTokenGenerate = async (
|
export const poTokenGenerate = async (
|
||||||
innertubeClient: Innertube,
|
innertubeClient: Innertube,
|
||||||
konfigStore: Store<Record<string, unknown>>,
|
konfigStore: Store<Record<string, unknown>>,
|
||||||
innertubeClientCache: UniversalCache
|
innertubeClientCache: UniversalCache,
|
||||||
): Promise<Innertube> => {
|
): Promise<Innertube> => {
|
||||||
const requestKey = "O43z0dpjhgX20SCx4KAo";
|
const requestKey = "O43z0dpjhgX20SCx4KAo";
|
||||||
|
|
||||||
|
@ -62,7 +68,7 @@ export const poTokenGenerate = async (
|
||||||
bgConfig,
|
bgConfig,
|
||||||
});
|
});
|
||||||
|
|
||||||
await BG.PoToken.generatePlaceholder(visitorData);;
|
await BG.PoToken.generatePlaceholder(visitorData);
|
||||||
|
|
||||||
return (await Innertube.create({
|
return (await Innertube.create({
|
||||||
po_token: poTokenResult.poToken,
|
po_token: poTokenResult.poToken,
|
||||||
|
|
145
src/main.ts
145
src/main.ts
|
@ -5,8 +5,14 @@ import { poTokenGenerate } from "./lib/jobs/potoken.ts";
|
||||||
import { konfigLoader } from "./lib/helpers/konfigLoader.ts";
|
import { konfigLoader } from "./lib/helpers/konfigLoader.ts";
|
||||||
let getFetchClientLocation = "getFetchClient";
|
let getFetchClientLocation = "getFetchClient";
|
||||||
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
|
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
|
||||||
getFetchClientLocation = import.meta.dirname + "/" +
|
if (Deno.env.has("DENO_COMPILED")) {
|
||||||
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
|
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);
|
const { getFetchClient } = await import(getFetchClientLocation);
|
||||||
|
|
||||||
|
@ -16,100 +22,101 @@ const konfigStore = await konfigLoader();
|
||||||
let innertubeClient: Innertube;
|
let innertubeClient: Innertube;
|
||||||
let innertubeClientFetchPlayer = true;
|
let innertubeClientFetchPlayer = true;
|
||||||
const innertubeClientOauthEnabled = konfigStore.get(
|
const innertubeClientOauthEnabled = konfigStore.get(
|
||||||
"youtube_session.oauth_enabled",
|
"youtube_session.oauth_enabled",
|
||||||
) as boolean;
|
) as boolean;
|
||||||
const innertubeClientJobPoTokenEnabled = konfigStore.get(
|
const innertubeClientJobPoTokenEnabled = konfigStore.get(
|
||||||
"jobs.youtube_session.po_token_enabled",
|
"jobs.youtube_session.po_token_enabled",
|
||||||
) as boolean;
|
) as boolean;
|
||||||
const innertubeClientCookies = konfigStore.get(
|
const innertubeClientCookies = konfigStore.get(
|
||||||
"jobs.youtube_session.cookies",
|
"jobs.youtube_session.cookies",
|
||||||
) as string;
|
) as string;
|
||||||
let innertubeClientCache = new UniversalCache(
|
let innertubeClientCache = new UniversalCache(
|
||||||
true,
|
true,
|
||||||
konfigStore.get('cache.directory') as string + "/youtubei.js/",
|
konfigStore.get("cache.directory") as string + "/youtubei.js/",
|
||||||
) as UniversalCache;
|
) 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 (!innertubeClientOauthEnabled) {
|
||||||
if (innertubeClientJobPoTokenEnabled) {
|
if (innertubeClientJobPoTokenEnabled) {
|
||||||
console.log("[INFO] job po_token is active.");
|
console.log("[INFO] job po_token is active.");
|
||||||
// Don't fetch fetch player yet for po_token
|
// Don't fetch fetch player yet for po_token
|
||||||
innertubeClientFetchPlayer = false;
|
innertubeClientFetchPlayer = false;
|
||||||
} else if (!innertubeClientJobPoTokenEnabled) {
|
} else if (!innertubeClientJobPoTokenEnabled) {
|
||||||
console.log("[INFO] job po_token is NOT active.");
|
console.log("[INFO] job po_token is NOT active.");
|
||||||
}
|
}
|
||||||
} else if (innertubeClientOauthEnabled) {
|
} else if (innertubeClientOauthEnabled) {
|
||||||
// Can't use cache if using OAuth#cacheCredentials
|
// Can't use cache if using OAuth#cacheCredentials
|
||||||
innertubeClientCache = new UniversalCache(false);
|
innertubeClientCache = new UniversalCache(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
innertubeClient = await Innertube.create({
|
innertubeClient = await Innertube.create({
|
||||||
cache: innertubeClientCache,
|
cache: innertubeClientCache,
|
||||||
retrieve_player: innertubeClientFetchPlayer,
|
retrieve_player: innertubeClientFetchPlayer,
|
||||||
fetch: getFetchClient(konfigStore),
|
fetch: getFetchClient(konfigStore),
|
||||||
cookie: innertubeClientCookies || undefined,
|
cookie: innertubeClientCookies || undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!innertubeClientOauthEnabled) {
|
if (!innertubeClientOauthEnabled) {
|
||||||
if (innertubeClientJobPoTokenEnabled) {
|
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) {
|
|
||||||
innertubeClient = await poTokenGenerate(
|
innertubeClient = await poTokenGenerate(
|
||||||
innertubeClient,
|
innertubeClient,
|
||||||
konfigStore,
|
konfigStore,
|
||||||
innertubeClientCache,
|
innertubeClientCache as UniversalCache,
|
||||||
);
|
);
|
||||||
} else {
|
}
|
||||||
innertubeClient = await Innertube.create({
|
Deno.cron(
|
||||||
cache: innertubeClientCache,
|
"regenerate youtube session",
|
||||||
retrieve_player: innertubeClientFetchPlayer,
|
konfigStore.get("jobs.youtube_session.frequency") as string,
|
||||||
});
|
async () => {
|
||||||
}
|
if (innertubeClientJobPoTokenEnabled) {
|
||||||
},
|
innertubeClient = await poTokenGenerate(
|
||||||
);
|
innertubeClient,
|
||||||
} else if (innertubeClientOauthEnabled) {
|
konfigStore,
|
||||||
// Fired when waiting for the user to authorize the sign in attempt.
|
innertubeClientCache,
|
||||||
innertubeClient.session.on("auth-pending", (data) => {
|
);
|
||||||
console.log(
|
} else {
|
||||||
`[INFO] [OAUTH] Go to ${data.verification_url} in your browser and enter code ${data.user_code} to authenticate.`,
|
innertubeClient = await Innertube.create({
|
||||||
|
cache: innertubeClientCache,
|
||||||
|
retrieve_player: innertubeClientFetchPlayer,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
);
|
);
|
||||||
});
|
} else if (innertubeClientOauthEnabled) {
|
||||||
// Fired when authentication is successful.
|
// Fired when waiting for the user to authorize the sign in attempt.
|
||||||
innertubeClient.session.on("auth", () => {
|
innertubeClient.session.on("auth-pending", (data) => {
|
||||||
console.log("[INFO] [OAUTH] Sign in successful!");
|
console.log(
|
||||||
});
|
`[INFO] [OAUTH] Go to ${data.verification_url} in your browser and enter code ${data.user_code} to authenticate.`,
|
||||||
// Fired when the access token expires.
|
);
|
||||||
innertubeClient.session.on("update-credentials", async () => {
|
});
|
||||||
console.log("[INFO] [OAUTH] Credentials updated.");
|
// Fired when authentication is successful.
|
||||||
await innertubeClient.session.oauth.cacheCredentials();
|
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
|
// Attempt to sign in and then cache the credentials
|
||||||
await innertubeClient.session.signIn();
|
await innertubeClient.session.signIn();
|
||||||
await innertubeClient.session.oauth.cacheCredentials();
|
await innertubeClient.session.oauth.cacheCredentials();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.use("*", async (c, next) => {
|
app.use("*", async (c, next) => {
|
||||||
// @ts-ignore Do not understand how to fix this error.
|
// @ts-ignore Do not understand how to fix this error.
|
||||||
c.set("innertubeClient", innertubeClient);
|
c.set("innertubeClient", innertubeClient);
|
||||||
// @ts-ignore Do not understand how to fix this error.
|
// @ts-ignore Do not understand how to fix this error.
|
||||||
c.set("konfigStore", konfigStore);
|
c.set("konfigStore", konfigStore);
|
||||||
await next();
|
await next();
|
||||||
});
|
});
|
||||||
|
|
||||||
routes(app, konfigStore);
|
routes(app, konfigStore);
|
||||||
|
|
||||||
Deno.serve({
|
Deno.serve({
|
||||||
port: Number(Deno.env.get("PORT")) || konfigStore.get("server.port") as number,
|
port: Number(Deno.env.get("PORT")) ||
|
||||||
hostname: Deno.env.get("HOST") || konfigStore.get("server.host") as string,
|
konfigStore.get("server.port") as number,
|
||||||
|
hostname: Deno.env.get("HOST") || konfigStore.get("server.host") as string,
|
||||||
}, app.fetch);
|
}, app.fetch);
|
||||||
|
|
|
@ -3,8 +3,14 @@ import { Store } from "@willsoto/node-konfig-core";
|
||||||
import { HTTPException } from "hono/http-exception";
|
import { HTTPException } from "hono/http-exception";
|
||||||
let getFetchClientLocation = "getFetchClient";
|
let getFetchClientLocation = "getFetchClient";
|
||||||
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
|
if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) {
|
||||||
getFetchClientLocation = import.meta.dirname + "/" +
|
if (Deno.env.has("DENO_COMPILED")) {
|
||||||
Deno.env.get("GET_FETCH_CLIENT_LOCATION");
|
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);
|
const { getFetchClient } = await import(getFetchClientLocation);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue