|
|
@ -9,8 +9,10 @@ class Douyin extends HeroBot { |
|
|
|
let data = {url: url, done: false}; |
|
|
|
let data = {url: url, done: false}; |
|
|
|
|
|
|
|
|
|
|
|
//use iphone
|
|
|
|
//use iphone
|
|
|
|
|
|
|
|
if (this.ua == 'mob') { |
|
|
|
configs.userAgent = configs.userAgents.iphone_safari; |
|
|
|
configs.userAgent = configs.userAgents.iphone_safari; |
|
|
|
configs.viewport = configs.viewports.mob; |
|
|
|
configs.viewport = configs.viewports.mob; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let options = { |
|
|
|
let options = { |
|
|
|
userAgent: configs.userAgent, //default mac os
|
|
|
|
userAgent: configs.userAgent, //default mac os
|
|
|
@ -42,48 +44,54 @@ class Douyin extends HeroBot { |
|
|
|
const tab = await hero.activeTab; |
|
|
|
const tab = await hero.activeTab; |
|
|
|
|
|
|
|
|
|
|
|
//for mob
|
|
|
|
//for mob
|
|
|
|
|
|
|
|
if (this.ua == 'mob') { |
|
|
|
await tab.waitForLoad('DomContentLoaded', {timeoutMs: configs.heroTabOptions.timeoutMs}); |
|
|
|
await tab.waitForLoad('DomContentLoaded', {timeoutMs: configs.heroTabOptions.timeoutMs}); |
|
|
|
|
|
|
|
}else { |
|
|
|
//for pc
|
|
|
|
//for pc
|
|
|
|
//await tab.waitForLoad('AllContentLoaded', {timeoutMs: configs.heroTabOptions.timeoutMs});
|
|
|
|
await tab.waitForLoad('DomContentLoaded', {timeoutMs: configs.heroTabOptions.timeoutMs}); //AllContentLoaded, DomContentLoaded
|
|
|
|
//await hero.waitForState({
|
|
|
|
await hero.waitForState({ |
|
|
|
// all(assert) {
|
|
|
|
all(assert) { |
|
|
|
// assert(
|
|
|
|
assert( |
|
|
|
// hero.document.title,
|
|
|
|
hero.document.title, |
|
|
|
// text => text != '',
|
|
|
|
text => text != '', |
|
|
|
// );
|
|
|
|
); |
|
|
|
// }
|
|
|
|
} |
|
|
|
//}, {timeoutMs: configs.heroTabOptions.timeoutMs});
|
|
|
|
}, {timeoutMs: configs.heroTabOptions.timeoutMs}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//解析网页HTML数据
|
|
|
|
//解析网页HTML数据
|
|
|
|
data.title = await hero.document.title; |
|
|
|
data.title = await hero.document.title; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.ua == 'mob') { |
|
|
|
|
|
|
|
//手机版网页解析
|
|
|
|
|
|
|
|
const imgElem = await hero.querySelector('.video-container img.poster'); |
|
|
|
|
|
|
|
if (!imgElem) { |
|
|
|
|
|
|
|
console.error('HTML解析出错,找不到封面图', data); |
|
|
|
|
|
|
|
await hero.close(); |
|
|
|
|
|
|
|
//删除profile文件后重试
|
|
|
|
|
|
|
|
await this.deleteProfile(); |
|
|
|
|
|
|
|
this.ua = 'pc'; //切换到pc模式重试
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data.cover = await imgElem.src; |
|
|
|
|
|
|
|
}else { |
|
|
|
//pc版网页解析
|
|
|
|
//pc版网页解析
|
|
|
|
/* |
|
|
|
|
|
|
|
const elems = await hero.document.querySelectorAll('meta'); |
|
|
|
const elems = await hero.document.querySelectorAll('meta'); |
|
|
|
let meta_name = ''; |
|
|
|
let meta_name = ''; |
|
|
|
for (const elem of elems) { |
|
|
|
for (const elem of elems) { |
|
|
|
meta_name = await elem.getAttribute('name'); |
|
|
|
meta_name = await elem.getAttribute('name'); |
|
|
|
if (!meta_name) {continue;} |
|
|
|
if (!meta_name) {continue;} |
|
|
|
meta_name = meta_name.toLowerCase(); |
|
|
|
meta_name = meta_name.toLowerCase(); |
|
|
|
//console.log('meta', meta_name);
|
|
|
|
|
|
|
|
if (meta_name.indexOf('video_cover_image_url') > -1) { |
|
|
|
if (meta_name.indexOf('video_cover_image_url') > -1) { |
|
|
|
data.cover = await elem.getAttribute('content'); |
|
|
|
data.cover = await elem.getAttribute('content'); |
|
|
|
}else if (meta_name.indexOf('video_title') > -1) { |
|
|
|
}else if (meta_name.indexOf('video_title') > -1) { |
|
|
|
data.title = await elem.getAttribute('content'); |
|
|
|
data.title = await elem.getAttribute('content'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//手机版网页解析
|
|
|
|
|
|
|
|
const imgElem = await hero.querySelector('.video-container img.poster'); |
|
|
|
|
|
|
|
if (!imgElem) { |
|
|
|
|
|
|
|
console.error('HTML解析出错,找不到封面图', data); |
|
|
|
|
|
|
|
await hero.close(); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
data.cover = await imgElem.src; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//get cover image's base64 data
|
|
|
|
//get cover image's base64 data
|
|
|
|
if (typeof(data.cover) != 'undefined' && data.cover) { |
|
|
|
if (typeof(data.cover) != 'undefined' && data.cover) { |
|
|
@ -105,6 +113,11 @@ class Douyin extends HeroBot { |
|
|
|
}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(); |
|
|
|
await hero.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//删除profile文件后重试
|
|
|
|
|
|
|
|
await this.deleteProfile(); |
|
|
|
|
|
|
|
//切换模式
|
|
|
|
|
|
|
|
this.ua = this.ua == 'mob' ? 'pc' : 'mob'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return data; |
|
|
|
return data; |
|
|
|