3 Commits

Author SHA1 Message Date
SNEK WAIFU
fe225e1090 8.3.4-alpha 2026-05-14 07:25:26 +07:00
SNEK WAIFU
6ed1567f0e need testing 2026-05-14 07:24:58 +07:00
Indrawan I.
67445ed648 fix: resolve cache ttl calculation (#22) 2026-05-14 06:38:19 +07:00
4 changed files with 34 additions and 11 deletions

3
.gitignore vendored
View File

@@ -10,4 +10,5 @@ p.ts
.idea
CHANGELOG.md
theme.zip
AGENTS.md
AGENTS.md
lustpress-express

View File

@@ -1,6 +1,6 @@
{
"name": "lustpress",
"version": "8.2.3-alpha",
"version": "8.3.4-alpha",
"description": "RESTful and experimental API for PH and other R18 websites",
"main": "src/index.ts",
"scripts": {

View File

@@ -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<string> {
@@ -77,7 +79,10 @@ class LustPress {
async fetchBody(url: string): Promise<Buffer> {
const cached = await keyv.get(url);
if (cached) return cached;
if (cached) {
console.log(`[CACHE HIT] ${url}`);
return cached;
}
const isPornhub = /pornhub\.com/i.test(url);
const domain = new URL(url).hostname;
@@ -96,21 +101,25 @@ class LustPress {
headers["Accept-Language"] = "en-US,en;q=0.9";
}
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;
}

View File

@@ -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,