gadl/platform.ts

111 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-12-02 23:22:33 +09:00
import fs from "node:fs/promises";
import type { Book } from "./library";
2023-11-19 16:18:00 +09:00
import type { Browser } from "./browser";
2023-12-02 23:22:33 +09:00
import type { Database } from "./database";
2023-12-24 22:24:25 +09:00
import { GooglePlayBooks } from "./platforms/google-play-books";
2023-11-19 16:18:00 +09:00
import { DmmBooks } from "./platforms/dmm-books";
2023-12-24 22:24:25 +09:00
import { DlsiteManiax } from "./platforms/dlsite-maniax";
2023-12-03 05:54:45 +09:00
import { FanzaDoujin } from "./platforms/fanza-doujin";
2023-12-02 23:22:33 +09:00
2023-12-03 17:35:19 +09:00
export const platforms = {
2023-12-24 22:24:25 +09:00
"google-play-books": GooglePlayBooks,
2023-12-02 23:22:33 +09:00
"dmm-books": DmmBooks,
2023-12-24 22:24:25 +09:00
"dlsite-maniax": DlsiteManiax,
2023-12-03 05:54:45 +09:00
"fanza-doujin": FanzaDoujin,
2023-12-02 23:22:33 +09:00
};
export type TPlatform = keyof typeof platforms;
export function site(url: string): TPlatform {
2023-12-03 05:54:45 +09:00
for (const [platform, { siteUrl }] of Object.entries(platforms)) {
if (siteUrl(new URL(url))) return platform as TPlatform;
2023-12-02 23:22:33 +09:00
}
throw new Error(`Unsupported URL: ${url}`);
}
export function createPlatform(opts: {
platform: TPlatform;
db: Database;
browser: Browser;
}) {
if (!(opts.platform in platforms)) {
2023-12-03 05:54:45 +09:00
throw new Error(
`The value must be a platform type: ${[...Object.keys(platforms)].join(
", ",
)}.`,
);
2023-12-02 23:22:33 +09:00
}
const platform = platforms[opts.platform](opts.browser);
2023-11-19 02:25:32 +09:00
2023-12-02 23:22:33 +09:00
return {
...platform,
2023-12-03 00:48:24 +09:00
2023-12-02 23:22:33 +09:00
async download(dir: string, book: Book): Promise<void> {
await fs.mkdir(dir, { recursive: true });
2023-12-03 00:48:24 +09:00
2023-12-03 16:14:38 +09:00
const files: Array<() => Promise<Blob>> = await platform.getFiles(book);
2023-12-03 00:48:24 +09:00
const digits = String(files.length).length;
function pad(n: string) {
return n.padStart(digits, "0");
}
const supportedTypes = {
"image/png": "png",
"image/jpeg": "jpg",
2023-12-03 12:44:31 +09:00
"application/zip": "zip",
"application/vnd.comicbook+zip": "cbz",
2023-12-03 00:48:24 +09:00
};
2023-12-03 16:14:38 +09:00
for (const [n, getBlob] of Object.entries(files)) {
const blob = await getBlob();
const extension = supportedTypes[blob.type];
2023-12-03 00:48:24 +09:00
if (!extension) {
throw new Error(
2023-12-03 16:14:38 +09:00
`It was ${blob.type}. The image must be a file of type: ${[
2023-12-03 00:48:24 +09:00
...Object.keys(supportedTypes),
].join(", ")}.`,
);
}
2023-12-03 16:14:38 +09:00
const buffer = Buffer.from(await blob.arrayBuffer());
2023-12-03 00:48:24 +09:00
await fs.writeFile(`${dir}/${pad(n)}.${extension}`, buffer);
}
process.stderr.write(`\n`);
2023-12-02 23:22:33 +09:00
},
2023-12-03 00:48:24 +09:00
2023-12-03 16:14:38 +09:00
async login() {
const ctx = await opts.browser.newContext();
const page = await ctx.newPage();
for (const loginEndpoint of platform.loginEndpoints) {
await page.goto(loginEndpoint);
await page.waitForURL(platform.loginSuccessUrl, { timeout: 0 });
}
await opts.browser.saveBrowserContext(opts.platform, ctx);
},
2023-12-02 23:22:33 +09:00
async logout() {
2023-12-03 16:14:38 +09:00
try {
const ctx = await opts.browser.loadBrowserContext(opts.platform);
const page = await ctx.newPage();
for (const logoutEndpoint of platform.logoutEndpoints) {
await page.goto(logoutEndpoint);
}
} catch (error) {
process.stderr.write(`Warning: ${(error as Error).message}\n`);
}
2023-12-02 23:22:33 +09:00
await opts.db.run(
`update platforms set secrets = 'null' where name = ?`,
opts.platform,
);
},
};
2023-11-19 02:25:32 +09:00
}