diff --git a/.gitignore b/.gitignore index eff015e..3ec6933 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json .DS_Store user_data/ +debug-html/ \ No newline at end of file diff --git a/src/Bandai.ts b/src/Bandai.ts index 5d5562d..082a19b 100644 --- a/src/Bandai.ts +++ b/src/Bandai.ts @@ -2,7 +2,9 @@ import { fetch } from 'bun'; import BandaiCookie from './Cookie'; import { loginBandai } from './utils/account-utils'; import * as cheerio from 'cheerio'; -import { getBandaiCookieRedisKey, redis } from './utils'; +import { getBandaiRawCookieRedisKey, redis } from './utils'; +import fs from 'fs'; +import path from 'path'; export default class Bandai { #email: string; @@ -38,7 +40,7 @@ export default class Bandai { } async #isLogind() { - const hasCookie = await redis.get(getBandaiCookieRedisKey(this.#email)); + const hasCookie = await redis.get(getBandaiRawCookieRedisKey(this.#email)); if (!hasCookie) return false; const hasLoginId = await this.#fetch('https://p-bandai.jp/mypage') .then(res => res.text()) @@ -46,11 +48,23 @@ export default class Bandai { return !hasLoginId; } + #debugHTML(html: string, name: string) { + const dir = path.resolve(__dirname, '../debug-html', this.#email.replace(/\W/g, '_')); + const file = path.resolve(dir, `${name}.html`); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + fs.writeFileSync(file, html); + } + async #getId() { - const selector = 'body > div.pb24-wrap > div.pb24-mypage-content > div.pb24-mypage-content__side > div:nth-child(1) > dl > dd:nth-child(2)'; + const selector = '#headerMenu .pb24-header-menu__data--name > dd'; const id = await this.#fetch('https://p-bandai.jp/mypage/') .then(res => res.text()) - .then(html => cheerio.load(html)) + .then(html => { + this.#debugHTML(html, 'my-page'); + return cheerio.load(html); + }) .then($ => { return $(selector).text().trim(); }); diff --git a/src/Cookie.ts b/src/Cookie.ts index aa7d8b4..5bd7746 100644 --- a/src/Cookie.ts +++ b/src/Cookie.ts @@ -1,7 +1,8 @@ -import { getBandaiCookieRedisKey, redis } from "./utils"; +import type { Cookie } from "puppeteer"; +import { getBandaiRawCookieRedisKey, redis } from "./utils"; export default class BandaiCookie { - #cookieMap = new Map(); + #cookieMap = new Map(); #id: string; @@ -10,23 +11,21 @@ export default class BandaiCookie { } async init() { - const cache = await redis.get(getBandaiCookieRedisKey(this.#id)); + const cache = await redis.get(getBandaiRawCookieRedisKey(this.#id)); if (!cache) return; - cache.split(';').forEach((cookie) => { - const [key, value] = cookie.trim().split('='); - if (!key || !value) return; - this.#cookieMap.set(key, value); + const cookies: Cookie[] = JSON.parse(cache); + cookies.map(cookie => { + this.#cookieMap.set(cookie.name, cookie); }); console.debug('Cookie initialized.'); } updateCookie(headers: Headers) { const cookies = headers.getSetCookie(); - const newCookies = cookies.map(cookie => (cookie.split(';')[0] ?? '').trim()).filter(Boolean); - newCookies.forEach(cookie => { - const [key, value] = cookie.trim().split('='); - if (!key || !value) return; - this.#cookieMap.set(key.trim(), value.trim()); + cookies.forEach(cookie => { + const cookieData = cookie.split(';'); // Extract the name=value part of + console.debug(cookieData); + // this.#cookieMap.set(cookie) }); } @@ -38,7 +37,7 @@ export default class BandaiCookie { } async saveCookie() { - const cookieString = this.getCookie(); - await redis.set(getBandaiCookieRedisKey(this.#id), cookieString); + const cookieString = JSON.stringify(this.#cookieMap.values().toArray()); + await redis.set(getBandaiRawCookieRedisKey(this.#id), cookieString); } } \ No newline at end of file diff --git a/src/utils/account-utils.ts b/src/utils/account-utils.ts index c3fc4ac..a4f1bb1 100644 --- a/src/utils/account-utils.ts +++ b/src/utils/account-utils.ts @@ -1,4 +1,4 @@ -import { getBandaiCookieRedisKey, redis } from "."; +import { getBandaiRawCookieRedisKey, redis } from "."; import { initBrowser } from "./puppeteer-utils"; export async function loginBandai(emai: string, password: string) { @@ -20,12 +20,8 @@ export async function loginBandai(emai: string, password: string) { page.click('#btnLogin'), page.waitForNavigation(), ]); - const cookies = await browser.cookies() - .then(cookies => { - return cookies.map(cookie => `${cookie.name}=${cookie.value}`).join('; '); - }); - console.debug('Cookie', cookies); - await redis.set(getBandaiCookieRedisKey(emai), cookies); + const rawCookies = await browser.cookies(); + await redis.set(getBandaiRawCookieRedisKey(emai), JSON.stringify(rawCookies)); await browser.close() - return cookies; + return; } \ No newline at end of file diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 93f5fa4..4af53bc 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -15,6 +15,7 @@ export const puppeteerInitArgs = [ export const CHROME = { Windows: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', - Linux: '/usr/bin/google-chrome', + // Linux: '/usr/bin/google-chrome', + Linux: '/usr/bin/chromium', Mac: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', }; \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index ffc3fba..7ad0f01 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,7 +3,8 @@ import { RedisClient } from "bun"; import { CHROME } from "./constants"; export const redis = new RedisClient(process.env.REDIS); -export const getBandaiCookieRedisKey = (email: string) => `bandai:cookies:${email}`; +export const getBandaiRawCookieRedisKey = (email: string) => `bandai:cookies:${email}:raw`; + export const getChromePath = () => { const platform = process.platform; if (platform === 'win32') return CHROME.Windows; diff --git a/test/login.test.ts b/test/login.test.ts index 48b6e6a..ad739c8 100644 --- a/test/login.test.ts +++ b/test/login.test.ts @@ -1,12 +1,12 @@ import { test, expect } from "bun:test"; import Bandai from "../src/Bandai"; -import { getBandaiCookieRedisKey, redis } from "../src/utils"; +import { getBandaiRawCookieRedisKey, redis } from "../src/utils"; test('Test login', async () => { const email = 'allin-603@outlook.com'; const password = '123456789qw'; const account = new Bandai(email, password); await account.login(); - const cookie = await redis.get(getBandaiCookieRedisKey(email)); + const cookie = await redis.get(getBandaiRawCookieRedisKey(email)); expect(cookie).toBeDefined(); }); \ No newline at end of file