Commit a296a1b3 by 马玮

Fix logs

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