diff --git a/page/GalleryPage.ts b/page/GalleryPage.ts index 625959d..67a96ae 100644 --- a/page/GalleryPage.ts +++ b/page/GalleryPage.ts @@ -2,23 +2,26 @@ import { DOMParser, Element } from "deno_dom/deno-dom-wasm-noinit.ts"; import { Client } from "../client.ts"; import { initDOMParser, map, parse_bool } from "../utils.ts"; import { parseUrl, UrlType } from "../url.ts"; +import { SinglePage } from "./SinglePage.ts"; type Page = { token: string; thumbnail: string; name: string; + index: number; }; class Image { base; - /**Page number*/ - index; #gp; - constructor(base: Page, index: number, gp: GalleryPage) { + data: SinglePage | undefined; + constructor(base: Page, gp: GalleryPage) { this.base = base; - this.index = index; this.#gp = gp; } + get is_original() { + return this.data?.is_original; + } } class GalleryPage { @@ -56,10 +59,11 @@ class GalleryPage { const href = a.getAttribute("href"); if (!href) throw Error("Link not found."); const u = parseUrl(href); - if (!u || u.type !== UrlType.Single) { + if (!u || u.type !== UrlType.Single || u.index === undefined) { throw Error("Failed to parse url."); } const token = u.token; + const index = u.index; const img = b.querySelector("img"); if (!img) throw Error("Image not found."); const thumbnail = img.getAttribute("src"); @@ -67,7 +71,7 @@ class GalleryPage { const name = img.getAttribute("title")?.match(/page \d+: (.*)/i) ?.at(1); if (!name) throw Error("name not found"); - return { name, token, thumbnail }; + return { name, token, thumbnail, index }; }); } let b = load_image(this.doc); @@ -77,7 +81,7 @@ class GalleryPage { now = await now.next_page(); b = b.concat(load_image(now.doc)); } - return b.map((v, i) => new Image(v, i + 1, this)); + return b.map((v) => new Image(v, this)); } get favorited() { const o = this.gdd_data.get("Favorited")?.innerText; diff --git a/page/SinglePage.ts b/page/SinglePage.ts index 84c859d..1f8a951 100644 --- a/page/SinglePage.ts +++ b/page/SinglePage.ts @@ -2,12 +2,18 @@ import { DOMParser } from "deno_dom/deno-dom-wasm-noinit.ts"; import { Client } from "../client.ts"; import { initDOMParser } from "../utils.ts"; -class SinglePage { +export class SinglePage { dom; doc; client; _meta: string | undefined; _gid: number | undefined; + #i2_data: string | undefined; + #i7_data: string | undefined; + #oxres: number | undefined; + #oyres: number | undefined; + #xres: number | undefined; + #yres: number | undefined; constructor(html: string, client: Client) { const dom = (new DOMParser()).parseFromString(html, "text/html"); if (!dom) { @@ -44,6 +50,28 @@ class SinglePage { } return this._gid; } + get i2_data() { + if (this.#i2_data === undefined) { + const ele = this.doc.querySelector("#i2 > div:not([class=sn])"); + if (!ele) throw Error("Element not found."); + /**@ts-ignore */ + const e = ele as HTMLElement; + const i2_data = e.innerText; + this.#i2_data = i2_data; + return i2_data; + } else return this.#i2_data; + } + get i7_data() { + if (this.#i7_data === undefined) { + const ele = this.doc.querySelector("#i7 a"); + if (!ele) throw Error("Element not found."); + /**@ts-ignore */ + const e = ele as HTMLElement; + const i7_data = e.innerText; + this.#i7_data = i7_data; + return i7_data; + } else return this.#i7_data; + } get img_url() { const img = this.doc.querySelector("#img"); if (!img) throw Error("Unknown image url."); @@ -51,6 +79,9 @@ class SinglePage { if (!url) throw Error("Unknown image url."); return url; } + get is_original() { + return this.original_url === null; + } get meta() { if (this._meta === undefined) { const scripts = this.doc.getElementsByTagName("script"); @@ -63,6 +94,9 @@ class SinglePage { } return this._meta; } + get name() { + return this.i2_data.match(/(.*?) ::/)?.at(1); + } async nextPage() { const url = this.nextPageUrl; if (!url) return null; @@ -74,6 +108,26 @@ class SinglePage { if (a === null) return null; return a.getAttribute("href"); } + get origin_xres() { + if (this.is_original) return this.xres; + if (this.#oxres === undefined) { + const ox = this.i7_data.match(/(\d+) x \d+/)?.at(1); + if (!ox) throw Error("Failed to parse width."); + const oxres = parseInt(ox); + this.#oxres = oxres; + return oxres; + } else return this.#oxres; + } + get origin_yres() { + if (this.is_original) return this.yres; + if (this.#oyres === undefined) { + const oy = this.i7_data.match(/\d+ x (\d+)/)?.at(1); + if (!oy) throw Error("Failed to parse height."); + const oyres = parseInt(oy); + this.#oyres = oyres; + return oyres; + } else return this.#oyres; + } get original_url() { const a = this.doc.querySelector("#i7 a"); if (a == null) return null; @@ -95,6 +149,24 @@ class SinglePage { if (a === null) return null; return a.getAttribute("href"); } + get xres() { + if (this.#xres === undefined) { + const xr = this.i2_data.match(/.*? :: (\d+)/)?.at(1); + if (!xr) throw Error("Failed to parse width."); + const xres = parseInt(xr); + this.#xres = xres; + return xres; + } else return this.#xres; + } + get yres() { + if (this.#yres === undefined) { + const yr = this.i2_data.match(/.*? :: \d+ x (\d+)/)?.at(1); + if (!yr) throw Error("Failed to parse height."); + const yres = parseInt(yr); + this.#yres = yres; + return yres; + } else return this.#yres; + } } export async function load_single_page(html: string, client: Client) { diff --git a/page/SinglePage_test.ts b/page/SinglePage_test.ts index 6986f02..7c1585e 100644 --- a/page/SinglePage_test.ts +++ b/page/SinglePage_test.ts @@ -16,4 +16,25 @@ Deno.test({ assert(np); assertEquals(np.currentIndex, 2); assertEquals(np.gid, re.gid); + if (re.is_original) { + assertEquals(re.xres, 2067); + assertEquals(re.yres, 3001); + assertEquals(re.name, "logo.png"); + } + assertEquals(re.origin_xres, 2067); + assertEquals(re.origin_yres, 3001); + if (np.is_original) { + assertEquals(np.xres, 2067); + assertEquals(np.yres, 3001); + assertEquals(np.name, "1.png"); + } + assertEquals(np.origin_xres, 2067); + assertEquals(np.origin_yres, 3001); + const re2 = await client.fetchSignlePage(2028320, "1f48eb617e", 19); + assertEquals(re2.currentIndex, 19); + assertEquals(re2.gid, 2028320); + assertEquals(re2.name, "18.jpg"); + assertEquals(re2.is_original, false); + assertEquals(re2.origin_xres, 4893); + assertEquals(re2.origin_yres, 3446); });