Hero scripts of machete.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

67 lines
1.8 KiB

import Hero from '@ulixee/hero';
import configs from '../config.mjs';
import fs from 'node:fs';
import path from 'node:path';
class HeroBot {
constructor(heroCloudServer) {
this.heroServer = heroCloudServer ? heroCloudServer : '';
this.supportedBots = {
douyin: 'https://www.douyin.com',
kuaishou: 'https://www.kuaishou.com',
xigua: 'https://www.ixigua.com',
bilibili: 'https://www.bilibili.com',
};
this.name = '';
}
//返回profile对象
async init(botName) {
if (typeof(this.supportedBots[botName]) == 'undefined') {
return false;
}
const base_url = this.supportedBots[botName];
try {
this.name = botName;
let options = {};
if (this.heroServer) {
options.connectionToCore = this.heroServer;
}
const profilePath = path.resolve('../tmp/', `profile_${botName}.json`);
if (fs.existsSync(profilePath) != false) {
const json = fs.readFileSync(profilePath, { encoding: 'utf8' });
options.userProfile = JSON.parse(json);
return options.userProfile;
}
const hero = new Hero(options);
await hero.goto(base_url, configs.heroBotOptions);
//等待所有内容加载完成
const tab = await hero.activeTab;
await tab.waitForLoad('AllContentLoaded', {timeoutMs: configs.heroTabOptions.timeoutMs});
//保存profile
const latestUserProfile = await hero.exportUserProfile();
fs.writeFileSync(profilePath, JSON.stringify(latestUserProfile, null, 2));
await hero.close();
return latestUserProfile;
}catch(error) {
console.error("Error got when request %s via hero: %s", base_url, error);
};
return false;
}
}
export default HeroBot;