import { ScrapConfig } from '../entities/scrap-config.entity'; import { ScrapItem } from '../entities/scrap-item.entity'; import { ScrapInterface } from './scrap-interface'; import { Element } from 'domhandler'; export class ScrapModel implements ScrapInterface { protected keywords: string; protected search_url: string; protected scrap_config_id: ScrapConfig['id']; public results: Record = {}; constructor({ keywords, search_url, scrap_config_id, }: { keywords: string; search_url: string; scrap_config_id: ScrapConfig['id']; }) { this.keywords = keywords; this.search_url = search_url; this.scrap_config_id = scrap_config_id; } protected buildUrlWithKey(rawUrl: string, keyword: string) { return rawUrl.replaceAll('{{keyword}}', keyword); } protected extractUrls(): { url: string; keyword: string }[] { // Tách từng key ra từ một chuỗi keywords const keywordList = this.keywords.split(', '); // Dừng hàm nếu không có key nào if (keywordList.length <= 0) return []; // Lập qua từng key để lấy url return keywordList.map((keyword) => { return { url: this.buildUrlWithKey(this.search_url, keyword), keyword, }; }); } action: () => Promise; getInfoItems: ( data: { name: string; el: Element }[], ) => Record[]; getItemsInHtml: (data: { html: string; keyword: string; }) => Promise; protected filterItemByKeyword = (keyword: string, data: ScrapItem[]) => { return data.filter((item) => item.name.toLowerCase().includes(keyword.toLowerCase()), ); }; }