This commit is contained in:
Nebel 2023-11-19 16:18:00 +09:00
parent e696627acc
commit 7208920cd1
Signed by: nebel
GPG key ID: 79807D08C6EF6460
4 changed files with 44 additions and 24 deletions

View file

@ -1,3 +1,3 @@
import { type BrowserContext, chromium } from "playwright";
import { type Browser, chromium } from "playwright";
export { BrowserContext, chromium };
export { Browser, chromium };

12
main.ts
View file

@ -21,11 +21,19 @@ const options = {
async run() {
const db = await createDatabase(args.values.db!);
const browser = await chromium.launch({ headless: false });
const ctx = await browser.newContext();
const platform = createPlatform(db, ctx);
const platform = createPlatform(db, browser);
await platform.login();
},
},
logout: {
type: "boolean",
async run() {
const db = await createDatabase(args.values.db!);
const browser = await chromium.launch();
const platform = createPlatform(db, browser);
await platform.logout();
},
},
add: {
type: "string",
async run() {

View file

@ -1,28 +1,16 @@
import { Database } from "./database";
import { BrowserContext } from "./browser";
import type { Database } from "./database";
import type { Browser } from "./browser";
import { DmmBooks } from "./platforms/dmm-books";
function dmm(db: Database, ctx: BrowserContext) {
return {
async login() {
const page = await ctx.newPage();
await page.goto("https://accounts.dmm.com/service/login/password");
await page.waitForURL("https://www.dmm.com/", { timeout: 0 });
const secrets = await ctx.storageState();
await ctx.close();
await db.run(
`update platforms set secrets = ? where name = 'dmm-books'`,
JSON.stringify(secrets),
);
},
};
}
export function createPlatform(db: Database, ctx: BrowserContext) {
const platform = dmm(db, ctx);
export function createPlatform(db: Database, browser: Browser) {
const platform = DmmBooks(db, browser);
return {
async login() {
await platform.login();
},
async logout() {
await platform.logout();
},
};
}

24
platforms/dmm-books.ts Normal file
View file

@ -0,0 +1,24 @@
import type { Database } from "../database";
import type { Browser } from "../browser";
export function DmmBooks(db: Database, browser: Browser) {
return {
async login() {
const ctx = await browser.newContext();
const page = await ctx.newPage();
await page.goto("https://accounts.dmm.com/service/login/password");
await page.waitForURL("https://www.dmm.com/", { timeout: 0 });
const secrets = await ctx.storageState();
await ctx.close();
await db.run(
`update platforms set secrets = ? where name = 'dmm-books'`,
JSON.stringify(secrets),
);
},
async logout() {
await db.run(
`update platforms set secrets = 'null' where name = 'dmm-books'`,
);
},
};
}