import Hero from '@ulixee/hero'; import configs from '../config.mjs'; import fs from 'node:fs'; import path from 'node:path'; class Kuaishou { constructor(heroCloudServer) { this.heroServer = heroCloudServer ? heroCloudServer : ''; } async scrap(url) { let data = {}; try { let options = {}; if (this.heroServer) { options.connectionToCore = this.heroServer; } const profilePath = path.resolve('../tmp/', 'profile_kuaishou.json'); let saveProfile = false; if (fs.existsSync(profilePath) != false) { const json = fs.readFileSync(profilePath, { encoding: 'utf8' }); options.userProfile = JSON.parse(json); saveProfile = true; } const hero = new Hero(options); await hero.goto(url, configs.heroBotOptions); //等待所有内容加载完成 const tab = await hero.activeTab; await tab.waitForLoad('AllContentLoaded', {timeoutMs: configs.heroTabOptions.timeoutMs}); if (saveProfile) { const latestUserProfile = await hero.exportUserProfile(); fs.writeFileSync(profilePath, JSON.stringify(latestUserProfile, null, 2)); } //解析网页HTML数据 data.title = await hero.document.title; //data.url = await hero.url; const elem = await hero.detach( hero.document.querySelector('.video-container-player') ); data.cover = elem.getAttribute('poster'); await hero.close(); }catch(error) { console.error("Error got when request %s via hero: %s", url, error); }; return data; } } export default Kuaishou;