index.js
3.04 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
const ROOT_PATH = global.ROOT_PATH;
const _ = require('lodash');
const co = Promise.coroutine;
const rp = require('request-promise');
const redis = require(`${ROOT_PATH}/libs/redis`);
const YohoApiModel = require('./../interface/yoho-api');
const TaskLogModel = require('./../interface/task-log');
const senUrl = 'http://data.zz.baidu.com/urls?appid=1583402501013173&token=K0L5PUhk1XOko81r&type=';
// http://ziyuan.baidu.com/xzh/commit/push?appid=1583402501013173&qq-pf-to=pcqq.c2c
class XzhIndexModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.yohoApiModel = new YohoApiModel(ctx);
this.taskLogModel = new TaskLogModel(ctx);
}
index() {
let that = this;
return co(function* () {
let ids = [];
let message = '';
// 新增
let rdata = yield that.yohoApiModel.getLastArticleList({page: 1, limit: 100});
rdata = _.get(rdata, 'data.artList', []);
ids = _.map(rdata, item => {
return `${item.articleId}`;
});
let artice = {
total: ids.length,
notAlready: 0
};
ids = _.difference(ids, yield redis.hmgetAsync('global:yoho:seo:xzh:guang', ids));// 去除已经推送的
artice.notAlready = ids.length;
message = `获取${artice.total}条记录,未推送的有${artice.notAlready}条记录`;
if (artice.notAlready <= 0) {
return {code: 201, data: {}, message: message};
}
rdata = yield that.sendData(ids, 'realtime');
if (rdata.code !== 200) {
return rdata;
}
let tids = [];
_.each(ids, id => {
tids.push(id, id);
});
yield redis.hmsetAsync('global:yoho:seo:xzh:guang', tids);
ids = [];
tids = [];
return Object.assign(rdata, {message: message});
})().then(rdata => {
let key = this.taskLogModel.getKey('http://127.0.0.1:6005/sendXzh');
return this.taskLogModel.ltrim(key).then(() => {
return this.taskLogModel.add(key, rdata);
}).then(() => {
return rdata;
});
});
}
// 向百度发送数据
sendData(data, type) {
if (data.length <= 0) {
return Promise.resolve({code: 400, data: {}, message: 'data is empty'});
}
type = type || 'batch';
data = _.map(data, id => {
return `https://m.yohobuy.com/mip/guang/${id}.html`;
});
return rp({
method: 'POST',
uri: `${senUrl}${type}`,
body: data.join('\n'),
timeout: 8 * 1000
}).then(result => {
return Object.assign({code: 200}, JSON.parse(result || '{}'));
}).catch(e => {
console.log(e.message);
return {code: 400, data: {}, message: e.message};
});
}
}
module.exports = XzhIndexModel;