Browse Source

bot exception catch

master
filesite 7 months ago
parent
commit
870f41509e
  1. 8
      bot/Bilibili.mjs
  2. 10
      bot/Douyin.mjs
  3. 7
      bot/HeroBot.mjs
  4. 8
      bot/Kuaishou.mjs
  5. 8
      bot/Xigua.mjs

8
bot/Bilibili.mjs

@ -8,7 +8,6 @@ class Bilibili extends HeroBot {
async scrap(url) { async scrap(url) {
let data = {url: url, done: false}; let data = {url: url, done: false};
try {
let options = { let options = {
userAgent: configs.userAgent, userAgent: configs.userAgent,
viewport: configs.viewport viewport: configs.viewport
@ -18,12 +17,18 @@ class Bilibili extends HeroBot {
options.connectionToCore = this.heroServer; options.connectionToCore = this.heroServer;
} }
try {
const profile = await this.init('bilibili'); const profile = await this.init('bilibili');
if (profile) { if (profile) {
options.userProfile = profile; options.userProfile = profile;
} }
}catch(err) {
console.error("Error got when init Bilibili bot", err);
}
const hero = new Hero(options); const hero = new Hero(options);
try {
hero.use(ClientLogPlugin); //开启log hero.use(ClientLogPlugin); //开启log
await hero.goto(url, configs.heroBotOptions); await hero.goto(url, configs.heroBotOptions);
@ -79,6 +84,7 @@ class Bilibili extends HeroBot {
data.done = true; data.done = true;
}catch(error) { }catch(error) {
console.error("Error got when request %s via hero: %s", url, error); console.error("Error got when request %s via hero: %s", url, error);
await hero.close();
} }
return data; return data;

10
bot/Douyin.mjs

@ -8,7 +8,6 @@ class Douyin extends HeroBot {
async scrap(url) { async scrap(url) {
let data = {url: url, done: false}; let data = {url: url, done: false};
try {
let options = { let options = {
userAgent: configs.userAgent, userAgent: configs.userAgent,
viewport: configs.viewport viewport: configs.viewport
@ -18,13 +17,18 @@ class Douyin extends HeroBot {
options.connectionToCore = this.heroServer; options.connectionToCore = this.heroServer;
} }
try {
const profile = await this.init('douyin'); const profile = await this.init('douyin');
data.bot = this.name;
if (profile) { if (profile) {
options.userProfile = profile; options.userProfile = profile;
} }
}catch(err) {
console.error("Error got when init Douyin bot", err);
}
const hero = new Hero(options); const hero = new Hero(options);
try {
hero.use(ClientLogPlugin); //开启log hero.use(ClientLogPlugin); //开启log
await hero.goto(url, configs.heroBotOptions); await hero.goto(url, configs.heroBotOptions);
@ -73,9 +77,11 @@ class Douyin extends HeroBot {
await hero.close(); await hero.close();
data.bot = this.name;
data.done = true; data.done = true;
}catch(error) { }catch(error) {
console.error("Error got when request %s via hero: %s", url, error); console.error("Error got when request %s via hero: %s", url, error);
await hero.close();
} }
return data; return data;

7
bot/HeroBot.mjs

@ -29,7 +29,6 @@ class HeroBot {
const base_url = this.supportedBots[botName]; const base_url = this.supportedBots[botName];
try {
this.name = botName; this.name = botName;
let options = { let options = {
@ -48,8 +47,9 @@ class HeroBot {
} }
//console.log('Hero init配置', configs); //console.log('Hero init配置', configs);
const hero = new Hero(options); const hero = new Hero(options);
try {
hero.use(ClientLogPlugin); //开启log hero.use(ClientLogPlugin); //开启log
await hero.goto(base_url, configs.heroBotOptions); await hero.goto(base_url, configs.heroBotOptions);
@ -65,7 +65,8 @@ class HeroBot {
return latestUserProfile; return latestUserProfile;
}catch(error) { }catch(error) {
console.error("Error got when request %s via hero: %s", base_url, error); console.error("Error got when bot init with %s via hero, error: %s", base_url, error);
await hero.close();
} }
return false; return false;

8
bot/Kuaishou.mjs

@ -8,7 +8,6 @@ class Kuaishou extends HeroBot {
async scrap(url) { async scrap(url) {
let data = {url: url, done: false}; let data = {url: url, done: false};
try {
let options = { let options = {
userAgent: configs.userAgent, userAgent: configs.userAgent,
viewport: configs.viewport viewport: configs.viewport
@ -18,12 +17,18 @@ class Kuaishou extends HeroBot {
options.connectionToCore = this.heroServer; options.connectionToCore = this.heroServer;
} }
try {
const profile = await this.init('kuaishou'); const profile = await this.init('kuaishou');
if (profile) { if (profile) {
options.userProfile = profile; options.userProfile = profile;
} }
}catch(err) {
console.error("Error got when init Kuaishou bot", err);
}
const hero = new Hero(options); const hero = new Hero(options);
try {
hero.use(ClientLogPlugin); //开启log hero.use(ClientLogPlugin); //开启log
await hero.goto(url, configs.heroBotOptions); await hero.goto(url, configs.heroBotOptions);
@ -60,6 +65,7 @@ class Kuaishou extends HeroBot {
data.done = true; data.done = true;
}catch(error) { }catch(error) {
console.error("Error got when request %s via hero: %s", url, error); console.error("Error got when request %s via hero: %s", url, error);
await hero.close();
}; };
return data; return data;

8
bot/Xigua.mjs

@ -8,7 +8,6 @@ class Xigua extends HeroBot {
async scrap(url) { async scrap(url) {
let data = {url: url, done: false}; let data = {url: url, done: false};
try {
let options = { let options = {
userAgent: configs.userAgent, userAgent: configs.userAgent,
viewport: configs.viewport viewport: configs.viewport
@ -18,12 +17,18 @@ class Xigua extends HeroBot {
options.connectionToCore = this.heroServer; options.connectionToCore = this.heroServer;
} }
try {
const profile = await this.init('xigua'); const profile = await this.init('xigua');
if (profile) { if (profile) {
options.userProfile = profile; options.userProfile = profile;
} }
}catch(err) {
console.error("Error got when init Xigua bot", err);
}
const hero = new Hero(options); const hero = new Hero(options);
try {
hero.use(ClientLogPlugin); //开启log hero.use(ClientLogPlugin); //开启log
await hero.goto(url, configs.heroBotOptions); await hero.goto(url, configs.heroBotOptions);
@ -69,6 +74,7 @@ class Xigua extends HeroBot {
data.done = true; data.done = true;
}catch(error) { }catch(error) {
console.error("Error got when request %s via hero: %s", url, error); console.error("Error got when request %s via hero: %s", url, error);
await hero.close();
}; };
return data; return data;

Loading…
Cancel
Save