Update MPVPage

This commit is contained in:
2023-05-20 22:26:54 +08:00
parent 915d7995f1
commit 5e71baac47
3 changed files with 94 additions and 8 deletions

View File

@@ -33,6 +33,14 @@ export class Client {
hostname.endsWith(".e-hentai.org") ||
hostname.endsWith(".exhentai.org");
}
async redirect(url: string) {
const re = await this.get(url, { redirect: "manual" });
if (re.status == 301 || re.status == 302) {
const t = re.headers.get("location");
re.body?.cancel();
return t ? t : undefined;
} else return undefined;
}
request(
url: string | Request | URL,
method: string | undefined = undefined,

View File

@@ -22,7 +22,7 @@ export type MPVDispatchData = {
ll: string;
/**File search url. full url is `${base_url}${lo}` */
lo: string;
/**`ori` or `Download original ${width} x ${height} ${file_size} source`*/
/**`org` or `Download original ${width} x ${height} ${file_size} source`*/
o: string;
/**Reload token*/
s: string;
@@ -32,20 +32,59 @@ export type MPVDispatchData = {
yres: string;
};
const ORG_REG = /^download original (\d+) x (\d+) (.*) source$/i;
class MPVImage {
base;
/**Page number*/
index;
#mpv;
data: MPVDispatchData | undefined;
#oxres: number | undefined;
#oyres: number | undefined;
redirected_url: string | undefined;
constructor(base: MPVRawImage, index: number, mpv: MPVPage) {
this.base = base;
this.index = index;
this.#mpv = mpv;
}
get is_original() {
const t = this.data?.o;
if (!t) return undefined;
return t === "org";
}
get name() {
return this.base.n;
}
get original_imgurl() {
const t = this.data?.lf;
if (!t) return undefined;
return `${this.#mpv.base_url}${t}`;
}
get origin_xres() {
if (this.is_original) return this.xres;
if (this.#oxres === undefined) {
const o = this.data?.o;
if (!o) return undefined;
const m = o.match(ORG_REG);
if (!m) return undefined;
const oxres = parseInt(m[1]);
this.#oxres = oxres;
return oxres;
} else return this.#oxres;
}
get origin_yres() {
if (this.is_original) return this.yres;
if (this.#oyres === undefined) {
const o = this.data?.o;
if (!o) return undefined;
const m = o.match(ORG_REG);
if (!m) return undefined;
const oyres = parseInt(m[2]);
this.#oyres = oyres;
return oyres;
} else return this.#oyres;
}
get page_number() {
return this.index;
}
@@ -71,8 +110,32 @@ class MPVImage {
this.index,
this.page_token,
);
} else {
this.data = await this.#mpv.image_dispatch(
this.index,
this.page_token,
this.data.s,
);
}
}
async #load_image(u: string) {
const re = await this.#mpv.client.get(u);
if (re.status !== 200) {
re.body?.cancel();
return undefined;
}
return re;
}
async load_original_image() {
if (this.redirected_url) {
const re = await this.#load_image(this.redirected_url);
if (re) return re;
}
const url = this.original_imgurl;
if (!url) return undefined;
this.redirected_url = await this.#mpv.client.redirect(url);
return await this.#load_image(this.redirected_url || url);
}
}
class MPVPage {

View File

@@ -1,13 +1,9 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Client } from "../client.ts";
import { load_settings } from "../config.ts";
import { API_PERMISSION } from "../test_base.ts";
import { assert } from "https://deno.land/[email protected]/_util/asserts.ts";
Deno.test({
name: "MPVPage_test",
permissions: API_PERMISSION,
}, async () => {
Deno.test("MPVPage_test", async () => {
const cfg = await load_settings("./config.json");
const client = new Client(cfg);
const re = await client.fetchMPVPage(1473589, "b1f3c60a95");
@@ -20,7 +16,26 @@ Deno.test({
assertEquals(p1.page_token, "7ffd92a751");
await p1.load();
assert(p1.data);
assertEquals(p1.xres, 2449);
assertEquals(p1.yres, 3427);
if (p1.is_original) {
assertEquals(p1.xres, 2449);
assertEquals(p1.yres, 3427);
}
assertEquals(p1.origin_xres, 2449);
assertEquals(p1.origin_yres, 3427);
console.log(p1.data.i);
const p2 = re.imagelist[1];
assertEquals(p2.index, 2);
assertEquals(p2.name, "2.png");
assertEquals(p2.page_token, "95e59ae4b7");
await p2.load();
assert(p2.data);
assert(!p2.is_original);
assertEquals(p2.origin_xres, 3351);
assertEquals(p2.origin_yres, 4894);
console.log(p2.data.i);
console.log(p2.original_imgurl);
const r = await p2.load_original_image();
assert(r);
r.body?.cancel();
console.log(p2.redirected_url);
});