-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.go
380 lines (324 loc) · 7.88 KB
/
spider.go
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package weint
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"time"
)
const (
TYPE_INFO = 1
TYPE_WEIBO = 1 << 1
)
type Type int64
const (
Int64 Type = iota
String
)
const WEIBO_URL = "https://m.weibo.cn/api/container/getIndex"
type WeiboResp struct {
Ok int `json:"ok"`
Data struct {
UserInfo *UserInfo `json:"userInfo"`
Cards []*CardInfo `json:"cards"`
TabsInfo struct {
Tabs []struct {
TabKey string `json:"tabKey"`
ContainerId string `json:"containerid"`
} `json:"tabs"`
} `json:"tabsInfo"`
CardListInfo struct {
SinceId uint64 `json:"since_id"`
} `json:"cardlistInfo"`
} `json:"data"`
}
type UserInfo struct {
Id int64 `json:"id"`
ScreenName string `json:"screen_name"`
ProfileImageUrl string `json:"profile_image_url"`
ProfileUrl string `json:"profile_url"`
StatusesCount int64 `json:"statuses_count"`
Verified bool `json:"verified"`
VerifiedType int `json:"verified_type"`
VerifiedTypeExt int `json:"verified_type_ext"`
VerifiedReason string `json:"verified_reason"`
Description string `json:"description"`
Gender string `json:"gender"`
FollowCount int64 `json:"follow_count"`
FollowersCount int64 `json:"followers_count"`
Mbtype int `json:"mbtype"`
Mbrank int64 `json:"mbrank"`
Urank int64 `json:"urank"`
}
type CardInfo struct {
CardType int `json:"card_type"`
Itemid string `json:"itemid"`
Scheme string `json:"scheme"`
Mblog *WeiboInfo `json:"mblog"`
}
type WeiboInfo struct {
User *UserInfo `json:"user" gorm:"-"`
UserId string `json:"-"`
ScreenName string `json:"-"`
Idstr string `json:"idstr"`
Mid string `json:"mid"`
Text string `json:"text"`
Source string `json:"source"`
OriginalPic string `json:"original_pic"`
MblogVipType int `json:"mblog_vip_type"`
CreatedAt string `json:"created_at"`
RepostsCount WeiboCount `json:"reposts_count" gorm:"-"`
RepostsCountStr string `json:"-"`
CommentsCount WeiboCount `json:"comments_count" gorm:"-"`
CommentsCountStr string `json:"-"`
AttitudesCount WeiboCount `json:"attitudes_count" gorm:"-"`
AttitudesCountStr string `json:"-"`
Pics []struct {
Pid string `json:"pid"`
Url string `json:"url"`
} `json:"pics" gorm:"-"`
PicsList string `json:"pics_list"`
}
type WeiboCount struct {
Type Type
IntVal int64
StrVal string
}
// @implement: json unmarshal
func (count *WeiboCount) UnmarshalJSON(value []byte) error {
if value[0] == '"' {
count.Type = String
return json.Unmarshal(value, &count.StrVal)
}
count.Type = Int64
return json.Unmarshal(value, &count.IntVal)
}
// 实现 json.Marshaller 接口
func (count *WeiboCount) MarshalJSON() ([]byte, error) {
switch count.Type {
case Int64:
return json.Marshal(count.IntVal)
case String:
return json.Marshal(count.StrVal)
default:
return []byte{}, fmt.Errorf("impossible Weibo.Type")
}
}
func (count *WeiboCount) String() string {
switch count.Type {
case Int64:
return strconv.FormatInt(count.IntVal, 10)
case String:
return count.StrVal
default:
return ""
}
}
func (w *WeiboInfo) Build() *WeiboInfo {
b, _ := json.Marshal(w.Pics)
w.UserId = strconv.FormatInt(w.User.Id, 10)
w.ScreenName = w.User.ScreenName
w.CommentsCountStr = w.CommentsCount.String()
w.RepostsCountStr = w.RepostsCount.String()
w.AttitudesCountStr = w.AttitudesCount.String()
w.PicsList = string(b)
return w
}
func (w *WeiboInfo) Slice() []string {
pb, _ := json.Marshal(w.Pics)
return []string{
w.Idstr,
w.CreatedAt,
w.Source,
w.Text,
string(pb),
w.CommentsCount.String(),
w.AttitudesCount.String(),
w.RepostsCount.String(),
strconv.FormatInt(w.User.Id, 10),
w.User.ScreenName,
}
}
func (u *UserInfo) Slice() []string {
return []string{
strconv.FormatInt(u.Id, 10),
u.ScreenName,
u.Description,
u.ProfileUrl,
u.Gender,
strconv.FormatInt(u.FollowCount, 10),
strconv.FormatInt(u.FollowersCount, 10),
strconv.FormatInt(u.StatusesCount, 10),
strconv.FormatBool(u.Verified),
u.VerifiedReason,
}
}
type Spider struct {
req *http.Request
client *http.Client
_type int
quick bool
uid string
limit int
container string
since uint64
profile *UserInfo
cards []*CardInfo
defaultOut OutInterface
userOut OutInterface
}
// @function: 初始化一个爬虫对象
func NewSpider() *Spider {
s := new(Spider)
s.client = new(http.Client)
s.req, _ = http.NewRequest("GET", WEIBO_URL, nil)
s.defaultOut = &ConsoleOut{}
s.userOut = nil
return s
}
// @function: 设置 client
func (s *Spider) Client(c *http.Client) *Spider {
s.client = c
return s
}
// @function: 设置微博用户的 id
func (s *Spider) Uid(uid string) *Spider {
s.uid = uid
return s
}
// @function: 设置获取微博数量
func (s *Spider) Limit(limit int) *Spider {
s.limit = limit
return s
}
// @function: 设置爬虫类型
func (s *Spider) Type(_type int) *Spider {
s._type += _type
return s
}
// @function: 设置爬虫速度
func (s *Spider) Quick(quick bool) *Spider {
s.quick = quick
return s
}
// @function: 设置输出类型
func (s *Spider) Out(out OutInterface) *Spider {
s.userOut = out
return s
}
// @function: 运行爬虫
func (s *Spider) Run() error {
if s.uid == "" {
return errors.New("weibo user id is not correct")
}
if s._type&TYPE_INFO > 0 {
if err := s.fetchUserInfo(); err != nil {
return err
}
}
if s._type&TYPE_WEIBO > 0 {
if err := s.fetchWeiboInfo(); err != nil {
return err
}
}
return nil
}
// @function: 获取用户信息
func (s *Spider) fetchUserInfo() error {
q := s.req.URL.Query()
q.Set("is_all", "1")
q.Set("type", "uid")
q.Set("value", s.uid)
s.req.URL.RawQuery = q.Encode()
if err := s.doRequest(); err != nil {
return err
}
return s.outProfile()
}
// @function: 获取微博信息
func (s *Spider) fetchWeiboInfo() error {
q := s.req.URL.Query()
q.Set("is_all", "1")
q.Set("type", "uid")
q.Set("value", s.uid)
q.Set("containerid", s.container)
if s.since != 0 {
q.Set("since_id", strconv.FormatUint(s.since, 10))
}
s.req.URL.RawQuery = q.Encode()
if err := s.doRequest(); err != nil {
return err
}
if err := s.outWeibo(); err != nil {
return err
}
if s.since != 0 {
return s.fetchWeiboInfo()
}
return nil
}
// @function: 执行请求获取微博响应数据
func (s *Spider) doRequest() error {
rs := rand.NewSource(time.Now().Unix())
ra := rand.New(rs)
if !s.quick {
time.Sleep(time.Duration(ra.Intn(5)+1) * time.Second)
}
s.req.Header.Set("User-Agent", uaList[ra.Intn(len(uaList))])
resp, err := s.client.Do(s.req)
if err != nil {
return err
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK || string(b) == "" {
return errors.New("weibo has no resp due to some reason such as request rate limit")
}
var data WeiboResp
if err := json.Unmarshal(b, &data); err != nil {
return err
}
s.profile = data.Data.UserInfo
s.cards = data.Data.Cards
s.since = data.Data.CardListInfo.SinceId
for _, t := range data.Data.TabsInfo.Tabs {
if t.TabKey == "weibo" {
s.container = t.ContainerId
}
}
return nil
}
func (s *Spider) outProfile() error {
if s.defaultOut != nil {
if err := s.defaultOut.WriteUserInfo(s.profile); err != nil {
return err
}
}
if s.userOut != nil {
if err := s.userOut.WriteUserInfo(s.profile); err != nil {
return err
}
}
return nil
}
func (s *Spider) outWeibo() error {
for _, c := range s.cards {
if c.CardType != 9 {
continue
}
if s.defaultOut != nil {
if err := s.defaultOut.WriteWeiboInfo(c.Mblog); err != nil {
return err
}
}
if s.userOut != nil {
if err := s.userOut.WriteWeiboInfo(c.Mblog); err != nil {
return err
}
}
}
return nil
}