From 94c6ade9cf5cde0cb00ee46ab10ac7b74e5f4967 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Thu, 23 Nov 2023 01:31:27 +0900 Subject: [PATCH] --download=all --- library.ts | 6 +++++- main.ts | 34 +++++++++++++++++++++++++--------- platforms/dmm-books.ts | 6 ------ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/library.ts b/library.ts index 00d0ffd..2e70eed 100644 --- a/library.ts +++ b/library.ts @@ -81,7 +81,11 @@ on conflict(reader_url) } 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) { if (err) { out.destroy(err); diff --git a/main.ts b/main.ts index 12b1afc..0a59505 100644 --- a/main.ts +++ b/main.ts @@ -2,7 +2,7 @@ import fs from "node:fs/promises"; import util from "node:util"; import { chromium } from "./browser"; import { createDatabase } from "./database"; -import { createLibrary } from "./library"; +import { type Book, createLibrary } from "./library"; import { createPlatform } from "./platform"; const options = { @@ -27,6 +27,7 @@ const options = { const browser = await chromium.launch({ headless: false }); const platform = createPlatform({ db, browser }); await platform.login(); + await browser.close(); }, }, logout: { @@ -36,6 +37,7 @@ const options = { const browser = await chromium.launch(); const platform = createPlatform({ db, browser }); await platform.logout(); + await browser.close(); }, }, add: { @@ -98,6 +100,8 @@ const options = { for await (const book of platform.pull()) { await library.add(book); } + + await browser.close(); }, }, download: { @@ -105,17 +109,29 @@ const options = { async run() { const db = await createDatabase(args.values.db!); const library = createLibrary(db); - const book = await library.get(Number(args.values.download!)); - - if (!book) { - process.exit(1); - } - const browser = await chromium.launch(); const platform = createPlatform({ db, browser }); - const dir = `${args.values["out-dir"]!}/${book.id}`; - await platform.download(dir, book); + const books: Array = []; + + if (args.values.download === "all") { + books.push(...(await library.getBooks())); + } else { + const book = await library.get(Number(args.values.download!)); + + if (!book) { + process.exit(1); + } + + books.push(book); + } + + for (const book of books) { + const dir = `${args.values["out-dir"]!}/${book.id}`; + await platform.download(dir, book); + } + await library.archive(args.values["out-dir"]!); + await browser.close(); }, }, json: { diff --git a/platforms/dmm-books.ts b/platforms/dmm-books.ts index 724f8f7..bb4d1c2 100644 --- a/platforms/dmm-books.ts +++ b/platforms/dmm-books.ts @@ -243,7 +243,6 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) { page.waitForURL("https://www.dmm.co.jp/top/", { timeout: 0 }), ]); const secrets = await ctx.storageState(); - await browser.close(); await db.run( `update platforms set secrets = ? where name = 'dmm-books'`, JSON.stringify(secrets), @@ -251,7 +250,6 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) { }, async logout() { - await browser.close(); await db.run( `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); process.stderr.write(`\n`); - - await browser.close(); }, async download(dir: string, book: Book) { @@ -302,8 +298,6 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) { } process.stderr.write(`\n`); - - await browser.close(); }, }; }