145 lines
3.2 KiB
JavaScript
145 lines
3.2 KiB
JavaScript
import * as fs from "fs";
|
|
import path from "path";
|
|
import BID_TYPE from "../system/bid-type.js";
|
|
import browser from "../system/browser.js";
|
|
import CONSTANTS from "../system/constants.js";
|
|
import {
|
|
findEarlyLoginTime,
|
|
getPathProfile,
|
|
isTimeReached,
|
|
sanitizeFileName,
|
|
} from "../system/utils.js";
|
|
import { Bid } from "./bid.js";
|
|
|
|
export class ApiBid extends Bid {
|
|
id;
|
|
account;
|
|
children = [];
|
|
children_processing = [];
|
|
created_at;
|
|
updated_at;
|
|
origin_url;
|
|
active;
|
|
// browser_context;
|
|
username;
|
|
password;
|
|
|
|
constructor({
|
|
url,
|
|
username,
|
|
password,
|
|
id,
|
|
children,
|
|
created_at,
|
|
updated_at,
|
|
origin_url,
|
|
active,
|
|
}) {
|
|
super(BID_TYPE.API_BID, url);
|
|
|
|
this.created_at = created_at;
|
|
this.updated_at = updated_at;
|
|
this.children = children;
|
|
this.origin_url = origin_url;
|
|
this.active = active;
|
|
this.username = username;
|
|
this.password = password;
|
|
this.id = id;
|
|
}
|
|
|
|
setNewData({
|
|
url,
|
|
username,
|
|
password,
|
|
id,
|
|
children,
|
|
created_at,
|
|
updated_at,
|
|
origin_url,
|
|
active,
|
|
}) {
|
|
this.created_at = created_at;
|
|
this.updated_at = updated_at;
|
|
this.children = children;
|
|
this.origin_url = origin_url;
|
|
this.active = active;
|
|
this.username = username;
|
|
this.password = password;
|
|
this.url = url;
|
|
}
|
|
|
|
puppeteer_connect = async () => {
|
|
this.browser_context = await browser.createBrowserContext();
|
|
|
|
const page = await this.browser_context.newPage();
|
|
|
|
this.page_context = page;
|
|
|
|
await this.restoreContext();
|
|
};
|
|
|
|
listen_events = async () => {
|
|
if (this.page_context) return;
|
|
|
|
await this.puppeteer_connect();
|
|
|
|
await this.action();
|
|
|
|
await this.saveContext();
|
|
};
|
|
|
|
async saveContext() {
|
|
if (!this.browser_context || !this.page_context) return;
|
|
|
|
try {
|
|
const cookies = await this.browser_context.cookies();
|
|
const localStorageData = await this.page_context.evaluate(() =>
|
|
JSON.stringify(localStorage)
|
|
);
|
|
|
|
const contextData = {
|
|
cookies,
|
|
localStorage: localStorageData,
|
|
};
|
|
|
|
const dirPath = path.join(CONSTANTS.PROFILE_PATH);
|
|
|
|
if (!fs.existsSync(dirPath)) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
console.log(`📂 Save at folder: ${dirPath}`);
|
|
}
|
|
|
|
fs.writeFileSync(
|
|
path.join(dirPath, sanitizeFileName(this.origin_url) + ".json"),
|
|
JSON.stringify(contextData, null, 2)
|
|
);
|
|
console.log("✅ Context saved!");
|
|
} catch (error) {
|
|
console.log("Save Context: ", error.message);
|
|
}
|
|
}
|
|
|
|
async restoreContext() {
|
|
if (!this.browser_context || !this.page_context) return;
|
|
|
|
const filePath = getPathProfile(this.origin_url);
|
|
|
|
if (!fs.existsSync(filePath)) return;
|
|
|
|
const contextData = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
|
|
// Restore Cookies
|
|
await this.page_context.setCookie(...contextData.cookies);
|
|
|
|
console.log("🔄 Context restored!");
|
|
}
|
|
|
|
async onCloseLogin() {}
|
|
|
|
async isTimeToLogin() {
|
|
const earlyLoginTime = findEarlyLoginTime(this);
|
|
|
|
return earlyLoginTime && isTimeReached(earlyLoginTime);
|
|
}
|
|
}
|