-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.js
285 lines (229 loc) · 8.26 KB
/
main.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
281
282
283
284
285
/**
* Created by lenovo on 2014/9/14.
*/
var iconv = require('iconv-lite'); //字符集转换
var mongo = require('mongodb');
var monk = require('monk');
var Request = require('request');
var RsaEncrypt = require("./rsa").RSAKey;
var async = require('async');
var cheerio = require('cheerio');
var cookieColl = Request.jar()
var request = Request.defaults({jar: cookieColl});
var connection_string = '127.0.0.1:27017/weiboSina3';
var db = monk(connection_string);
var cachedUsers = {};
var userCnt = 0;
function saveUser(user){
var userColl = db.get("users");
userColl.insert(user);
}
function getJsonObj(body){
var start = body.indexOf("{");
var end = body.lastIndexOf("}");
var jsonStr = body.substr(start,end -start + 1);
var responseJson = JSON.parse(jsonStr);
return responseJson;
}
function getFansRecur(userId){
//新浪限制只能取每人前十页的fans
for(var i=1; i< 10; i++){
var fansUrl = "http://weibo.com/" + userId + "/follow?page=" + i;
request({
"uri": fansUrl,
"encoding": "utf-8"
}, function(err,response,body){
if(err){
console.log(err);
}
else{
var userLst = getUserLst(body,userId);
if (userLst){
userLst.map(function(item){
getFansRecur(item.uId);
});
}
}
});
}
}
function getUserLst(htmlContent,userId){
var matched = htmlContent.match(/\"cnfList\s*\\\".*\/ul>/gm);
if(matched) {
var str = matched[0].replace(/(\\n|\\t|\\r)/g," ").replace(/\\/g,"");
var ulStr = "<ul class=" + str;
var $ = cheerio.load(ulStr);
var myFans = [];
$("li").map(function (index, item) {
var userInfo = getUserInfo($,this);
if(userInfo){
if(!cachedUsers[userInfo.uId]){
userInfo.from = userId; //设置来源用户
cachedUsers[userInfo.uId] = true;
// if(userInfo.fansCnt > 100){
userCnt++;
console.log(userCnt);
saveUser(userInfo);
myFans.push(userInfo);
}
else{
console.log("duplicate users");
}
}
});
return myFans;
}
return null;
}
function getUserInfo($,liSelector){
var liActionData = $(liSelector).attr("action-data").split("&");
var sex = "unknown";
if(liActionData.length == 3){
sex = liActionData[2].split("=")[1];
}
var alnk = $(liSelector).find("a[usercard]");
var addr = $(liSelector).find("div.name span").text().trim();
var infoSel = $(liSelector).find("div.con_left div.info");
var personInfo = "";
if(infoSel.length > 0){
personInfo = infoSel.text();
}
var cntSel = $(liSelector).find("div.con_left div.connect a");
return {
name:alnk.text(),
uId:alnk.attr("usercard").split('=')[1],
followCnt:tryParseInt($(cntSel[0]).text()),
fansCnt:tryParseInt($(cntSel[1]).text()),
weiboCnt:tryParseInt($(cntSel[2]).text()),
addr: addr,
sex:sex,
info: personInfo
};
}
function tryParseInt(str){
try{
return parseInt(str);
}
catch(e){
console.log("parseInt failed.")
return 0;
}
}
function log(msg){
console.log(msg);
}
function start() {
var userName = "yourWeiboAccount";
var password = "pwd";
var preLoginUrl = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.11)&_=" + (new Date()).getTime();
async.waterfall([
function (callback) {
request({
"uri": preLoginUrl,
"encoding": "utf-8"
}, callback);
},
function (responseCode, body, callback) {
var responseJson = getJsonObj(body);
log(responseJson);
log("Prelogin Success. ");
var loginUrl = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)';
var loginPostData = {
entry: "weibo",
gateway: "1",
from: "",
savestate: "7",
useticket: "1",
vsnf: "1",
su: "",
service: "miniblog",
servertime: "",
nonce: "",
pwencode: "rsa2",
rsakv: "1330428213",
sp: "",
sr: "1366*768",
encoding: "UTF-8",
prelt: "282",
url: "http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack",
returntype: "META"
};
loginPostData.su = new Buffer(userName).toString('base64');
var rsaKey = new RsaEncrypt();
rsaKey.setPublic(responseJson.pubkey, '10001');
var pwd = rsaKey.encrypt([responseJson.servertime, responseJson.nonce].join("\t") + "\n" + password);
log([responseJson.servertime, responseJson.nonce].join("\t") + "\n" + password);
loginPostData.sp = pwd;
loginPostData.servertime = responseJson.servertime;
loginPostData.nonce = responseJson.nonce;
loginPostData.rsakv = responseJson.rsakv;
log("pk:" + responseJson.pubkey);
log("su:" + loginPostData.su);
log("pwd:" + loginPostData.sp);
request.post({
"uri": loginUrl,
"encoding": null, //GBK编码 需要额外收到处理,
form: loginPostData
}, callback);
},
function (responseCode, body, callback) {
body = iconv.decode(body,"GBK");
log(body)
var errReason = /reason=(.*?)\"/;
var errorLogin = body.match(errReason);
if (errorLogin) {
callback("登录失败,原因:" + errorLogin[1]);
}
else {
var urlReg = /location\.replace\(\'(.*?)\'\)./;
var urlLoginAgain = body.match(urlReg);
if (urlLoginAgain) {
request({
"uri": urlLoginAgain[1],
"encoding": "utf-8"
}, callback);
}
else {
callback("match failed");
}
}
},
function (responseCode, body, callback) {
console.log("登录完成");
var responseJson = getJsonObj(body);
console.log(responseJson);
var myfansUrl = "http://weibo.com/" + responseJson.userinfo.uniqueid + "/myfans"
request({
"uri": myfansUrl,
"encoding": "utf-8"
}, callback);
var fansUrl = "http://weibo.com/{userId}/fans";
},
function (responseCode, body, callback) {
console.log("开始分析... ");
var userColl = db.get("users");
var lastUid = "";
console.log("查询已经记录的用户");
var nIndex = 0;
userColl.find({},{stream:true}).each(function(doc){
cachedUsers[doc.uId] = true;
lastUid = doc.uId;
}).success(function(){
console.log("已有用户已经缓存完成, 开始进行递归查询");
console.log(lastUid);
getFansRecur("1708942053"); //周鸿祎
}).error(function(err){
console.log(err);
});
/*
var myFans = getUserLst(body);
console.log("Myfans:" + myFans.length);
myFans.map(function(item){
getFansRecur(item.uId);
}); */
}
], function (err) {
console.log(err)
});
}
start();