-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
443 lines (422 loc) · 13.1 KB
/
api.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package qzone
import (
"encoding/json"
"errors"
"fmt"
"github.com/HHU-47133/qzone/models"
"github.com/tidwall/gjson"
"log"
"math"
"math/rand/v2"
"regexp"
"strconv"
"strings"
"time"
)
var (
// cReLike 点赞响应正则,frameElement.callback();
cReLike = regexp.MustCompile(`(?s)frameElement.callback\((.*)\)`)
// cRe 正则,_Callback();
cRe = regexp.MustCompile(`(?s)_Callback\((.*)\)`)
)
// QQGroupList 群列表获取
func (m *QZone) QQGroupList() ([]*models.QQGroupResp, error) {
gr := &models.QQGroupReq{
Uin: m.qq,
Do: "1",
Rd: fmt.Sprintf("%010.8f", rand.Float64()),
Fupdate: "1",
Clean: "1",
GTk: m.gtk2,
}
url := getQQGroupURL + structToStr(gr)
data, err := DialRequest(NewRequest(WithUrl(url), WithHeader(map[string]string{
"user-agent": ua,
"cookie": m.cookie,
})))
if err != nil {
er := errors.New("QQ群请求错误:" + err.Error())
log.Println("QQ群获取失败:", er.Error())
return nil, er
}
r := cRe.FindStringSubmatch(string(data))
if len(r) < 2 {
er := errors.New("QQ群响应正则解析错误:" + string(data))
log.Println("QQ群获取失败:", er.Error())
return nil, er
}
jsonStr := r[1]
resLen := gjson.Get(jsonStr, "data.group.#").Int()
results := make([]*models.QQGroupResp, resLen)
index := 0
groups := gjson.Get(jsonStr, "data.group").Array()
for _, group := range groups {
gro := &models.QQGroupResp{
GroupCode: group.Get("groupcode").Int(),
GroupName: group.Get("groupname").String(),
TotalMember: group.Get("total_member").Int(),
NotFriends: group.Get("notfriends").Int(),
}
results[index] = gro
index++
}
return results, nil
}
// QQGroupMemberList 群友(非好友)列表获取
func (m *QZone) QQGroupMemberList(gid int64) ([]*models.QQGroupMemberResp, error) {
gmr := &models.QQGroupMemberReq{
Uin: m.qq,
Gid: gid,
Fupdate: "1",
Type: "1",
GTk: m.gtk2,
}
url := getQQGroupMemberURL + structToStr(gmr)
data, err := DialRequest(NewRequest(WithUrl(url), WithHeader(map[string]string{
"user-agent": ua,
"cookie": m.cookie,
})))
if err != nil {
er := errors.New("QQ群非好友请求错误:" + err.Error())
log.Println("QQ群非好友获取失败:", er.Error())
return nil, er
}
r := cRe.FindStringSubmatch(string(data))
if len(r) < 2 {
er := errors.New("QQ群非好友正则解析错误:" + string(data))
log.Println("QQ群非好友获取失败:", er.Error())
return nil, er
}
jsonStr := r[1]
resLen := gjson.Get(jsonStr, "data.notfriends").Int()
results := make([]*models.QQGroupMemberResp, resLen)
index := 0
groupMembers := gjson.Get(jsonStr, "data.friends").Array()
for _, groupMember := range groupMembers {
gro := &models.QQGroupMemberResp{
Uin: groupMember.Get("fuin").Int(),
NickName: groupMember.Get("name").String(),
AvatarURL: groupMember.Get("img").String(),
}
gro.GroupCode = gjson.Get(jsonStr, "data.groupcode").Int()
results[index] = gro
index++
}
return results, nil
}
// FriendList 好友列表获取 TODO:有时候显示亲密度前200好友
func (m *QZone) FriendList() ([]*models.FriendInfoEasyResp, error) {
url := fmt.Sprintf(friendURL, m.gtk2) + "&uin=" + strconv.FormatInt(m.qq, 10)
data, err := DialRequest(NewRequest(WithUrl(url), WithHeader(map[string]string{
"referer": userQzoneURL,
"origin": userQzoneURL,
"cookie": m.cookie,
})))
if err != nil {
er := errors.New("好友列表请求错误:" + err.Error())
log.Println("好友列表获取失败:", er.Error())
return nil, er
}
r := cRe.FindStringSubmatch(string(data))
if len(r) < 2 {
er := errors.New("好友列表正则解析错误:" + string(data))
log.Println("好友列表获取失败:", er.Error())
return nil, er
}
jsonStr := r[1]
resLen := gjson.Get(jsonStr, "items.#").Int()
results := make([]*models.FriendInfoEasyResp, resLen)
index := 0
friends := gjson.Get(jsonStr, "items").Array()
for _, friend := range friends {
fie := &models.FriendInfoEasyResp{
Uin: friend.Get("uin").Int(),
Groupid: friend.Get("groupid").Int(),
Name: friend.Get("name").String(),
Remark: friend.Get("remark").String(),
Image: friend.Get("image").String(),
Online: friend.Get("online").Int(),
}
results[index] = fie
index++
}
groupName := gjson.Get(jsonStr, "gpnames.#.gpname").Array()
for i := 0; i < index; i++ {
results[i].GroupName = groupName[results[i].Groupid].String()
}
return results, nil
}
// FriendInfoDetail 好友详细信息获取
func (m *QZone) FriendInfoDetail(uin int64) (*models.FriendInfoDetailResp, error) {
url := fmt.Sprintf(detailFriendURL, m.gtk2) + "&uin=" + strconv.FormatUint(uint64(uin), 10)
data, err := DialRequest(NewRequest(WithUrl(url), WithHeader(map[string]string{
"referer": userQzoneURL,
"origin": userQzoneURL,
"cookie": m.cookie,
})))
if err != nil {
er := errors.New("好友详细信息请求错误:" + err.Error())
log.Println("好友详细信息获取失败:", er.Error())
return nil, er
}
r := cRe.FindStringSubmatch(string(data))
if len(r) < 2 {
er := errors.New("好友详细信息正则解析错误:" + string(data))
log.Println("好友详细信息获取失败:", er.Error())
return nil, er
}
jsonStr := r[1]
fid := &models.FriendInfoDetailResp{}
if err := json.Unmarshal([]byte(jsonStr), fid); err != nil {
er := errors.New("好友详细信息JSON绑定错误:" + err.Error())
log.Println("好友详细信息获取失败:", er.Error())
return nil, er
}
return fid, nil
}
// PublishShuoShuo 发布说说,content文本内容,base64imgList图片数组
func (m *QZone) PublishShuoShuo(content string, base64imgList []string) (*models.ShuoShuoPublishResp, error) {
var (
uir *models.UploadImageResp
err error
picBo string
richval string
richvalList = make([]string, 0, 9)
picBoList = make([]string, 0, 9)
)
for _, base64img := range base64imgList {
uir, err = m.uploadImage(base64img)
if err != nil {
log.Println("说说发布失败:", err.Error())
return nil, err
}
picBo, richval, err = m.getPicBoAndRichval(uir)
if err != nil {
log.Println("说说发布失败:", err.Error())
return nil, err
}
richvalList = append(richvalList, richval)
picBoList = append(picBoList, picBo)
}
epr := models.EmotionPublishRequest{
SynTweetVerson: "1",
Paramstr: "1",
Who: "1",
Con: content,
Feedversion: "1",
Ver: "1",
UgcRight: "1",
ToSign: "0",
Hostuin: m.qq,
CodeVersion: "1",
Format: "json",
Qzreferrer: userQzoneURL + "/" + strconv.FormatInt(m.qq, 10),
}
if len(base64imgList) > 0 {
epr.Richtype = "1"
epr.Richval = strings.Join(richvalList, "\t")
epr.Subrichtype = "1"
epr.PicBo = strings.Join(picBoList, ",")
}
url := fmt.Sprintf(emotionPublishURL, m.gtk2)
payload := strings.NewReader(structToStr(epr))
data, err := DialRequest(NewRequest(WithMethod("POST"), WithUrl(url), WithBody(payload),
WithHeader(map[string]string{
"referer": userQzoneURL,
"origin": userQzoneURL,
"cookie": m.cookie,
})))
if err != nil {
er := errors.New("说说发布请求错误:" + err.Error())
log.Println("说说发布失败:", er.Error())
return nil, er
}
jsonStr := string(data)
ssp := &models.ShuoShuoPublishResp{
Code: gjson.Get(jsonStr, "code").Int(),
Tid: gjson.Get(jsonStr, "tid").String(),
Now: gjson.Get(jsonStr, "now").Int(),
Message: gjson.Get(jsonStr, "message").String(),
}
if ssp.Message != "" {
er := errors.New("说说发布错误:" + ssp.Message)
log.Println("说说发布失败:", er.Error())
return nil, er
}
return ssp, nil
}
// ShuoShuoList 获取所有说说 实际能访问的说说个数 <= 说说总数(空间仅展示近半年等情况) (有空间访问权限即可)
func (m *QZone) ShuoShuoList(uin int64, num int64, ms int64) (ShuoShuo []*models.ShuoShuoResp, err error) {
cnt := num
t := int(math.Ceil(float64(cnt) / 20.0))
var i int
//获取最大数量,控制i的取值
maxCnt, err := m.GetShuoShuoCount(uin)
if err != nil {
log.Println("说说获取失败:", err.Error())
return nil, err
}
for range t {
if i >= int(maxCnt) {
break
}
ShuoShuoTemp, err := m.shuoShuoListRaw(uin, 20, i, 0)
if err != nil {
log.Println("所有说说获取失败:", err.Error())
return nil, err
}
if len(ShuoShuoTemp) == 0 {
break
}
if len(ShuoShuo) < int(cnt) {
ShuoShuo = append(ShuoShuo, ShuoShuoTemp[0:min(len(ShuoShuoTemp), int(cnt)-len(ShuoShuo))]...)
i = i + 20
time.Sleep(time.Millisecond * time.Duration(ms))
}
}
return ShuoShuo, nil
}
// GetShuoShuoCount 获取用户QQ号为uin的说说总数(有空间访问权限即可)
func (m *QZone) GetShuoShuoCount(uin int64) (cnt int64, err error) {
mlr := models.MsgListRequest{
Uin: uin,
Ftype: "0",
Sort: "0",
Pos: "0",
Num: "1",
Replynum: "0",
GTk: m.gtk2,
Callback: "_preloadCallback",
CodeVersion: "1",
Format: "json",
NeedPrivateComment: "1",
}
url := msglistURL + "?" + structToStr(mlr)
data, err := DialRequest(NewRequest(WithUrl(url), WithHeader(map[string]string{
"referer": userQzoneURL,
"origin": userQzoneURL,
"cookie": m.cookie,
})))
if err != nil {
er := errors.New("说说总数请求错误:" + err.Error())
log.Println("说说总数获取失败:", er.Error())
return -1, er
}
jsonStr := string(data)
// 判断是否有访问权限
forbid := gjson.Get(jsonStr, "message").String()
if forbid != "" {
er := errors.New("说说总数响应错误:" + forbid)
log.Println("说说总数响应失败:", er.Error())
return -1, er
}
cnt = gjson.Get(jsonStr, "total").Int()
return cnt, nil
}
// GetLevel1CommentCount 获取一级评论总数(限制本人)
func (m *QZone) GetLevel1CommentCount(tid string) (cnt int64, err error) {
url := fmt.Sprintf(getCommentsURL, strconv.FormatInt(m.qq, 10), 0, 1, tid, m.gtk2)
data, err := DialRequest(NewRequest(WithUrl(url), WithHeader(map[string]string{
"cookie": m.cookie,
})))
if err != nil {
er := errors.New("说说评论请求错误:" + err.Error())
log.Println("说说评论请求失败:", er.Error())
return -1, er
}
r := cRe.FindStringSubmatch(string(data))
//log.Println("空指针异常测试:" + string(data))
if len(r) < 2 {
er := errors.New("说说评论正则解析错误:" + err.Error())
log.Println("说说评论请求失败:", er.Error())
return -1, er
}
jsonRaw := r[1]
// 说说的一级评论总数
numOfComments := gjson.Get(jsonRaw, "cmtnum").Int()
return numOfComments, nil
}
// ShuoShuoCommentList 根据说说ID获取评论(限制本人)
func (m *QZone) ShuoShuoCommentList(tid string, num int64, ms int64) (comments []*models.Comment, err error) {
numOfComments := num
t := int(math.Ceil(float64(numOfComments) / 20.0))
//获取最大数量,控制i的取值
maxCnt, err := m.GetLevel1CommentCount(tid)
if err != nil {
log.Println("说说评论获取失败:", err.Error())
return nil, err
}
var i int
for range t {
if i >= int(maxCnt) {
break
}
commentsTemp, err := m.shuoShuoCommentsRaw(20, i, tid)
if err != nil {
log.Println("说说评论获取失败:", err.Error())
return nil, err
}
if len(commentsTemp) == 0 {
break
}
if len(comments) < int(num) {
comments = append(comments, commentsTemp[0:min(len(commentsTemp), int(num)-len(comments))]...)
i = i + 20
time.Sleep(time.Millisecond * time.Duration(ms))
}
}
return comments, nil
}
// GetLatestShuoShuo 获取用户QQ号为uin的最新说说(有空间访问权限即可)
func (m *QZone) GetLatestShuoShuo(uin int64) (*models.ShuoShuoResp, error) {
ss, err := m.shuoShuoListRaw(uin, 1, 0, 0)
fmt.Println("ss!!!!!!:", ss)
if err != nil {
er := errors.New("最新说说获取错误:" + err.Error())
log.Println("最新说说获取失败:", er.Error())
return nil, er
}
return ss[0], nil
}
// DoLike 说说空间点赞 TODO:疑似无效
func (m *QZone) DoLike(tid string) (*models.LikeResp, error) {
lr := models.LikeRequest{
Qzreferrer: userQzoneURL + strconv.FormatInt(m.qq, 10),
Opuin: m.qq,
Unikey: userQzoneURL + strconv.FormatInt(m.qq, 10) + "/mood/" + tid,
From: "1",
Fid: tid,
Typeid: "0",
Appid: "311",
}
lr.Curkey = lr.Unikey
url := fmt.Sprintf(likeURL, m.gtk2)
payload := strings.NewReader(structToStr(lr))
data, err := DialRequest(NewRequest(WithMethod("POST"), WithUrl(url),
WithBody(payload), WithHeader(map[string]string{
"referer": userQzoneURL,
"origin": userQzoneURL,
"cookie": m.cookie,
})))
if err != nil {
er := errors.New("点赞请求错误:" + err.Error())
log.Println("空间点赞失败:", er.Error())
return nil, er
}
r := cReLike.FindStringSubmatch(string(data))
if len(r) < 2 {
er := errors.New("点赞响应解析错误:" + string(data))
log.Println("空间点赞失败:", er.Error())
return nil, er
}
likeResp := &models.LikeResp{
Ret: gjson.Get(r[1], "ret").Int(),
Msg: gjson.Get(r[1], "msg").String(),
}
if likeResp.Msg != "succ" {
er := errors.New("点赞未生效" + likeResp.Msg)
log.Println("空间点赞失败:", er.Error())
return nil, er
}
return likeResp, nil
}