Commit cad0d745 by 马玮

Add redis duplicate check

parent 76eca14b
'use strict';
const path = require('path');
const Redis = require('ioredis');
const crypto = require('crypto');
const config = require('config');
const log4js = require('log4js');
const poolCluster = require('./database/poolCluster');
// 自定义模块
global.__root = path.resolve(path.dirname(__dirname));
class DI {
......@@ -12,10 +13,6 @@ class DI {
this.params = params;
}
static md5(data, inputEncoding = 'utf8', outputEncoding = '') {
return crypto.createHash('md5').update(data, inputEncoding).digest('hex');
}
static dir() {
console.log(App.root);
console.log(path.parse(process.mainModule.filename).dir);
......@@ -34,7 +31,7 @@ class DI {
log4js.configure({
appenders: {
console: { type: 'console' },
carspider: { type: 'dateFile', filename: `${__basedir}/logs/dealer/carspider.log` },
carspider: { type: 'dateFile', filename: `${__basedir}/logs/dealer/carspider.log`, backups: 7 },
},
categories: {
default: { appenders: appenders, level: level }
......
'use strict';
const fs = require('fs');
const config = require('config');
const ruokuai = require('./ruokuai');
......@@ -10,7 +12,7 @@ captcha.decode = async (captchaType, imageFile) => {
typeid = typeids[captchaType];
const captchaText = ruokuai.decode(typeid, imageFile);
return captchaText;
}
};
captcha.decodeBase64 = async (captchaType, base64Text) => {
const imageFile = `${__basedir}/tmp/captcha/${(new Date).getTime()}.gif`;
......@@ -19,9 +21,10 @@ captcha.decodeBase64 = async (captchaType, base64Text) => {
if (fs.existsSync(imageFile) && fs.statSync(imageFile).isFile()) {
const captchaText = this.decode(captchaType, imageFile);
fs.unlinkSync(imageFile);
// TODO: 本地调试验证码时不删除验证码图片,上线时放开
// fs.unlinkSync(imageFile);
return captchaText;
} else {
throw new Error(`${imageFile} is not a file`);
}
}
};
'use strict';
const config = require('config');
const moment = require('moment');
const errors = require('../errors');
const Promise = require('bluebird');
const poolCluster = require('../database/poolCluster');
const DI = require('../DI');
var utils = require('../../lib/utils');
const errors = require('../errors');
const poolCluster = require('../database/poolCluster');
const logger = DI.getLogger();
class DealerHelper {
......@@ -116,7 +119,7 @@ class DealerHelper {
let sql;
// areaStrs.push('河南三门峡卢氏', '北京', '上海', '天津天津', '河南濮阳南乐', '河南郑州管城');
console.log(`${connection.escape(areaStrs.join())}`);
logger.info(`accountID: ${params.accountID}, 待匹配城市: ${JSON.stringify(areaStrs)}`);
const getAreas = async (areaStrs, areaType) => {
// ${connection.escape(areaStrs.join())}
......@@ -254,7 +257,7 @@ class DealerHelper {
static async getClueHashKey(clue, hashValue = false, checkExist = false, hashBase = false) {
hashBase = `${clue.dealerID} | ${clue.clueInputType} | ${clue.customerPhone} | ${clue.intentionDes} | ${clue.createDate}`;
const hashKey = 'spiderClue:' + DI.md5(hashBase);
const hashKey = 'spiderClue:' + utils.md5(hashBase);
if (hashValue) {
hashValue = JSON.stringify(clue);
......
......@@ -178,7 +178,7 @@ class Autohome extends Base {
const headerCookie = await DealerHelper.getHeaderCookie(this.params);
let rowcount = 0;
let rowCount = 0;
let rowNew = 0;
let rowDuplicate = 0;
let startTime;
......@@ -226,7 +226,7 @@ class Autohome extends Base {
const writeStream = fs.createWriteStream(`${__basedir}/tmp/workflow/${this.params.website}/clues.json`);
writeStream.write(JSON.stringify(res.data));
rowcount = clueData.result.rowcount || 0;
rowCount = clueData.result.rowcount || 0;
const clues = [];
for (let l of clueData.result.list) {
......@@ -291,7 +291,7 @@ class Autohome extends Base {
}
// 判断是否抓取完全
if (customerIndex < rowcount) {
if (customerIndex < rowCount) {
let randomSecs = randomInt(1, 5);
logger.info(`accountID: ${this.params.accountID}, 随机等待${randomSecs}秒`);
await Promise.delay(randomSecs*1000).then(async function() {
......@@ -303,7 +303,7 @@ class Autohome extends Base {
}
await fetchByPage();
await this.updateLatestFetch();
logger.info(`accountID: ${this.params.accountID}, 线索总数: ${rowcount}, 线索重复: ${rowDuplicate}, 新增线索: ${rowNew}`);
logger.info(`accountID: ${this.params.accountID}, 线索总数: ${rowCount}, 线索重复: ${rowDuplicate}, 新增线索: ${rowNew}`);
} catch (e) {
logger.error(`accountID: ${this.params.accountID}, ${e}`);
}
......
......@@ -182,7 +182,7 @@ class Easypass extends Base {
ignoreSSLErrors: true
});
let rowcount = 0;
let rowCount = 0;
let rowNew = 0;
let rowDuplicate = 0;
let txtBeginTime;
......@@ -218,6 +218,7 @@ class Easypass extends Base {
const $ = cheerio.load(html);
const clues = [];
const clueList = [];
const params = this.params;
$('#dataTable #grvNewCarOpportunity tr').not('.th').each(function(i, elem) {
const clue = {};
......@@ -238,11 +239,11 @@ class Easypass extends Base {
clue.createdAt = customerCreatedAtStr.replace('提交时间:', '').trim();
clue.createDate = moment(clue.createdAt).format('YYYY-MM-DD');
clue.salesmanName = $(this).find('td span[title]').text().trim();
clues.push(clue);
clueList.push(clue);
});
// cheerio each don't support async, so we have to add another loop
for (let clue of clues) {
for (let clue of clueList) {
let hashKeyExist;
let hashBase;
[clue.hashKey, clue.hashValue, hashKeyExist, hashBase] = await DealerHelper.getClueHashKey(_.assign(
......@@ -252,6 +253,8 @@ class Easypass extends Base {
logger.info(`accountID: ${this.params.accountID}, hashBase: ${hashBase}`);
rowCount += 1;
let flag;
if (hashKeyExist) {
rowDuplicate += 1;
......@@ -259,6 +262,7 @@ class Easypass extends Base {
} else {
rowNew += 1;
flag = '新增';
clues.push(clue);
}
logger.info(`accountID: ${this.params.accountID}, ${flag}线索: ${JSON.stringify(clue)}`);
......@@ -301,7 +305,7 @@ class Easypass extends Base {
await fetchByPage();
await horseman.close();
await this.updateLatestFetch();
logger.info(`accountID: ${this.params.accountID}, 线索总数: ${rowcount}, 线索重复: ${rowDuplicate}, 新增线索: ${rowNew}`);
logger.info(`accountID: ${this.params.accountID}, 线索总数: ${rowCount}, 线索重复: ${rowDuplicate}, 新增线索: ${rowNew}`);
} catch (e) {
logger.error(`accountID: ${this.params.accountID}, ${e}`);
}
......
'use strict';
const crypto = require('crypto');
exports.md5 = (data, inputEncoding = 'utf8', outputEncoding = '') => {
return crypto.createHash('md5').update(data, inputEncoding).digest('hex');
};
\ No newline at end of file
const Promise = require('bluebird');
var utils = require('./lib/utils');
console.log(utils.md5('欢迎来到王者荣耀'));
// const DI = require('./lib/DI');
// process.env.NODE_ENV = process.env.NODE_ENV || 'production';
// process.env.NODE_CONFIG_DIR = __dirname + '/config';
// const DI = require('./lib/DI');
// console.log(DI.md5('我来了'));
//
// const _ = require('lodash/core');
// const p = { p: '哎呦不错呦' , q: 'qq糖', c: 'c', d: 'd'};
......@@ -13,14 +15,21 @@ const Promise = require('bluebird');
// const moment = require('moment');
(async () => {
const aa = [1, 2, 3];
for (let [i,a] of aa.entries()) {
console.log(i,a);
if (a > 1) {
aa.splice(i,1);
}
const aa = ['a', 'b', 'c', 'd', 'e', 'f'];
for (let [k, v] of aa.entries()) {
aa.splice(k,1);
}
console.log(aa);
// for (i = 0; i < aa.length; i += 1) {
// // aa.splice(i, 1);
// delete aa[i];
// console.log(aa);
// }
// console.log('111111', aa);
// aa.splice(0,1);
// aa.splice(1,1);
// aa.splice(2,1);
// aa.splice(3,1);
console.log(aa.filter(() => {return true}));
})();
// (async () => {
......
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