From f242adb848f04b0d2651acbe03735bedbbe458a3 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Sun, 19 Nov 2023 22:04:19 +0900 Subject: [PATCH] view --- library.ts | 8 ++++++++ main.ts | 23 +++++++++++++++++++++-- platforms/dmm-books.ts | 10 +++------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/library.ts b/library.ts index 3b3b5a4..8e3df62 100644 --- a/library.ts +++ b/library.ts @@ -20,6 +20,14 @@ export function createLibrary(db: Database) { async delete(id: number) { await db.run(`delete from books where id = ?`, id); }, + async get(id: number): Promise { + const book: Book | undefined = await db.get( + `select books.id, platforms.name as platform, books.reader_url as readerUrl from books left join platforms on books.platform_id = platforms.id where books.id = ?`, + id, + ); + + return book; + }, async getBooks(): Promise> { const books: Array = await db.all( `select books.id, platforms.name as platform, books.reader_url as readerUrl from books left join platforms on books.platform_id = platforms.id`, diff --git a/main.ts b/main.ts index 65f1093..f0954ea 100644 --- a/main.ts +++ b/main.ts @@ -60,15 +60,34 @@ const options = { console.dir(books, { depth: null }); }, }, + view: { + type: "string", + async run() { + const db = await createDatabase(args.values.db!); + const library = createLibrary(db); + const book = await library.get(Number(args.values.view!)); + + if (!book) { + process.exit(1); + } + + console.dir(book, { depth: null }); + }, + }, download: { type: "string", async run() { const db = await createDatabase(args.values.db!); const library = createLibrary(db); - const books = await library.getBooks(); + const book = await library.get(Number(args.values.download!)); + + if (!book) { + process.exit(1); + } + const browser = await chromium.launch(); const platform = createPlatform({ db, browser }); - await platform.download(args.values.download!, books); + await platform.download(args.values.download!, book); }, }, help: { diff --git a/platforms/dmm-books.ts b/platforms/dmm-books.ts index 6a32531..9ae8bae 100644 --- a/platforms/dmm-books.ts +++ b/platforms/dmm-books.ts @@ -143,16 +143,12 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) { `update platforms set secrets = 'null' where name = 'dmm-books'`, ); }, - async download(dir: string, books: Array) { - await fs.mkdir(dir); - + async download(dir: string, book: Book) { const ctx = await loadBrowserContext(); const page = await ctx.newPage(); - // TODO: 複数ブックのサポート - const book = books[0]; - - // TODO: downloadBook() にまとめる + // TODO: --all + await fs.mkdir(dir); await page.goto(book.readerUrl); const files = await page.evaluate(getFiles);