-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
271 lines (228 loc) · 6.79 KB
/
app.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
#!/usr/bin/env node
'use strict';
const Fs = require('fs');
const _ = require('lodash');
const Cheerio = require('cheerio');
const Async = require('async');
const Request = require('request-promise');
const CookieKit = require('tough-cookie-kit');
const Moment = require('moment');
const Bunyan = require('bunyan');
const Inquirer = require('inquirer');
const Chalk = require('chalk');
const Log = Bunyan.createLogger({
name: 'weibo-spider',
src: true
});
// Log.trace, Log.debug, Log.info, Log.warn, Log.error, and Log.fatal
let gCookies = Request.jar(new CookieKit('cookies.json'));
const gRequest = Request.defaults({
// 'proxy': 'http://8.8.8.8:8888',
'gzip': true,
'simple': false, // Get a rejection only if the request failed for technical reasons
'resolveWithFullResponse': true, // Get the full response instead of just the body
'followRedirect': false,
'jar': gCookies
});
let gHeaders = {
'Host': 'passport.weibo.cn',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch, br',
'Accept-Language': 'zh-CN,zh;q=0.8'
};
const USER_NAME = '';
const PASS_WORD = '';
main();
function main() {
return procMain();
}
function procMain() {
return Async.waterfall([
function(done) {
return getLogin().then(function(res) {
return done(null, res);
});
},
function(data, done) {
return postLogin(USER_NAME, PASS_WORD).then(function(res) {
return done(null, res);
});
},
function(data, done) {
return getMain().then(function(res) {
return done(null, res);
});
},
function(data, done) {
return getCommonGroup().then(function(res) {
return done(null, res);
});
},
function(data, done) {
return getFeed().then(function(res) {
return done(null, res);
});
},
function(data, done) {
return getMBlog().then(function(res) { // 从这里返回的html里面获取发微博需要的st参数
return done(null, getStCode(res.body));
});
},
function(data, done) {
return postAMBlog('send by node.js', data).then(function(res) {
return done(null, res);
});
}
], function (err, res) {
return err ? console.log(Chalk.red(err)) : showBody(res);
});
}
function showBody(data) {
console.log(data.body);
}
function getStCode(strHtml) {
let regexSt = /\"st\":\"(\w+?)\",/i;
let arrSt = strHtml.match(regexSt);
return ((arrSt.length !== 2) ? null : arrSt[1]);
}
function getLogin() {
let headers = _.assign({}, gHeaders, {
'Upgrade-Insecure-Requests': 1,
'Referer': 'https://passport.weibo.cn/signin/welcome'
});
return getHtml('https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F', headers);
}
function postLogin(username, password) {
let headers = _.assign({}, gHeaders, {
'Origin': 'https://passport.weibo.cn',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*',
'Referer': 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F'
});
let form = {
'username': username,
'password': password,
'savestate': 1,
'ec': 0,
'pagerefer': 'https://passport.weibo.cn/signin/welcome',
'entry': 'mweibo',
'wentry': '',
'loginfrom': '',
'client_id': '',
'code': '',
'qq': '',
'hff': '',
'hfp': ''
};
return postForm(`https://passport.weibo.cn/sso/login`, headers, form);
}
function getMain() {
let headers = _.assign({}, gHeaders, {
'Upgrade-Insecure-Requests': 1,
'Referer': 'https://passport.weibo.cn/signin/welcome',
'Host': 'm.weibo.cn'
});
return getHtml('http://m.weibo.cn/', headers);
}
function getCommonGroup() {
let headers = _.assign({}, gHeaders, {
'Referer': 'http://m.weibo.cn/',
'Host': 'm.weibo.cn',
'X-Requested-With': 'XMLHttpRequest'
});
return getJson('http://m.weibo.cn/index/getCommonGroup', headers);
}
function getFeed() {
let headers = _.assign({}, gHeaders, {
'Referer': 'http://m.weibo.cn/',
'Host': 'm.weibo.cn',
'X-Requested-With': 'XMLHttpRequest'
});
let data = {
'format': 'cards'
};
return getJson('http://m.weibo.cn/index/feed', headers, data);
}
function getMBlog() {
let headers = _.assign({}, gHeaders, {
'Referer': 'http://m.weibo.cn/',
'Host': 'm.weibo.cn',
'Upgrade-Insecure-Requests': 1
});
return getHtml('http://m.weibo.cn/mblog', headers);
}
function postAMBlog(content, st) {
let headers = _.assign({}, gHeaders, {
'Host': 'm.weibo.cn',
'Origin': 'http://m.weibo.cn',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Referer': 'http://m.weibo.cn/mblog',
'X-Requested-With': 'XMLHttpRequest'
});
let form = {
'content': content,
'annotations': '',
'st': st
};
return postForm(`http://m.weibo.cn/mblogDeal/addAMblog`, headers, form);
}
function getHtml(url, headers, data) {
let options = {
'url': url,
'headers': headers,
'qs': data
};
return get(options);
}
function getJson(url, headers, data) {
let options = {
'url': url,
'headers': headers,
'qs': data,
'json': true
};
return get(options);
}
function postJson(url, headers, json) {
let options = {
'url': url,
'headers': headers,
'form': json,
'json': true
};
return post(options);
}
function postForm(url, headers, form) {
let options = {
'url': url,
'headers': headers,
'form': form
};
return post(options);
}
function get(options) {
return reqHttp(_.assign({}, options, {
'method': 'GET'
}));
}
function post(options) {
return reqHttp(_.assign({}, options, {
'method': 'POST'
}));
}
function reqHttp(options) {
return gRequest(options)
.then(procReqSucceeded)
.catch(procReqFailed);
}
function procReqSucceeded(response) {
return response;
}
function procReqFailed(error) {
return Log.error(error);
}