169 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			169 lines
		
	
	
		
			4.5 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 { delay, getPathProfile, sanitizeFileName } from '../system/utils.js';
 | 
						|
import { Bid } from './bid.js';
 | 
						|
 | 
						|
export class ProductBid extends Bid {
 | 
						|
    id;
 | 
						|
    max_price;
 | 
						|
    model;
 | 
						|
    lot_id;
 | 
						|
    plus_price;
 | 
						|
    close_time;
 | 
						|
    first_bid;
 | 
						|
    quantity;
 | 
						|
    created_at;
 | 
						|
    updated_at;
 | 
						|
    histories;
 | 
						|
    start_bid_time;
 | 
						|
    parent_browser_context;
 | 
						|
    web_bid;
 | 
						|
    current_price;
 | 
						|
    name;
 | 
						|
    reserve_price;
 | 
						|
    update;
 | 
						|
 | 
						|
    constructor({
 | 
						|
        url,
 | 
						|
        max_price,
 | 
						|
        plus_price,
 | 
						|
        model,
 | 
						|
        first_bid = false,
 | 
						|
        id,
 | 
						|
        created_at,
 | 
						|
        updated_at,
 | 
						|
        quantity = 1,
 | 
						|
        histories = [],
 | 
						|
        close_time,
 | 
						|
        lot_id,
 | 
						|
        start_bid_time,
 | 
						|
        web_bid,
 | 
						|
        current_price,
 | 
						|
        reserve_price,
 | 
						|
        name,
 | 
						|
    }) {
 | 
						|
        super(BID_TYPE.PRODUCT_TAB, url);
 | 
						|
        this.max_price = max_price || 0;
 | 
						|
        this.model = model;
 | 
						|
        this.plus_price = plus_price || 0;
 | 
						|
        this.first_bid = first_bid;
 | 
						|
        this.id = id;
 | 
						|
        this.created_at = created_at;
 | 
						|
        this.updated_at = updated_at;
 | 
						|
        this.quantity = quantity;
 | 
						|
        this.histories = histories;
 | 
						|
        this.close_time = close_time;
 | 
						|
        this.lot_id = lot_id;
 | 
						|
        this.start_bid_time = start_bid_time;
 | 
						|
        this.web_bid = web_bid;
 | 
						|
        this.current_price = current_price;
 | 
						|
        this.name = name;
 | 
						|
        this.reserve_price = reserve_price;
 | 
						|
    }
 | 
						|
 | 
						|
    setNewData({
 | 
						|
        url,
 | 
						|
        max_price,
 | 
						|
        plus_price,
 | 
						|
        model,
 | 
						|
        first_bid = false,
 | 
						|
        id,
 | 
						|
        created_at,
 | 
						|
        updated_at,
 | 
						|
        quantity = 1,
 | 
						|
        histories = [],
 | 
						|
        close_time,
 | 
						|
        lot_id,
 | 
						|
        start_bid_time,
 | 
						|
        web_bid,
 | 
						|
        current_price,
 | 
						|
        reserve_price,
 | 
						|
        name,
 | 
						|
    }) {
 | 
						|
        this.max_price = max_price || 0;
 | 
						|
        this.model = model;
 | 
						|
        this.plus_price = plus_price || 0;
 | 
						|
        this.first_bid = first_bid;
 | 
						|
        this.id = id;
 | 
						|
        this.created_at = created_at;
 | 
						|
        this.updated_at = updated_at;
 | 
						|
        this.quantity = quantity;
 | 
						|
        this.histories = histories;
 | 
						|
        this.close_time = close_time;
 | 
						|
        this.lot_id = lot_id;
 | 
						|
        this.start_bid_time = start_bid_time;
 | 
						|
        this.web_bid = web_bid;
 | 
						|
        this.url = url;
 | 
						|
        this.current_price = current_price;
 | 
						|
        this.name = name;
 | 
						|
        this.reserve_price = reserve_price;
 | 
						|
    }
 | 
						|
 | 
						|
    puppeteer_connect = async () => {
 | 
						|
        if (!this.parent_browser_context) {
 | 
						|
            console.log(`❌ Connect fail. parent_browser_context is null: ${this.id}`);
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        const context = await browser.createBrowserContext();
 | 
						|
 | 
						|
        const statusInit = await this.restoreContext(context);
 | 
						|
 | 
						|
        if (!statusInit) {
 | 
						|
            console.log(`⚠️ Restore failed.`);
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        const page = await context.newPage();
 | 
						|
 | 
						|
        this.page_context = page;
 | 
						|
    };
 | 
						|
 | 
						|
    async restoreContext(context) {
 | 
						|
        const filePath = getPathProfile(this.web_bid.origin_url);
 | 
						|
 | 
						|
        if (!fs.existsSync(filePath)) return false;
 | 
						|
 | 
						|
        const contextData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
 | 
						|
 | 
						|
        // Restore Cookies
 | 
						|
        await context.setCookie(...contextData.cookies);
 | 
						|
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    // async gotoLink() {
 | 
						|
    //     const page = this.page_context;
 | 
						|
    //     console.log('🔄 Starting the bidding process...');
 | 
						|
 | 
						|
    //     await page.goto(this.url, { waitUntil: 'networkidle2' });
 | 
						|
    //     console.log(`✅ Navigated to: ${this.url}`);
 | 
						|
 | 
						|
    //     await page.bringToFront();
 | 
						|
    //     console.log('👀 Brought the tab to the foreground.');
 | 
						|
    // }
 | 
						|
    async gotoLink() {
 | 
						|
        const page = this.page_context;
 | 
						|
 | 
						|
        if (page.isClosed()) {
 | 
						|
            console.error('❌ Page has been closed, cannot navigate.');
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        console.log('🔄 Starting the bidding process...');
 | 
						|
 | 
						|
        try {
 | 
						|
            await page.goto(this.url, { waitUntil: 'networkidle2' });
 | 
						|
            console.log(`✅ Navigated to: ${this.url}`);
 | 
						|
 | 
						|
            await page.bringToFront();
 | 
						|
            console.log('👀 Brought the tab to the foreground.');
 | 
						|
        } catch (error) {
 | 
						|
            console.error('❌ Error during navigation:', error);
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |