diff --git a/.gitignore b/.gitignore index dc96a2b..944dda5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ p.ts .idea CHANGELOG.md theme.zip -AGENTS.md \ No newline at end of file +AGENTS.md +lustpress-express \ No newline at end of file diff --git a/src/LustPress.ts b/src/LustPress.ts index e8dd2d8..ff8873c 100644 --- a/src/LustPress.ts +++ b/src/LustPress.ts @@ -9,7 +9,7 @@ const keyv = process.env.REDIS_URL : new Keyv(); keyv.on("error", (err) => console.log("Connection Error", err)); -const ttl = 1000 * 60 * 60 * Number(process.env.EXPIRE_CACHE || "1"); +const ttl = 1000 * 60 * 60 * Number(process.env.EXPIRE_CACHE); const GEO_TIMEOUT_MS = 3000; let cachedLastLocation: string | null = null; @@ -25,12 +25,14 @@ function cachedLocationOrUnknown(): string { class LustPress { url: string; useragent: string; - private browserUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"; + private browserUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"; private cookieCache: { [domain: string]: string } = {}; constructor() { this.url = ""; - this.useragent = `${pkg.name}/${pkg.version} Bun/${Bun.version}`; + this.browserUA = + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"; + this.useragent = process.env.USER_AGENT || `${pkg.name}/${pkg.version} Bun/${Bun.version}`; } async getPornhubCookies(): Promise { @@ -101,20 +103,23 @@ class LustPress { console.log(`[FETCH] ${url}`); let res = await fetch(url, { headers, redirect: "follow" }); - let text = await res.text(); + let arrayBuf = await res.arrayBuffer(); + let body = Buffer.from(arrayBuf); - if (isPornhub && text.includes("leastFactor")) { + if (isPornhub && body.toString("utf8").includes("leastFactor")) { console.log("Challenge detected at target URL. Re-authenticating..."); this.cookieCache[domain] = await this.getPornhubCookies(); res = await fetch(url, { headers: { ...headers, Cookie: this.cookieCache[domain] }, redirect: "follow" }); - text = await res.text(); + arrayBuf = await res.arrayBuffer(); + body = Buffer.from(arrayBuf); } - const body = Buffer.from(text); - if (!url.includes("/random")) await keyv.set(url, body, ttl); + if (res.ok && !url.includes("/random")) { + await keyv.set(url, body, ttl); + } return body; } diff --git a/src/index.ts b/src/index.ts index 0f9c97f..d1ad176 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,6 @@ const app = new Elysia() })) .use(scrapeRoutes) .onError(({ code, error, set }) => { - console.log("Error occurred:", error); if (code === "NOT_FOUND") { set.status = 404; return { @@ -40,6 +39,20 @@ const app = new Elysia() message: (error as Error).message || "Not Found", }; } + if (code === "VALIDATION") { + set.status = 400; + return { + success: false, + message: JSON.parse((error as Error).message)[0]?.message || "Validation Error", + }; + } + if (code === "UNKNOWN") { + set.status = 400; + return { + success: false, + message: (error as Error).message, + }; + } set.status = 500; return { success: false,