82 lines
2.6 KiB
JavaScript
82 lines
2.6 KiB
JavaScript
import CONSTANTS from './constants.js';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { updateStatusWork } from './apis/bid.js';
|
|
|
|
export const isNumber = (value) => !isNaN(value) && !isNaN(parseFloat(value));
|
|
|
|
export const takeSnapshot = async (page, item, imageName, type = CONSTANTS.TYPE_IMAGE.ERRORS) => {
|
|
if (page.isClosed()) return;
|
|
|
|
try {
|
|
const baseDir = path.join(CONSTANTS.ERROR_IMAGES_PATH, item.type, String(item.id)); // Thư mục theo lot_id
|
|
const typeDir = path.join(baseDir, type); // Thư mục con theo type
|
|
|
|
// Tạo tên file, nếu type === 'work' thì không có timestamp
|
|
const fileName = type === CONSTANTS.TYPE_IMAGE.WORK ? `${imageName}.png` : `${imageName}_${new Date().toISOString().replace(/[:.]/g, '-')}.png`;
|
|
|
|
const filePath = path.join(typeDir, fileName);
|
|
|
|
// Kiểm tra và tạo thư mục nếu chưa tồn tại
|
|
if (!fs.existsSync(typeDir)) {
|
|
fs.mkdirSync(typeDir, { recursive: true });
|
|
console.log(`📂 Save at folder: ${typeDir}`);
|
|
}
|
|
|
|
await page.waitForSelector('body', { visible: true, timeout: 5000 });
|
|
|
|
// Chụp ảnh màn hình và lưu vào filePath
|
|
await page.screenshot({ path: filePath, fullPage: true });
|
|
|
|
console.log(`📸 Image saved at: ${filePath}`);
|
|
|
|
// Nếu type === 'work', gửi ảnh lên API
|
|
if (type === CONSTANTS.TYPE_IMAGE.WORK) {
|
|
await updateStatusWork(item, filePath);
|
|
}
|
|
} catch (error) {
|
|
console.log('Error when snapshot: ' + error.message);
|
|
}
|
|
};
|
|
|
|
export const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
export async function safeClosePage(item) {
|
|
try {
|
|
const page = item.page_context;
|
|
|
|
if (!page?.isClosed() && page?.close) {
|
|
await page.close();
|
|
}
|
|
|
|
item.page_context = undefined;
|
|
if (item?.page_context) {
|
|
item.page_context = undefined;
|
|
}
|
|
} catch (error) {
|
|
console.log("Can't close item: " + item.id);
|
|
}
|
|
}
|
|
|
|
export function isTimeReached(targetTime) {
|
|
if (!targetTime) return false;
|
|
|
|
const targetDate = new Date(targetTime);
|
|
const now = new Date();
|
|
|
|
return now >= targetDate;
|
|
}
|
|
|
|
export function extractNumber(str) {
|
|
const match = str.match(/\d+(\.\d+)?/);
|
|
return match ? parseFloat(match[0]) : null;
|
|
}
|
|
|
|
export const sanitizeFileName = (url) => {
|
|
return url.replace(/[:\/]/g, '_');
|
|
};
|
|
|
|
export const getPathProfile = (origin_url) => {
|
|
return path.join(CONSTANTS.PROFILE_PATH, sanitizeFileName(origin_url) + '.json');
|
|
};
|