Add cookie input field in UI for YouTube bot detection bypass
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import { getSession, isLoginEnabled } from "../../lib/session";
|
||||
import { tApi } from "../../lib/i18n";
|
||||
import { mkdir, unlink, readFile, readdir } from "node:fs/promises";
|
||||
import { mkdir, unlink, readFile, readdir, writeFile } from "node:fs/promises";
|
||||
import { existsSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
@@ -26,8 +26,11 @@ export const POST: APIRoute = async ({ request }) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Cookie-Unterstützung: Temporäre Datei außerhalb des try-Blocks definieren
|
||||
let tempCookiesFile: string | null = null;
|
||||
|
||||
try {
|
||||
const { url } = await request.json();
|
||||
const { url, cookies } = await request.json();
|
||||
|
||||
if (!url || typeof url !== "string") {
|
||||
return new Response(
|
||||
@@ -39,6 +42,20 @@ export const POST: APIRoute = async ({ request }) => {
|
||||
);
|
||||
}
|
||||
|
||||
// Cookie-Unterstützung: Wenn Cookies im Request sind, temporäre Datei erstellen
|
||||
if (cookies && typeof cookies === "string" && cookies.trim()) {
|
||||
try {
|
||||
tempCookiesFile = path.join(tmpdir(), `cookies-${Date.now()}.txt`);
|
||||
await writeFile(tempCookiesFile, cookies.trim(), "utf-8");
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Fehler beim Erstellen der temporären Cookie-Datei:",
|
||||
error
|
||||
);
|
||||
// Weiter ohne Cookies
|
||||
}
|
||||
}
|
||||
|
||||
// YouTube URL validieren
|
||||
if (!url.includes("youtube.com") && !url.includes("youtu.be")) {
|
||||
return new Response(
|
||||
@@ -57,7 +74,8 @@ export const POST: APIRoute = async ({ request }) => {
|
||||
const format = request.headers.get("x-format") || "mp4";
|
||||
|
||||
// Cookie-Unterstützung für YouTube Bot-Erkennung
|
||||
const cookiesFile = process.env.YT_DLP_COOKIES;
|
||||
// Priorität: 1. Cookies aus Request, 2. Umgebungsvariable, 3. Browser-Cookies
|
||||
const cookiesFile = tempCookiesFile || process.env.YT_DLP_COOKIES;
|
||||
const cookiesFromBrowser = process.env.YT_DLP_COOKIES_FROM_BROWSER;
|
||||
|
||||
// JavaScript Runtime für yt-dlp (Standard: deno)
|
||||
@@ -312,5 +330,12 @@ export const POST: APIRoute = async ({ request }) => {
|
||||
}),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
} finally {
|
||||
// Temporäre Cookie-Datei löschen falls vorhanden
|
||||
if (tempCookiesFile && existsSync(tempCookiesFile)) {
|
||||
await unlink(tempCookiesFile).catch((err) => {
|
||||
console.error("Fehler beim Löschen der temporären Cookie-Datei:", err);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user