--download=all

This commit is contained in:
Nebel 2023-11-23 01:31:27 +09:00
parent aac345833b
commit 94c6ade9cf
Signed by: nebel
GPG key ID: 79807D08C6EF6460
3 changed files with 30 additions and 16 deletions

View file

@ -81,7 +81,11 @@ on conflict(reader_url)
} }
const path = `${bookDir.path}/${bookDir.name}`; const path = `${bookDir.path}/${bookDir.name}`;
const out = createWriteStream(`${path}.cbz`); const book = await this.get(bookDir.name);
const title = book
? `${book.authors.join("、")}${book.title}`.replace(/[/]/g, "%2F")
: bookDir.name;
const out = createWriteStream(`${dir}/${title}.cbz`);
const zip = new Zip(function cb(err, data, final) { const zip = new Zip(function cb(err, data, final) {
if (err) { if (err) {
out.destroy(err); out.destroy(err);

22
main.ts
View file

@ -2,7 +2,7 @@ import fs from "node:fs/promises";
import util from "node:util"; import util from "node:util";
import { chromium } from "./browser"; import { chromium } from "./browser";
import { createDatabase } from "./database"; import { createDatabase } from "./database";
import { createLibrary } from "./library"; import { type Book, createLibrary } from "./library";
import { createPlatform } from "./platform"; import { createPlatform } from "./platform";
const options = { const options = {
@ -27,6 +27,7 @@ const options = {
const browser = await chromium.launch({ headless: false }); const browser = await chromium.launch({ headless: false });
const platform = createPlatform({ db, browser }); const platform = createPlatform({ db, browser });
await platform.login(); await platform.login();
await browser.close();
}, },
}, },
logout: { logout: {
@ -36,6 +37,7 @@ const options = {
const browser = await chromium.launch(); const browser = await chromium.launch();
const platform = createPlatform({ db, browser }); const platform = createPlatform({ db, browser });
await platform.logout(); await platform.logout();
await browser.close();
}, },
}, },
add: { add: {
@ -98,6 +100,8 @@ const options = {
for await (const book of platform.pull()) { for await (const book of platform.pull()) {
await library.add(book); await library.add(book);
} }
await browser.close();
}, },
}, },
download: { download: {
@ -105,17 +109,29 @@ const options = {
async run() { async run() {
const db = await createDatabase(args.values.db!); const db = await createDatabase(args.values.db!);
const library = createLibrary(db); const library = createLibrary(db);
const browser = await chromium.launch();
const platform = createPlatform({ db, browser });
const books: Array<Book> = [];
if (args.values.download === "all") {
books.push(...(await library.getBooks()));
} else {
const book = await library.get(Number(args.values.download!)); const book = await library.get(Number(args.values.download!));
if (!book) { if (!book) {
process.exit(1); process.exit(1);
} }
const browser = await chromium.launch(); books.push(book);
const platform = createPlatform({ db, browser }); }
for (const book of books) {
const dir = `${args.values["out-dir"]!}/${book.id}`; const dir = `${args.values["out-dir"]!}/${book.id}`;
await platform.download(dir, book); await platform.download(dir, book);
}
await library.archive(args.values["out-dir"]!); await library.archive(args.values["out-dir"]!);
await browser.close();
}, },
}, },
json: { json: {

View file

@ -243,7 +243,6 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) {
page.waitForURL("https://www.dmm.co.jp/top/", { timeout: 0 }), page.waitForURL("https://www.dmm.co.jp/top/", { timeout: 0 }),
]); ]);
const secrets = await ctx.storageState(); const secrets = await ctx.storageState();
await browser.close();
await db.run( await db.run(
`update platforms set secrets = ? where name = 'dmm-books'`, `update platforms set secrets = ? where name = 'dmm-books'`,
JSON.stringify(secrets), JSON.stringify(secrets),
@ -251,7 +250,6 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) {
}, },
async logout() { async logout() {
await browser.close();
await db.run( await db.run(
`update platforms set secrets = 'null' where name = 'dmm-books'`, `update platforms set secrets = 'null' where name = 'dmm-books'`,
); );
@ -263,8 +261,6 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) {
yield* getAllBooks(ctx); yield* getAllBooks(ctx);
process.stderr.write(`\n`); process.stderr.write(`\n`);
await browser.close();
}, },
async download(dir: string, book: Book) { async download(dir: string, book: Book) {
@ -302,8 +298,6 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) {
} }
process.stderr.write(`\n`); process.stderr.write(`\n`);
await browser.close();
}, },
}; };
} }