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
856ddd7e
authored
Aug 29, 2017
by
马玮
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add latest fetch point
parent
cb218239
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
29 deletions
+45
-29
lib/helpers/Dealer.js
+9
-10
lib/spider/Base.js
+11
-0
lib/spider/dealer/Autohome.js
+11
-8
lib/spider/dealer/Easypass.js
+12
-11
sql/history.sql
+2
-0
No files found.
lib/helpers/Dealer.js
View file @
856ddd7e
...
...
@@ -258,15 +258,16 @@ class DealerHelper {
// 保存线索
static
async
saveClues
(
params
)
{
return
new
Promise
(
async
(
resolve
,
reject
)
=>
{
try
{
const
dealerID
=
params
.
dealerID
||
0
;
const
website
=
params
.
website
||
''
;
let
clues
=
params
.
clues
||
[];
if
(
!
dealerID
||
!
clues
.
length
||
!
website
)
{
thro
w
errors
.
AppError
(
'Params missing'
);
throw
ne
w
errors
.
AppError
(
'Params missing'
);
}
clues
=
await
this
.
mapClues
(
params
).
catch
((
e
)
=>
{
console
.
log
(
e
);
logger
.
error
(
`accountID:
${
params
.
accountID
}
, 线索信息匹配失败:
${
e
}
}`
);
});
const
connection
=
await
DI
.
getConnection
(
'MASTER_CRM'
);
...
...
@@ -338,13 +339,7 @@ class DealerHelper {
await
this
.
saveSummary
(
params
);
logger
.
info
(
`accountID:
${
params
.
accountID
}
, 线索统计保存成功`
);
}
catch
(
e
)
{
logger
.
info
(
`accountID:
${
params
.
accountID
}
, 线索统计保存失败`
);
}
try
{
await
this
.
updateLatestFetch
(
params
);
logger
.
info
(
`accountID:
${
params
.
accountID
}
, 抓取时间更新成功`
);
}
catch
(
e
)
{
logger
.
info
(
`accountID:
${
params
.
accountID
}
, 抓取时间更新失败`
);
logger
.
info
(
`accountID:
${
params
.
accountID
}
, 线索统计保存失败,
${
e
}
`
);
}
resolve
(
true
);
...
...
@@ -353,6 +348,9 @@ class DealerHelper {
}
}
);
}
catch
(
e
)
{
reject
(
e
);
}
});
}
...
...
@@ -386,8 +384,9 @@ class DealerHelper {
return
new
Promise
(
async
(
resolve
,
reject
)
=>
{
try
{
const
sql
=
`UPDATE crm_dealer_thirdparty_account SET
latestClueAt = (SELECT MAX(create_time) FROM crm_clue WHERE clue_input_type = ? AND dealerID = ?),
latestFetchAt = UNIX_TIMESTAMP() WHERE id = ?`
;
const
replacements
=
[
params
.
accountID
];
const
replacements
=
[
config
.
get
(
'CLUE_INPUT_TYPE'
)[
params
.
website
],
params
.
dealerID
,
params
.
accountID
];
const
connection
=
await
DI
.
getConnection
(
'MASTER_CRM'
);
connection
.
query
(
sql
,
...
...
lib/spider/Base.js
View file @
856ddd7e
...
...
@@ -49,6 +49,16 @@ class Base {
throw
new
errors
.
LoginError
(
'登录失败'
);
}
}
// 更新抓取时间
async
updateLatestFetch
()
{
try
{
await
DealerHelper
.
updateLatestFetch
(
this
.
params
);
logger
.
info
(
`accountID:
${
this
.
params
.
accountID
}
, 抓取时间更新成功`
);
}
catch
(
e
)
{
logger
.
error
(
`accountID:
${
this
.
params
.
accountID
}
, 抓取时间更新失败,
${
e
}
`
);
}
}
}
module
.
exports
=
Base
;
\ No newline at end of file
lib/spider/dealer/Autohome.js
View file @
856ddd7e
...
...
@@ -198,11 +198,11 @@ class Autohome extends Base {
let
startTime
;
let
customerIndex
=
0
;
//
读取上次抓取时间点
if
(
this
.
params
.
latest
Fetch
At
)
{
startTime
=
moment
.
unix
(
this
.
params
.
latest
Fetch
At
).
format
(
'YYYY.MM.DD'
);
//
最新线索时间点作为抓取开始时间
if
(
this
.
params
.
latest
Clue
At
)
{
startTime
=
moment
.
unix
(
this
.
params
.
latest
Clue
At
).
format
(
'YYYY.MM.DD'
);
}
else
{
startTime
=
moment
().
subtract
(
3
0
,
'days'
).
format
(
'YYYY.MM.DD'
);
startTime
=
moment
().
subtract
(
3
,
'days'
).
format
(
'YYYY.MM.DD'
);
}
const
params
=
{};
...
...
@@ -276,17 +276,19 @@ class Autohome extends Base {
// logger.debug('线索类型:', clue.orderType);
// logger.debug('线索状态:', clue.orderState);
logger
.
debug
(
`accountID:
${
this
.
params
.
accountID
}
, 线索:
${
JSON
.
stringify
(
clue
)}
`
);
logger
.
debug
(
'==============================================================================='
);
logger
.
info
(
`accountID:
${
this
.
params
.
accountID
}
, 线索:
${
JSON
.
stringify
(
clue
)}
`
);
logger
.
info
(
'==============================================================================='
);
}
if
(
this
.
params
.
dealerID
)
{
if
(
clues
.
length
>
0
)
{
await
DealerHelper
.
saveClues
({
clues
:
clues
,
dealerID
:
this
.
params
.
dealerID
,
website
:
this
.
params
.
website
,
accountID
:
this
.
params
.
accountID
,
});
}
else
{
logger
.
info
(
`accountID:
${
this
.
params
.
accountID
}
, 时间段(
${
params
.
startTime
}
-
${
params
.
endtime
}
)没有线索`
);
}
// 判断是否抓取完全
...
...
@@ -301,8 +303,9 @@ class Autohome extends Base {
});
}
await
fetchByPage
();
await
this
.
updateLatestFetch
();
}
catch
(
e
)
{
logger
.
error
(
e
);
logger
.
error
(
`accountID:
${
this
.
params
.
accountID
}
,
${
e
}
`
);
}
}
}
...
...
lib/spider/dealer/Easypass.js
View file @
856ddd7e
...
...
@@ -194,15 +194,16 @@ class Easypass extends Base {
logger
.
debug
(
e
);
}
if
(
cookies
)
{
if
(
!
cookies
)
throw
new
errors
.
AppError
(
'抓取线索cookies读取失败'
);
const
horseman
=
new
Horseman
({
ignoreSSLErrors
:
true
});
// 上次抓取时间点
// 最新线索时间点作为抓取开始时间
let
txtBeginTime
;
if
(
this
.
params
.
latestFetch
At
)
{
txtBeginTime
=
moment
.
unix
(
this
.
params
.
latestFetch
At
).
format
(
'YYYY-MM-DD'
);
if
(
this
.
params
.
latestClue
At
)
{
txtBeginTime
=
moment
.
unix
(
this
.
params
.
latestClue
At
).
format
(
'YYYY-MM-DD'
);
}
else
{
txtBeginTime
=
moment
().
subtract
(
3
,
'days'
).
format
(
'YYYY-MM-DD'
);
}
...
...
@@ -253,17 +254,19 @@ class Easypass extends Base {
clue
.
salesmanName
=
$
(
this
).
find
(
'td span[title]'
).
text
().
trim
();
clues
.
push
(
clue
);
logger
.
debug
(
`accountID:
${
params
.
accountID
}
, 线索:
${
JSON
.
stringify
(
clue
)}
`
);
logger
.
debug
(
'==============================================================================='
);
logger
.
info
(
`accountID:
${
params
.
accountID
}
, 线索:
${
JSON
.
stringify
(
clue
)}
`
);
logger
.
info
(
'==============================================================================='
);
});
if
(
this
.
params
.
dealerID
)
{
if
(
clues
.
length
>
0
)
{
await
DealerHelper
.
saveClues
({
clues
:
clues
,
dealerID
:
this
.
params
.
dealerID
,
website
:
this
.
params
.
website
,
accountID
:
this
.
params
.
accountID
,
});
}
else
{
logger
.
info
(
`accountID:
${
this
.
params
.
accountID
}
, 时间段(
${
txtBeginTime
}
-
${
txtEndTime
}
)没有线索`
);
}
// 判断是否最后一页有两种情况:1.只有一页时分页按钮不存在 2.末页按钮置灰
...
...
@@ -290,11 +293,9 @@ class Easypass extends Base {
}
await
fetchByPage
();
await
horseman
.
close
();
}
else
{
logger
.
error
(
`accountID:
${
this
.
params
.
accountID
}
,
${
new
errors
.
AppError
(
'cookies读取失败'
)}
`
);
}
await
this
.
updateLatestFetch
();
}
catch
(
e
)
{
logger
.
error
(
e
);
logger
.
error
(
`accountID:
${
this
.
params
.
accountID
}
,
${
e
}
`
);
}
}
}
...
...
sql/history.sql
View file @
856ddd7e
...
...
@@ -16,6 +16,8 @@ CREATE TABLE `crm_dealer_thirdparty_account` (
`cookies`
text
COMMENT
'第三方账号cookies'
,
`error`
text
COMMENT
'爬取第三方过程中产生的错误'
,
`status`
tinyint
(
255
)
DEFAULT
'0'
COMMENT
'第三方账号状态: -1: 同步失败, 0: 未同步, 1: 同步成功'
,
`latestClueAt`
bigint
(
255
)
DEFAULT
NULL
COMMENT
'最新线索时间'
,
`latestFetchAt`
bigint
(
255
)
DEFAULT
NULL
COMMENT
'最近抓取时间'
,
`createdAt`
bigint
(
20
)
DEFAULT
NULL
COMMENT
'第三方账号创建时间'
,
`updatedAt`
bigint
(
20
)
DEFAULT
NULL
COMMENT
'第三方账号更新时间'
,
PRIMARY
KEY
(
`id`
),
...
...
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