Commit a296a1b3 by 马玮

Fix logs

parent ed51b60f
...@@ -293,7 +293,7 @@ class DealerHelper { ...@@ -293,7 +293,7 @@ class DealerHelper {
area: clue.customerAreaID, area: clue.customerAreaID,
createTime: clue.createdAt createTime: clue.createdAt
}); });
logger.debug(`accountID: ${params.accountID}, 客户信息保存成功,customerID: ${clue.customerID}`); logger.debug(`accountID: ${params.accountID}, 客户信息保存成功, customerID: ${clue.customerID}`);
} catch (e) { } catch (e) {
// 如果没有customerID则跳过这条线索 // 如果没有customerID则跳过这条线索
logger.error(`accountID: ${params.accountID}, 客户信息保存失败: ${e}}`); logger.error(`accountID: ${params.accountID}, 客户信息保存失败: ${e}}`);
......
...@@ -32,14 +32,12 @@ class Autohome extends Base { ...@@ -32,14 +32,12 @@ class Autohome extends Base {
switch (this.params.task) { switch (this.params.task) {
case 'login': { case 'login': {
await this.login(); await this.login();
return;
} }
case 'tryLogin': { case 'tryLogin': {
await this.tryLogin(); await this.tryLogin();
return;
} }
case 'fetchClues': { case 'fetchClues': {
return await this.fetchClues(); await this.fetchClues();
} }
} }
} }
...@@ -70,8 +68,12 @@ class Autohome extends Base { ...@@ -70,8 +68,12 @@ class Autohome extends Base {
.viewport(1920, 1080) .viewport(1920, 1080)
.cookies(oldCookies) .cookies(oldCookies)
.open('http://ics.Autohome.com.cn/passport/Home/Index') .open('http://ics.Autohome.com.cn/passport/Home/Index')
.screenshot('tmp/workflow/autohome/tryLogin.png')
.exists('#loginout'); .exists('#loginout');
if (process.env.LOG_LEVEL === 'debug') {
await horseman.screenshot(`tmp/workflow/${this.params.website}/tryLogin.png`);
}
newCookies = await horseman.cookies(); newCookies = await horseman.cookies();
await horseman.close(); await horseman.close();
} }
...@@ -96,7 +98,7 @@ class Autohome extends Base { ...@@ -96,7 +98,7 @@ class Autohome extends Base {
// 随机返回登录状态 // 随机返回登录状态
// return [new errors.LoginError('登录失败'), randomInt(0, 1)]; // return [new errors.LoginError('登录失败'), randomInt(0, 1)];
try { try {
logger.info('开始表单登录'); logger.info(`accountID: ${this.params.accountID}, 开始表单登录`);
const username = this.params.username; const username = this.params.username;
const password = this.params.password; const password = this.params.password;
...@@ -111,23 +113,22 @@ class Autohome extends Base { ...@@ -111,23 +113,22 @@ class Autohome extends Base {
.viewport(1920, 1080) .viewport(1920, 1080)
.open('http://ics.Autohome.com.cn/passport/'); .open('http://ics.Autohome.com.cn/passport/');
horseman.on('onResourceRequested', (requestData, networkRequest) => { if (process.env.LOG_LEVEL === 'debug') {
});
// 验证码存储到磁盘
const base64Text =
await await
horseman horseman
.screenshot('tmp/workflow/autohome/beforeCaptcha.png') .screenshot(`tmp/workflow/${this.params.website}/beforeCaptcha.png`)
.crop('#imgValidCodeDealer', 'tmp/workflow/autohome/captcha.png') .crop('#imgValidCodeDealer', `tmp/workflow/${this.params.website}/captcha.png`);
.cropBase64('#imgValidCodeDealer', 'PNG'); }
// 验证码存储到磁盘
const base64Text = await horseman.cropBase64('#imgValidCodeDealer', 'PNG');
// 验证码解码 // 验证码解码
let captchaText; let captchaText;
try { try {
const captchaType = config.get(this.params.website)['captchaType']; const captchaType = config.get(this.params.website)['captchaType'];
captchaText = await captcha.decodeBase64(captchaType, base64Text); captchaText = await captcha.decodeBase64(captchaType, base64Text);
console.info('captchaText:', captchaText); logger.info(`accountID: ${this.params.accountID}, captchaText: ${captchaText}`);
} catch (e) { } catch (e) {
logger.error(e); logger.error(e);
return; return;
...@@ -138,8 +139,11 @@ class Autohome extends Base { ...@@ -138,8 +139,11 @@ class Autohome extends Base {
horseman horseman
.type('input[id="UserNameDealer"]', username) .type('input[id="UserNameDealer"]', username)
.type('input[id="PasswordDealer"]', password) .type('input[id="PasswordDealer"]', password)
.type('input[id="checkCodeDealer"]', captchaText) .type('input[id="checkCodeDealer"]', captchaText);
.screenshot('tmp/workflow/autohome/typeLoginForm.png');
if (process.env.LOG_LEVEL === 'debug') {
await horseman.screenshot(`tmp/workflow/${this.params.website}/typeLoginForm.png`);
}
// 提交登录表单 // 提交登录表单
const loginStatus = const loginStatus =
...@@ -147,10 +151,14 @@ class Autohome extends Base { ...@@ -147,10 +151,14 @@ class Autohome extends Base {
horseman horseman
.click('[id="btnDealer"]') .click('[id="btnDealer"]')
.wait(1000) .wait(1000)
// .waitForNextPage({ timeout: 10000 })
.screenshot('tmp/workflow/autohome/submitLoginForm.png')
.exists('#loginout'); .exists('#loginout');
if (process.env.LOG_LEVEL === 'debug') {
// waitForNextPage({ timeout: 10000 })
// TODO: 找到不能用waitForNextPage的原因
await horseman.screenshot(`tmp/workflow/${this.params.website}/afterLoginForm.png`);
}
if (loginStatus) { if (loginStatus) {
const cookies = await horseman.cookies(); const cookies = await horseman.cookies();
const params = this.params; const params = this.params;
...@@ -179,8 +187,8 @@ class Autohome extends Base { ...@@ -179,8 +187,8 @@ class Autohome extends Base {
try { try {
await this.retryLogin(); await this.retryLogin();
} catch (e) { } catch (e) {
// TODO:需要考虑密码、验证码等因素 // TODO: 需要考虑密码、验证码等因素
// TODO:如果抓取线索过程中cookie失效可能也需要尝试重新登录 // TODO: 如果抓取线索过程中cookie失效可能也需要尝试重新登录
logger.error(`accountID: ${this.params.accountID}, 抓取线索登录失败: ${e}`); logger.error(`accountID: ${this.params.accountID}, 抓取线索登录失败: ${e}`);
return; return;
} }
...@@ -211,9 +219,9 @@ class Autohome extends Base { ...@@ -211,9 +219,9 @@ class Autohome extends Base {
params.assignDealerId = 0; params.assignDealerId = 0;
const fetchByPage = async () => { const fetchByPage = async () => {
if (!rowcount) { if (!rowcount) {
logger.info(`线索日期: ${params.startTime} - ${params.endtime}`); logger.info(`accountID: ${this.params.accountID}, 线索日期: ${params.startTime} - ${params.endtime}`);
} }
logger.info(`线索页面: ${params.pageIndex}`); logger.info(`accountID: ${this.params.accountID}, 线索页面: ${params.pageIndex}`);
await axios.get('http://ics.Autohome.com.cn/Dms/Order/SearchOrderList', { await axios.get('http://ics.Autohome.com.cn/Dms/Order/SearchOrderList', {
params: params, params: params,
headers:{ headers:{
...@@ -222,12 +230,12 @@ class Autohome extends Base { ...@@ -222,12 +230,12 @@ class Autohome extends Base {
}).then(async res => { }).then(async res => {
const clueData = res.data; const clueData = res.data;
const writeStream = fs.createWriteStream('tmp/workflow/autohome/clues.html'); const writeStream = fs.createWriteStream(`tmp/workflow/${this.params.website}/clues.json`);
writeStream.write(JSON.stringify(res.data)); writeStream.write(JSON.stringify(res.data));
if (!rowcount) { if (!rowcount) {
rowcount = clueData.result.rowcount; rowcount = clueData.result.rowcount;
logger.info('线索总数:', rowcount); logger.info(`accountID: ${this.params.accountID}, 线索总数: ${rowcount}`);
} }
const clues = []; const clues = [];
...@@ -261,7 +269,7 @@ class Autohome extends Base { ...@@ -261,7 +269,7 @@ class Autohome extends Base {
// logger.debug('线索类型:', clue.orderType); // logger.debug('线索类型:', clue.orderType);
// logger.debug('线索状态:', clue.orderState); // logger.debug('线索状态:', clue.orderState);
logger.debug(JSON.stringify(clue)); logger.debug(`accountID: ${this.params.accountID}, 线索: ${JSON.stringify(clue)}`);
logger.debug('==============================================================================='); logger.debug('===============================================================================');
} }
...@@ -276,8 +284,8 @@ class Autohome extends Base { ...@@ -276,8 +284,8 @@ class Autohome extends Base {
// 判断是否抓取完全 // 判断是否抓取完全
if (customerIndex < rowcount) { if (customerIndex < rowcount) {
let randomSecs = randomInt(1, 2); let randomSecs = randomInt(1, 5);
logger.info(`随机等待${randomSecs}秒`); logger.info(`accountID: ${this.params.accountID}, 随机等待${randomSecs}秒`);
await Promise.delay(randomSecs*1000).then(async function() { await Promise.delay(randomSecs*1000).then(async function() {
params.pageIndex += 1; params.pageIndex += 1;
await fetchByPage(); await fetchByPage();
...@@ -286,10 +294,8 @@ class Autohome extends Base { ...@@ -286,10 +294,8 @@ class Autohome extends Base {
}); });
} }
await fetchByPage(); await fetchByPage();
return;
} catch (e) { } catch (e) {
console.dir(e); logger.error(e);
// throw e;
} }
} }
} }
......
...@@ -70,8 +70,12 @@ class Easypass extends Base { ...@@ -70,8 +70,12 @@ class Easypass extends Base {
.viewport(1920, 1080) .viewport(1920, 1080)
.cookies(oldCookies) .cookies(oldCookies)
.open('http://dealer.Easypass.cn/HomeIndex.aspx') .open('http://dealer.Easypass.cn/HomeIndex.aspx')
.screenshot('tmp/workflow/easypass/tryLogin.png')
.exists('#myAccountLi'); .exists('#myAccountLi');
if (process.env.LOG_LEVEL === 'debug') {
await horseman.screenshot(`tmp/workflow/${this.params.website}/tryLogin.png`);
}
newCookies = await horseman.cookies(); newCookies = await horseman.cookies();
await horseman.close(); await horseman.close();
} }
...@@ -94,7 +98,7 @@ class Easypass extends Base { ...@@ -94,7 +98,7 @@ class Easypass extends Base {
// 随机返回登录状态 // 随机返回登录状态
// return randomInt(0, 1); // return randomInt(0, 1);
// return [new errors.LoginError('登录失败'), randomInt(0, 1)]; // return [new errors.LoginError('登录失败'), randomInt(0, 1)];
console.log('开始表单登录'); logger.info(`accountID: ${this.params.accountID}, 开始表单登录`);
const username = this.params.username; const username = this.params.username;
const password = this.params.password; const password = this.params.password;
...@@ -108,25 +112,25 @@ class Easypass extends Base { ...@@ -108,25 +112,25 @@ class Easypass extends Base {
.userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0') .userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0')
.viewport(1920, 1080) .viewport(1920, 1080)
.open('http://dealer.Easypass.cn/LoginPage/DefaultLogin.aspx'); .open('http://dealer.Easypass.cn/LoginPage/DefaultLogin.aspx');
console.log('打开了登录页');
// 验证码存储到磁盘 if (process.env.LOG_LEVEL === 'debug') {
const base64Text =
await await
horseman horseman
.screenshot('tmp/workflow/easypass/beforeCaptcha.png') .screenshot(`tmp/workflow/${this.params.website}/beforeCaptcha.png`)
.crop('#imgVerificationCode', 'tmp/workflow/easypass/captcha.png') .crop('#imgVerificationCode', `tmp/workflow/${this.params.website}/captcha.png`);
.cropBase64('#imgVerificationCode', 'PNG'); }
console.log('验证码准备完毕');
// 验证码存储到磁盘
const base64Text = await horseman.cropBase64('#imgVerificationCode', 'PNG');
// 验证码解码 // 验证码解码
let captchaText; let captchaText;
try { try {
const captchaType = config.get(this.params.website)['captchaType']; const captchaType = config.get(this.params.website)['captchaType'];
captchaText = await captcha.decodeBase64(captchaType, base64Text); captchaText = await captcha.decodeBase64(captchaType, base64Text);
console.info('captchaText:', captchaText); logger.info(`accountID: ${this.params.accountID}, captchaText: ${captchaText}`);
} catch (e) { } catch (e) {
console.log(e.toString()); logger.error(e);
return; return;
} }
...@@ -135,20 +139,26 @@ class Easypass extends Base { ...@@ -135,20 +139,26 @@ class Easypass extends Base {
horseman horseman
.type('input[id="txtUserName"]', username) .type('input[id="txtUserName"]', username)
.type('input[id="txtPwd"]', password) .type('input[id="txtPwd"]', password)
.type('input[id="txtVerificationCode"]', captchaText) .type('input[id="txtVerificationCode"]', captchaText);
.screenshot('tmp/workflow/easypass/typeLoginForm.png');
if (process.env.LOG_LEVEL === 'debug') {
await horseman.screenshot(`tmp/workflow/${this.params.website}/typeLoginForm.png`);
}
// 提交登录表单 // 提交登录表单
const loginStatus = const loginStatus =
await await
horseman horseman
.click('[id="btnLogin"]') .click('[id="btnLogin"]')
.screenshot('tmp/workflow/easypass/beforeSubmitLoginForm.png')
.wait(1000) .wait(1000)
// .waitForNextPage({ timeout: 5000 })
.screenshot('tmp/workflow/easypass/submitLoginForm.png')
.exists('#myAccountLi'); .exists('#myAccountLi');
if (process.env.LOG_LEVEL === 'debug') {
// waitForNextPage({ timeout: 10000 })
// TODO: 找到不能用waitForNextPage的原因
await horseman.screenshot(`tmp/workflow/${this.params.website}/afterLoginForm.png`);
}
if (loginStatus) { if (loginStatus) {
const cookies = await horseman .cookies(); const cookies = await horseman .cookies();
const params = this.params; const params = this.params;
...@@ -190,11 +200,6 @@ class Easypass extends Base { ...@@ -190,11 +200,6 @@ class Easypass extends Base {
ignoreSSLErrors: true ignoreSSLErrors: true
}); });
horseman.on('onResourceRequested', (requestData, networkRequest) => {
console.info('Requesting', requestData.url);
console.log('networkRequest', networkRequest);
});
const txtBeginTime = moment().subtract(15, 'days').format('YYYY-MM-DD'); const txtBeginTime = moment().subtract(15, 'days').format('YYYY-MM-DD');
await await
...@@ -207,16 +212,19 @@ class Easypass extends Base { ...@@ -207,16 +212,19 @@ class Easypass extends Base {
.waitForSelector('#pager1') .waitForSelector('#pager1')
.type('input[id="txtBeginTime"]', txtBeginTime) .type('input[id="txtBeginTime"]', txtBeginTime)
.click('input[id="btnSearch"]') .click('input[id="btnSearch"]')
.wait(1000) .wait(1000);
// .waitForNextPage()
.screenshot('tmp/workflow/easypass/clues.png');
// TODO: if waitfor failed need some error check // TODO: if waitfor failed need some error check
if (process.env.LOG_LEVEL === 'debug') {
await horseman.screenshot(`tmp/workflow/${this.params.website}/clues.png`);
}
const fetchByPage = async () => { const fetchByPage = async () => {
const html = await horseman.html('#UpdatePanel1'); const html = await horseman.html('#UpdatePanel1');
const $ = cheerio.load(html); const $ = cheerio.load(html);
const clues = []; const clues = [];
const params = this.params;
$('#dataTable #grvNewCarOpportunity tr').not('.th').each(function(i, elem) { $('#dataTable #grvNewCarOpportunity tr').not('.th').each(function(i, elem) {
const clue = {}; const clue = {};
// 主体信息样本:赵永亮[河南新乡],电话15249705814,订购大迈X5升级版 1.5T 手动 精英型2017款 // 主体信息样本:赵永亮[河南新乡],电话15249705814,订购大迈X5升级版 1.5T 手动 精英型2017款
...@@ -237,7 +245,7 @@ class Easypass extends Base { ...@@ -237,7 +245,7 @@ class Easypass extends Base {
clue.salesmanName = $(this).find('td span[title]').text().trim(); clue.salesmanName = $(this).find('td span[title]').text().trim();
clues.push(clue); clues.push(clue);
logger.debug(JSON.stringify(clue)); logger.debug(`accountID: ${params.accountID}, 线索: ${JSON.stringify(clue)}`);
logger.debug('==============================================================================='); logger.debug('===============================================================================');
}); });
...@@ -253,16 +261,20 @@ class Easypass extends Base { ...@@ -253,16 +261,20 @@ class Easypass extends Base {
// 判断是否最后一页有两种情况:1.只有一页时分页按钮不存在 2.末页按钮置灰 // 判断是否最后一页有两种情况:1.只有一页时分页按钮不存在 2.末页按钮置灰
const isLastPage = !$('#pager1').length || $('#pager1 a:contains(末页)').prop('disabled'); const isLastPage = !$('#pager1').length || $('#pager1 a:contains(末页)').prop('disabled');
if (!isLastPage) { if (!isLastPage) {
if (process.env.LOG_LEVEL === 'debug') {
await horseman.screenshot(`tmp/workflow/${this.params.website}/beforeNextPage.png`);
}
await horseman await horseman
.click('a.paginator:contains(下一页)') .click('a.paginator:contains(下一页)')
.screenshot('tmp/workflow/easypass/bedoreNextPage.png')
// .waitForNextPage()
.wait(1000) .wait(1000)
.waitForSelector('#UpdatePanel1') .waitForSelector('#UpdatePanel1');
.screenshot('tmp/workflow/easypass/nextPage.png');
let randomSecs = randomInt(1, 2); if (process.env.LOG_LEVEL === 'debug') {
console.log(`随机等待${randomSecs}秒`); await horseman.screenshot(`tmp/workflow/${this.params.website}/nextPage.png`);
}
let randomSecs = randomInt(1, 5);
logger.info(`accountID: ${this.params.accountID}, 随机等待${randomSecs}秒`);
await Promise.delay(randomSecs*1000).then(async function() { await Promise.delay(randomSecs*1000).then(async function() {
await fetchByPage(); await fetchByPage();
}); });
...@@ -270,12 +282,11 @@ class Easypass extends Base { ...@@ -270,12 +282,11 @@ class Easypass extends Base {
} }
await fetchByPage(); await fetchByPage();
await horseman.close(); await horseman.close();
return;
} else { } else {
logger.error(`accountID: ${this.params.accountID}, ${new errors.AppError('cookies读取失败')}`); logger.error(`accountID: ${this.params.accountID}, ${new errors.AppError('cookies读取失败')}`);
} }
} catch (e) { } catch (e) {
console.log(e.toString()); logger.error(e);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment