index.js
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const rp = require('request-promise');
const util = require(`${ROOT_PATH}/libs/util`);
const senUrl = 'http://data.zz.baidu.com/urls?appid=1583402501013173&token=K0L5PUhk1XOko81r&type=';
class XzhIndexModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
index() {
// 新增
return this.getLastArticleList({page: 1, limit: 100}).then(rdata => {
rdata = _.get(rdata, 'data.artList', []);
return this.sendData(rdata, 'realtime');
});
}
sendDataHistory(page) {
return util.sleep(3000).then(() => {
return this.getLastArticleList({page: 1, limit: 1000});
}).then(rdata => {
let code = rdata.code || 400;
rdata = _.get(rdata, 'data.artList', []);
if (code === 200 && rdata.length <= 0) {
return false;
}
return this.sendData(rdata).then(result => {
console.log(`sendDataHistory => result: ${JSON.stringify(result)}, page: ${page} `);
return this.sendDataHistory(++page);
});
});
}
// 向百度发送数据
sendData(data, type) {
if (data.length <= 0) {
return Promise.resolve({code: 400, data:{}, message: 'data is empty'});
}
type = type || 'batch';
data = _.map(data, item => {
return `https://m.yohobuy.com/mip/guang/${item.articleId}.html`;
});
return rp({
method: 'POST',
uri: `${senUrl}${type}`,
body: data.join('\n'),
timeout: 8 * 1000
}).then(result => {
return JSON.parse(result || '{}');
}).catch(e => {
console.log(e.message);
return {code: 400, data: {}, message: e.message};
});
}
getLastArticleList(params) {
params = _.assign({
limit: 100,
udid: 'seo_format_data'
}, params);
return this.get({
url: '/guang/api/v2/article/getLastArticleList',
data: params,
api: global.yoho.ServiceAPI
}).catch(e => {
console.log(e.message);
return {code: 400, data: {}, message: e.message};
});
}
}
module.exports = XzhIndexModel;