support zip file

This commit is contained in:
Nebel 2023-12-03 12:44:31 +09:00
parent 5135d5121f
commit 7547a34e03
Signed by: nebel
GPG key ID: 79807D08C6EF6460
5 changed files with 67 additions and 10 deletions

View file

@ -107,6 +107,27 @@ on conflict(reader_url)
book.title book.title
}`.replace(/[/]/g, "%2F"); }`.replace(/[/]/g, "%2F");
const files = await fs.readdir(path);
if (files.every((f) => f.match(/[.](zip|cbz)$/))) {
const digits = String(files.length).length;
function pad(n: string) {
return n.padStart(digits, "0");
}
for (const [n, f] of Object.entries(files)) {
await fs.rename(
`${path}/${f}`,
`${opts.outDir}/${title}${
files.length > 1 ? ` - ${pad(n)}` : ""
}.${f.split(".").at(-1)}`,
);
}
await fs.rmdir(path);
return;
}
const out = createWriteStream(`${opts.outDir}/${title}.cbz`); const out = createWriteStream(`${opts.outDir}/${title}.cbz`);
const zip = new Zip(function cb(err, data, final) { const zip = new Zip(function cb(err, data, final) {
@ -118,8 +139,6 @@ on conflict(reader_url)
out[final ? "end" : "write"](data); out[final ? "end" : "write"](data);
}); });
const files = await fs.readdir(path);
for (const file of files) { for (const file of files) {
const data = new ZipPassThrough(file); const data = new ZipPassThrough(file);
zip.add(data); zip.add(data);

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "@fogtype/gadl", "name": "@fogtype/gadl",
"version": "1.0.0", "version": "1.2.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@fogtype/gadl", "name": "@fogtype/gadl",
"version": "1.0.0", "version": "1.2.0",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"dependencies": { "dependencies": {
"fflate": "^0.8.1", "fflate": "^0.8.1",

View file

@ -1,6 +1,6 @@
{ {
"name": "@fogtype/gadl", "name": "@fogtype/gadl",
"version": "1.1.0", "version": "1.2.0",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"type": "module", "type": "module",
"bin": "bin/run.js", "bin": "bin/run.js",

View file

@ -55,6 +55,8 @@ export function createPlatform(opts: {
const supportedTypes = { const supportedTypes = {
"image/png": "png", "image/png": "png",
"image/jpeg": "jpg", "image/jpeg": "jpg",
"application/zip": "zip",
"application/vnd.comicbook+zip": "cbz",
}; };
for (const [n, dataUrl] of Object.entries(files)) { for (const [n, dataUrl] of Object.entries(files)) {

View file

@ -72,13 +72,49 @@ export function FanzaDoujin(browser: Browser) {
const page = await ctx.newPage(); const page = await ctx.newPage();
await page.goto(book.readerUrl); await page.goto(book.readerUrl);
const [, productId] = /product_id=([^/]*)/.exec(book.readerUrl) ?? [];
if (!productId) {
throw new Error(`product_id is not included: ${book.readerUrl}`);
}
const res = await ctx.request.get(
`https://www.dmm.co.jp/dc/doujin/api/mylibraries/details/${productId}/`,
);
if (!res.ok()) {
throw new Error(`${res.status()} ${res.statusText()}`);
}
const body: {
data: {
drm: {
dmmBooks: boolean;
softDenchi: boolean;
};
downloadLinks: Record<number, string>;
};
} = await res.json();
const imageFiles: Array<ImageFile> = [];
if (body.data.drm.dmmBooks) {
await page.waitForSelector(`li[class^="fileTreeItem"]`); await page.waitForSelector(`li[class^="fileTreeItem"]`);
await page.click(`li[class^="fileTreeItem"]>a`); await page.click(`li[class^="fileTreeItem"]>a`);
await page.waitForURL((url) => await page.waitForURL((url) =>
url.href.startsWith("https://www.dmm.co.jp/dc/-/viewer/=/product_id="), url.href.startsWith(
"https://www.dmm.co.jp/dc/-/viewer/=/product_id=",
),
); );
const imageFiles: Array<ImageFile> = await page.evaluate(getImageFiles); imageFiles.push(...(await page.evaluate(getImageFiles)));
} else {
for (const link of Object.values(body.data.downloadLinks)) {
const url = new URL(link, "https://www.dmm.co.jp/").href;
imageFiles.push({ url });
}
}
return imageFiles.map((imageFile) => async () => { return imageFiles.map((imageFile) => async () => {
const dataUrl = await browser.drawImage(page, imageFile); const dataUrl = await browser.drawImage(page, imageFile);