view
This commit is contained in:
parent
ef7445ab10
commit
f242adb848
3 changed files with 32 additions and 9 deletions
|
@ -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<Book | undefined> {
|
||||
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<Array<Book>> {
|
||||
const books: Array<Book> = 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`,
|
||||
|
|
23
main.ts
23
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: {
|
||||
|
|
|
@ -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<Book>) {
|
||||
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);
|
||||
|
|
Loading…
Add table
Reference in a new issue