Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
马玮
/
carspider
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
cad0d745
authored
Sep 01, 2017
by
马玮
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add redis duplicate check
parent
76eca14b
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
55 additions
and
31 deletions
+55
-31
lib/DI.js
+4
-7
lib/captcha/index.js
+6
-3
lib/helpers/Dealer.js
+7
-4
lib/spider/dealer/Autohome.js
+4
-4
lib/spider/dealer/Easypass.js
+8
-4
lib/utils/index.js
+8
-0
test.js
+18
-9
No files found.
lib/DI.js
View file @
cad0d745
'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
}
...
...
lib/captcha/index.js
View file @
cad0d745
'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`
);
}
}
}
;
lib/helpers/Dealer.js
View file @
cad0d745
'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
);
...
...
lib/spider/dealer/Autohome.js
View file @
cad0d745
...
...
@@ -178,7 +178,7 @@ class Autohome extends Base {
const
headerCookie
=
await
DealerHelper
.
getHeaderCookie
(
this
.
params
);
let
row
c
ount
=
0
;
let
row
C
ount
=
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
));
row
c
ount
=
clueData
.
result
.
rowcount
||
0
;
row
C
ount
=
clueData
.
result
.
rowcount
||
0
;
const
clues
=
[];
for
(
let
l
of
clueData
.
result
.
list
)
{
...
...
@@ -291,7 +291,7 @@ class Autohome extends Base {
}
// 判断是否抓取完全
if
(
customerIndex
<
row
c
ount
)
{
if
(
customerIndex
<
row
C
ount
)
{
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
}
, 线索总数:
${
row
c
ount
}
, 线索重复:
${
rowDuplicate
}
, 新增线索:
${
rowNew
}
`
);
logger
.
info
(
`accountID:
${
this
.
params
.
accountID
}
, 线索总数:
${
row
C
ount
}
, 线索重复:
${
rowDuplicate
}
, 新增线索:
${
rowNew
}
`
);
}
catch
(
e
)
{
logger
.
error
(
`accountID:
${
this
.
params
.
accountID
}
,
${
e
}
`
);
}
...
...
lib/spider/dealer/Easypass.js
View file @
cad0d745
...
...
@@ -182,7 +182,7 @@ class Easypass extends Base {
ignoreSSLErrors
:
true
});
let
row
c
ount
=
0
;
let
row
C
ount
=
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
();
clue
s
.
push
(
clue
);
clue
List
.
push
(
clue
);
});
// cheerio each don't support async, so we have to add another loop
for
(
let
clue
of
clue
s
)
{
for
(
let
clue
of
clue
List
)
{
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
}
, 线索总数:
${
row
c
ount
}
, 线索重复:
${
rowDuplicate
}
, 新增线索:
${
rowNew
}
`
);
logger
.
info
(
`accountID:
${
this
.
params
.
accountID
}
, 线索总数:
${
row
C
ount
}
, 线索重复:
${
rowDuplicate
}
, 新增线索:
${
rowNew
}
`
);
}
catch
(
e
)
{
logger
.
error
(
`accountID:
${
this
.
params
.
accountID
}
,
${
e
}
`
);
}
...
...
lib/utils/index.js
0 → 100644
View file @
cad0d745
'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
test.js
View file @
cad0d745
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 () => {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment