244 lines
5.9 KiB
JavaScript
244 lines
5.9 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,
|
|
getPathLocalData,
|
|
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);
|
|
}
|
|
|
|
async saveCodeToLocal({ name, code }) {
|
|
try {
|
|
const filePath = getPathLocalData(this.origin_url); // file path
|
|
const dirPath = path.dirname(filePath); // lấy thư mục cha
|
|
|
|
// kiểm tra folder chứa file đã tồn tại chưa
|
|
if (!fs.existsSync(dirPath)) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
}
|
|
|
|
// ghi file
|
|
fs.writeFileSync(
|
|
filePath,
|
|
JSON.stringify({ name, code, time: Date.now() }, null, 2) // format JSON đẹp
|
|
);
|
|
} catch (error) {
|
|
console.log(
|
|
`%cerror [${this.id}] models/api-bid.js line:149`,
|
|
"color: red; display: block; width: 100%;",
|
|
error
|
|
);
|
|
}
|
|
}
|
|
|
|
async clearCodeFromLocal() {
|
|
try {
|
|
const filePath = getPathLocalData(this.origin_url); // file path
|
|
const dirPath = path.dirname(filePath); // lấy thư mục cha
|
|
|
|
// kiểm tra folder chứa file đã tồn tại chưa
|
|
if (!fs.existsSync(dirPath)) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
}
|
|
|
|
// ghi file
|
|
fs.writeFileSync(
|
|
filePath,
|
|
JSON.stringify({}, null, 2) // format JSON đẹp
|
|
);
|
|
} catch (error) {
|
|
console.log(
|
|
`%cerror [${this.id}] models/api-bid.js line:187`,
|
|
"color: red; display: block; width: 100%;",
|
|
error
|
|
);
|
|
}
|
|
}
|
|
|
|
async loadCodeFromLocal() {
|
|
try {
|
|
const filePath = getPathLocalData(this.origin_url);
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
console.warn(
|
|
`%cwarn [${this.id}] models/api-bid.js`,
|
|
"color: orange; display: block; width: 100%;",
|
|
`File not found: ${filePath}`
|
|
);
|
|
return null; // hoặc {} tùy bạn muốn trả gì
|
|
}
|
|
|
|
const fileContent = fs.readFileSync(filePath, "utf-8");
|
|
const data = JSON.parse(fileContent);
|
|
|
|
return data; // { name, code, time }
|
|
} catch (error) {
|
|
console.error(
|
|
`%cerror [${this.id}] models/api-bid.js`,
|
|
"color: red; display: block; width: 100%;",
|
|
error
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async isCodeValid(minutes = 8) {
|
|
try {
|
|
const data = await this.loadCodeFromLocal();
|
|
if (!data || !data.time) {
|
|
return false;
|
|
}
|
|
|
|
const now = Date.now();
|
|
const timeDiff = now - data.time; // tính chênh lệch thời gian (ms)
|
|
|
|
const validDuration = minutes * 60 * 1000; // phút -> mili giây
|
|
|
|
return timeDiff <= validDuration;
|
|
} catch (error) {
|
|
console.error(
|
|
`%cerror [${this.id}] models/api-bid.js`,
|
|
"color: red; display: block; width: 100%;",
|
|
error
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
}
|