75 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
import { extractModelId, extractNumber } from "../system/ultils.js";
 | 
						|
import { ScrapModel } from "./scrap-model.js";
 | 
						|
 | 
						|
export class GraysScrapModel extends ScrapModel {
 | 
						|
  action = async () => {
 | 
						|
    const urlsData = this.extractUrls();
 | 
						|
 | 
						|
    for (let item of urlsData) {
 | 
						|
      await this.page.goto(item.url);
 | 
						|
 | 
						|
      const data = await this.getItemsInHtml(item);
 | 
						|
 | 
						|
      const results = this.filterItemByKeyword(item.keyword, data);
 | 
						|
 | 
						|
      this.results[item.keyword] = results;
 | 
						|
    }
 | 
						|
 | 
						|
    console.log({ results: this.results });
 | 
						|
  };
 | 
						|
 | 
						|
  getPriceByEl = async (elementHandle) => {
 | 
						|
    const selectors = [
 | 
						|
      ".sc-ijDOKB.sc-bStcSt.ikmQUw.eEycyP", // Single product price
 | 
						|
      ".sc-ijDOKB.ikmQUw", // Multiple product price
 | 
						|
    ];
 | 
						|
 | 
						|
    for (const selector of selectors) {
 | 
						|
      const priceText = await elementHandle
 | 
						|
        .$eval(selector, (el) => el.textContent.trim())
 | 
						|
        .catch(() => null);
 | 
						|
      if (priceText) {
 | 
						|
        const price = extractNumber(priceText);
 | 
						|
        if (price) return price;
 | 
						|
      }
 | 
						|
    }
 | 
						|
 | 
						|
    return null;
 | 
						|
  };
 | 
						|
 | 
						|
  getItemsInHtml = async (data) => {
 | 
						|
    const elements = await this.page.$$(".sc-102aeaf3-1.eYPitT > div");
 | 
						|
    const results = [];
 | 
						|
 | 
						|
    for (const el of elements) {
 | 
						|
      const url = await el
 | 
						|
        .$eval(".sc-pKqro.sc-gFnajm.gqkMpZ.dzWUkJ", (el) =>
 | 
						|
          el.getAttribute("href")
 | 
						|
        )
 | 
						|
        .catch(() => null);
 | 
						|
 | 
						|
      const image_url = await el
 | 
						|
        .$eval("img.sc-gtJxfw.jbgdlx", (img) => img.getAttribute("src"))
 | 
						|
        .catch(() => null);
 | 
						|
 | 
						|
      const name = await el
 | 
						|
        .$eval(".sc-jlGgGc.dJRywx", (el) => el.textContent.trim())
 | 
						|
        .catch(() => null);
 | 
						|
 | 
						|
      const current_price = await this.getPriceByEl(el); // Gọi hàm async được định nghĩa trong class
 | 
						|
 | 
						|
      results.push({
 | 
						|
        url,
 | 
						|
        image_url,
 | 
						|
        name,
 | 
						|
        keyword: data.keyword,
 | 
						|
        model: extractModelId(url),
 | 
						|
        current_price,
 | 
						|
        scrap_config_id: this.scrap_config_id,
 | 
						|
      });
 | 
						|
    }
 | 
						|
 | 
						|
    return results;
 | 
						|
  };
 | 
						|
}
 |