import type { Book } from "../library"; import type { Browser, BrowserContext, ImageFile } from "../browser"; export function DlsiteManiax(browser: Browser) { async function* getAllBooks(ctx: BrowserContext): AsyncGenerator { const totalCountEndpoint = "https://play.dlsite.com/api/product_count"; const endpoint = "https://play.dlsite.com/api/purchases"; const pager = { page: 1, perPage: 50, totalCount: Infinity, }; const res = await ctx.request.get(totalCountEndpoint); const body: { user: number; } = await res.json(); pager.totalCount = body.user; while ((pager.page - 1) * pager.perPage <= pager.totalCount) { const res = await ctx.request.get(`${endpoint}?page=${pager.page}`); if (!res.ok()) { throw new Error(`${res.status()} ${res.statusText()}`); } const body: { limit: number; works: Array<{ workno: number; name: { ja_JP: string; }; maker: { name: { ja_JP: string; }; }; }>; } = await res.json(); for (const work of Object.values(body.works).flat()) { yield { id: NaN, platform: "dlsite-maniax", readerUrl: `https://play.dlsite.com/#/work/${work.workno}`, title: work.name.ja_JP || "", authors: [work.maker.name.ja_JP || ""], }; process.stderr.write("."); } pager.page += 1; pager.perPage = body.limit; } } return { async *pull(): AsyncGenerator { const ctx = await browser.loadBrowserContext("dlsite-maniax"); yield* getAllBooks(ctx); process.stderr.write(`\n`); }, async getFiles(book: Book): Promise Promise>> { const ctx = await browser.loadBrowserContext("dlsite-maniax"); const page = await ctx.newPage(); await page.goto(book.readerUrl); const [, workId] = /^https:[/][/]play[.]dlsite[.]com[/]#[/]work[/]([^/]+)/.exec( book.readerUrl, ) ?? []; if (!workId) { throw new Error(`workId is not included: ${book.readerUrl}`); } const url = `https://www.dlsite.com/home/download/=/product_id/${workId}.html`; const imageFile = { url }; return [ async () => { const blob = await browser.drawImage(page, imageFile); process.stderr.write("."); return blob; }, ]; }, loginEndpoints: ["https://www.dlsite.com/home/login"], loginSuccessUrl: (url: URL) => url.origin === "https://www.dlsite.com", logoutEndpoints: ["https://www.dlsite.com/home/logout"], }; } DlsiteManiax.siteUrl = (url: URL) => url.href.startsWith("https://play.dlsite.com/#/work/");