-
Notifications
You must be signed in to change notification settings - Fork 1
/
repost.js
280 lines (247 loc) · 8.06 KB
/
repost.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
var fs = require('fs');
var settings = require(__dirname + '/etc/settings.json');
var url = require('url');
var de = require('devent').createDEvent('sender');
var queue = require('queuer');
var logger = require('./lib/logger').logger(settings.logFile);
var util = require('util');
var event = require('events').EventEmitter;
var tool = require('./lib/tool').tool;
var async = require('async');
var redis = require("redis");
var redisCli = redis.createClient(settings.redis.port, settings.redis.host);
//发送队列的API
var rq = queue.getQueue('http://'+settings.queue.host+':'+settings.queue.port+'/queue', settings.queue.repost);
var Reposter = require('./lib/reposter_v3').Reposter;
var reposter = new Reposter();
reposter.init(settings);
//发送对象保存在该数组中
var senders = [];
var db = require('./lib/db').db;
db.init(settings);
//所有微博账号
var weiboAccounts = {};
db.loadAccounts(function(err, accounts){
if(err){
console.log('!!!load account error!!!');
return;
}
weiboAccounts = accounts;
console.log('access token loaded');
//由于发送依赖账号,所以必须先加载完账号才能开始处理发送请求
console.log('starting dequeue');
start();
});
var taskBack = function(task, status){
if(status){
de.emit('task-finished', task);
}else{
de.emit('task-error', task);
}
}
var repost = function(task, callback){
db.getRepostTask(task.uri, function(err, record, sent){
var context = {task:task,record:record};
if(err){
console.log(['fetch repost task error', record, err]);
//要转发的微博不存在或者没有发送,并且超过3小时,放弃这个任务
err.nextAction = 'drop';
complete(err, null, '', '', context);
taskBack(task, true);
callback();
dequeue();
return;
}
if(tool.timestamp() - record.in_time > 21600){
complete({number:8000,message:'waiting more than 6 hour'}, null, '', '', context);
taskBack(task, true);
callback();
dequeue();
return;
}
var status = '';
if(record.content){
status = record.content;
} else if (record.title){
status = record.title;
}
if(status.match(/(公告|:|:|【|\[)/)){
logger.info("error\t" + record.id + "\t" + "\tthe title is invalid");
taskBack(task, true);
callback();
dequeue();
return;
}
//debug模式下,总是使用[email protected]发送微博
if(settings.mode == 'debug'){
record.stock_code= 'sz900000';
}
record.stock_code = record.stock_code.toLowerCase();
//微博账号错误
var account = getAccount(record.stock_code, sent);
if(!account || !account.weibo_center_id){
logger.info("error\t" + record.id + "\t" + record.stock_code+ "\tNOT Found the account\t");
taskBack(task, true);
callback();
dequeue();
return;
}
var cb = function(error, body, id, status, context){
taskBack(context.task, complete(error, body, id, status, context));
callback();
dequeue();
}
context.user = account;
//限速,不再做任何处理,等到任务超时重新入队
sendAble(record.stock_code, sent.weibo_id, account.id, function(err, result){
if(err){
if (err.msg == 'reposted') {
logger.info("error\trepeat\t" + sent.weibo_id+ "\t" + account.id);
taskBack(task, true);
}
callback();
dequeue();
}else{
reposter.repost(sent.weibo_id, status, account, context, cb);
}
});
});
};
var getAccount = function (stockCode, sent) {
if(settings.mode == 'debug') {
stockCode = 'sz900000';
}
var sentAccount = weiboAccounts.ids[sent.account_id];
if(!sentAccount) {
return null;
}
if(weiboAccounts.stocks[stockCode]) {
return weiboAccounts.stocks[stockCode][sentAccount.provider];
}
return null;
}
//限速
var lockedAccounts = {};
var sendAble = function(stockCode, weiboId, accountId, callback){
lockedAccounts[accountId] = accountId;
var reposted = function (cb) {
db.getRepostRecord(accountId, weiboId, function (err, result) {
if(err || result.length != 0) {
cb({msg:'reposted'});
}else {
cb();
}
});
};
var accountAble = function (cb) {
var limited = function(lcb){
var ts = tool.timestamp();
var key = "SEND_LIMIT_" + accountId;
redisCli.get(key, function(err, lastSend){
if(!lastSend){
redisCli.setex(key, 180, ts);
lcb(null, true);
}else{
lcb({msg:'limit'}, false);
}
});
}
if(stockCode == 'a_stock'){
redisCli.get('a_stock_send_' + accountId, function(err, count){
if(count){
cb({msg:'limit_astock'}, false);
}else{
limited(cb);
}
});
}else{
limited(cb);
}
};
async.series ([reposted, accountAble], function (err, result) {
delete lockedAccounts[accountId];
callback(err);
});
}
var dequeue = function(){
if(aq.length() >= settings.reposterCount){
return;
}
rq.dequeue(function(err, task){
if(err == 'empty' || task == undefined){
console.log('repost queue is empty');
return;
}
//console.log(['dequeue', task]);
aq.push(task);
});
}
var start = function(){
setInterval(function(){
dequeue();
}, settings.queue.interval);
de.on('queued', function( queue ){
if(queue == settings.queue.repost){
console.log( queue + "有内容");
dequeue();
}
});
console.log('sender start ok');
}
/**
发送结束后的处理,返回true表示发送完成
*/
var complete = function(error, body, weiboId, status, context){
var record = context.record || {};
var task = context.task;
if(!error){
body.t_url = '';
logger.info("success\t" + record.id + "\t" + record.stock_code + "\t" +context.user.id+ "\t" + weiboId + "\t" + body.id);
db.reposted(record, body.id, body.t_url, weiboId, context.user.id, function(err, info){
if(err){
console.log([err, info]);
}
});
return true;
}
if(!error.error_code){
error.error_code = '70000';
}
var errMsg = error.error || error.message;
logger.info("error\t" + record.id +"\t"+ weiboId + "\t" + record.stock_code + "\t" + errMsg);
//发送受限制
if(error.nextAction == 'delay' && task.retry < settings.queue.retry){
return false;
//40013太长, 40025重复
}else{
if(error.nextAction == 'drop' || task.retry >= settings.queue.retry){
return true;
}else{
return false;
}
}
}
var aq = async.queue(repost, settings.reposterCount);
fs.writeFileSync(__dirname + '/server.pid', process.pid.toString(), 'ascii');
//收到进程信号重新初始化
process.on('SIGUSR2', function () {
settings = require('./etc/settings.json');
db.init(settings);
for(i = 0; i < senders.length; i++){
senders[i].init(settings);
}
});
/*
process.on('uncaughtException', function(e){
console.log(['unkonwn exception:', e]);
});
*/
console.log('reposter start at ' + tool.getDateString() + ', pid is ' + process.pid);
/**
* 测试代码
setTimeout(function(){
var task = {uri:'mysql://abc.com/dddd#142', retry:0};
aq.push(task);
console.log(aq.length());
}, 1000);
*/