Browse Source

add client log plugin

master
filesite 1 year ago
parent
commit
debbf60601
  1. 6
      bot/Bilibili.mjs
  2. 6
      bot/Douyin.mjs
  3. 2
      bot/HeroBot.mjs
  4. 6
      bot/Kuaishou.mjs
  5. 6
      bot/Xigua.mjs
  6. 4
      config.mjs
  7. 22
      plugin/ClientLogPlugin.mjs
  8. 2
      test/scrap_test.mjs

6
bot/Bilibili.mjs

@ -1,11 +1,12 @@
import Hero from '@ulixee/hero'; import Hero from '@ulixee/hero';
import configs from '../config.mjs'; import configs from '../config.mjs';
import HeroBot from './HeroBot.mjs'; import HeroBot from './HeroBot.mjs';
import ClientLogPlugin from '../plugin/ClientLogPlugin.mjs';
class Bilibili extends HeroBot { class Bilibili extends HeroBot {
async scrap(url) { async scrap(url) {
let data = {}; let data = {url: url, done: false};
try { try {
let options = { let options = {
@ -23,6 +24,7 @@ class Bilibili extends HeroBot {
} }
const hero = new Hero(options); const hero = new Hero(options);
hero.use(ClientLogPlugin); //开启log
await hero.goto(url, configs.heroBotOptions); await hero.goto(url, configs.heroBotOptions);
//等待所有内容加载完成 //等待所有内容加载完成
@ -56,6 +58,8 @@ class Bilibili extends HeroBot {
} }
await hero.close(); await hero.close();
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);
} }

6
bot/Douyin.mjs

@ -1,11 +1,12 @@
import Hero from '@ulixee/hero'; import Hero from '@ulixee/hero';
import configs from '../config.mjs'; import configs from '../config.mjs';
import HeroBot from './HeroBot.mjs'; import HeroBot from './HeroBot.mjs';
import ClientLogPlugin from '../plugin/ClientLogPlugin.mjs';
class Douyin extends HeroBot { class Douyin extends HeroBot {
async scrap(url) { async scrap(url) {
let data = {}; let data = {url: url, done: false};
try { try {
let options = { let options = {
@ -23,6 +24,7 @@ class Douyin extends HeroBot {
} }
const hero = new Hero(options); const hero = new Hero(options);
hero.use(ClientLogPlugin); //开启log
await hero.goto(url, configs.heroBotOptions); await hero.goto(url, configs.heroBotOptions);
//等待所有内容加载完成 //等待所有内容加载完成
@ -56,6 +58,8 @@ class Douyin extends HeroBot {
} }
await hero.close(); await hero.close();
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);
} }

2
bot/HeroBot.mjs

@ -2,6 +2,7 @@ import Hero from '@ulixee/hero';
import configs from '../config.mjs'; import configs from '../config.mjs';
import fs from 'node:fs'; import fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import ClientLogPlugin from '../plugin/ClientLogPlugin.mjs';
class HeroBot { class HeroBot {
constructor(heroCloudServer) { constructor(heroCloudServer) {
@ -46,6 +47,7 @@ class HeroBot {
console.log('Hero init配置', configs); console.log('Hero init配置', configs);
const hero = new Hero(options); const hero = new Hero(options);
hero.use(ClientLogPlugin); //开启log
await hero.goto(base_url, configs.heroBotOptions); await hero.goto(base_url, configs.heroBotOptions);
//等待所有内容加载完成 //等待所有内容加载完成

6
bot/Kuaishou.mjs

@ -1,11 +1,12 @@
import Hero from '@ulixee/hero'; import Hero from '@ulixee/hero';
import configs from '../config.mjs'; import configs from '../config.mjs';
import HeroBot from './HeroBot.mjs'; import HeroBot from './HeroBot.mjs';
import ClientLogPlugin from '../plugin/ClientLogPlugin.mjs';
class Kuaishou extends HeroBot { class Kuaishou extends HeroBot {
async scrap(url) { async scrap(url) {
let data = {}; let data = {url: url, done: false};
try { try {
let options = { let options = {
@ -23,6 +24,7 @@ class Kuaishou extends HeroBot {
} }
const hero = new Hero(options); const hero = new Hero(options);
hero.use(ClientLogPlugin); //开启log
await hero.goto(url, configs.heroBotOptions); await hero.goto(url, configs.heroBotOptions);
//等待所有内容加载完成 //等待所有内容加载完成
@ -40,6 +42,8 @@ class Kuaishou extends HeroBot {
} }
await hero.close(); await hero.close();
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);
}; };

6
bot/Xigua.mjs

@ -1,11 +1,12 @@
import Hero from '@ulixee/hero'; import Hero from '@ulixee/hero';
import configs from '../config.mjs'; import configs from '../config.mjs';
import HeroBot from './HeroBot.mjs'; import HeroBot from './HeroBot.mjs';
import ClientLogPlugin from '../plugin/ClientLogPlugin.mjs';
class Xigua extends HeroBot { class Xigua extends HeroBot {
async scrap(url) { async scrap(url) {
let data = {}; let data = {url: url, done: false};
try { try {
let options = { let options = {
@ -23,6 +24,7 @@ class Xigua extends HeroBot {
} }
const hero = new Hero(options); const hero = new Hero(options);
hero.use(ClientLogPlugin); //开启log
await hero.goto(url, configs.heroBotOptions); await hero.goto(url, configs.heroBotOptions);
//等待所有内容加载完成 //等待所有内容加载完成
@ -49,6 +51,8 @@ class Xigua extends HeroBot {
} }
await hero.close(); await hero.close();
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);
}; };

4
config.mjs

@ -18,13 +18,13 @@ export default {
//请求参数 //请求参数
heroBotOptions: { heroBotOptions: {
timeoutMs: 10000, timeoutMs: 20000,
referrer: '', referrer: '',
}, },
//网页tab参数 //网页tab参数
heroTabOptions: { heroTabOptions: {
timeoutMs: 5000 timeoutMs: 30000
}, },
//常用浏览器user-agent //常用浏览器user-agent

22
plugin/ClientLogPlugin.mjs

@ -0,0 +1,22 @@
import { ClientPlugin } from '@ulixee/hero-plugin-utils';
class ClientLogPlugin extends ClientPlugin {
async onHero(hero, sendToCore) {
const logTime = Date.now();
console.log('%s - New Hero is initialized, session id %s.', logTime, await hero.sessionId);
}
async onTab(hero, tab, sendToCore) {
const logTime = Date.now();
console.log('%s - New Tab is initialized, id %s.', logTime, await tab.tabId);
}
async onFrameEnvironment(hero, frameEnvironment, sendToCore) {
const logTime = Date.now();
console.log('%s - New FrameEnvironment is initialized.', logTime);
}
}
export default ClientLogPlugin;

2
test/scrap_test.mjs

@ -64,6 +64,8 @@ import configs from '../config.mjs';
configs.userAgent = configs.userAgents.mac_chrome; configs.userAgent = configs.userAgents.mac_chrome;
configs.viewport = configs.viewports.pc; configs.viewport = configs.viewports.pc;
configs.heroTabOptions.timeoutMs = 20000; //所有内容加载完成超时
console.log('Hero配置', configs); console.log('Hero配置', configs);
const xigua = new Xigua(heroCloudServer); const xigua = new Xigua(heroCloudServer);

Loading…
Cancel
Save