-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
689 lines (638 loc) · 18.8 KB
/
client.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// Package pica 哔咔漫画Golang客户端
package pica
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
var json jsoniter.API
func init() {
extra.RegisterFuzzyDecoders()
json = jsoniter.ConfigCompatibleWithStandardLibrary
}
// 哔咔的网址
const server = "https://picaapi.picacomic.com/"
// Client
// 客户端 struct
type Client struct {
http.Client
Token string
}
// 设置签名header
func (client *Client) header(req *http.Request) {
// define
var apiKey = "C69BAF41DA5ABD1FFEDC6D2FEA56B"
var nonce = "b1ab87b4800d4d4590a11701b8551afa"
// header
var header = map[string]string{
"api-key": apiKey,
"accept": "application/vnd.picacomic.com.v1+json",
"app-channel": "2",
"time": strconv.FormatInt(time.Now().Unix(), 10),
"nonce": nonce,
"signature": "",
"app-version": "2.2.1.2.3.3",
"app-uuid": "defaultUuid",
"app-platform": "android",
"app-build-version": "44",
"Content-Type": "application/json; charset=UTF-8",
"User-Agent": "okhttp/3.8.1",
"authorization": client.Token,
"image-quality": "original",
}
// sign
var raw = strings.TrimPrefix(req.URL.RequestURI(), "/") + header["time"] + nonce + req.Method + apiKey
raw = strings.ToLower(raw)
h := hmac.New(sha256.New, []byte("~d}$Q7$eIni=V)9\\RK/P.RM4;9[7|@/CA}b~OW!3?EV`:<>M7pddUBL5n|0/*Cn"))
h.Write([]byte(raw))
header["signature"] = hex.EncodeToString(h.Sum(nil))
// put in req
if req != nil {
for k, v := range header {
req.Header.Set(k, v)
}
}
}
// postToPica 向哔咔发送请求
func (client *Client) bodyRequestToPica(method string, path string, body interface{}) ([]byte, error) {
var req *http.Request
var err error
if body == nil {
req, err = http.NewRequest(method, server+path, nil)
} else {
bodyBuff, err := json.Marshal(body)
if err != nil {
return nil, err
}
bodyStream := bytes.NewBuffer(bodyBuff)
req, err = http.NewRequest(method, server+path, bodyStream)
}
if err != nil {
return nil, err
}
client.header(req)
return client.responseFromPica(req)
}
// postToPica 向哔咔发送POST请求
func (client *Client) postToPica(path string, body interface{}) ([]byte, error) {
return client.bodyRequestToPica("POST", path, body)
}
// putToPica 向哔咔发送PUT请求
func (client *Client) putToPica(path string, body interface{}) ([]byte, error) {
return client.bodyRequestToPica("PUT", path, body)
}
// getToPica 向哔咔发送GET请求
func (client *Client) getToPica(path string) ([]byte, error) {
req, err := http.NewRequest("GET", server+path, nil)
if err != nil {
return nil, err
}
client.header(req)
return client.responseFromPica(req)
}
// getToPicaWithQuality 向哔咔发送GET请求, 并修改 "image-quality" 请求头
func (client *Client) getToPicaWithQuality(path string, quality string) ([]byte, error) {
req, err := http.NewRequest("GET", server+path, nil)
if err != nil {
return nil, err
}
client.header(req)
req.Header.Set("image-quality", quality)
return client.responseFromPica(req)
}
// responseFromPica 从哔咔接口返回体, 并解析异常信息
func (client *Client) responseFromPica(req *http.Request) ([]byte, error) {
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buff, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response Response
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
if response.Code != 200 {
return nil, errors.New(response.Message)
}
return buff, nil
}
// Register 注册新用户
func (client *Client) Register(dto RegisterDto) error {
_, err := client.postToPica("auth/register", &dto)
return err
}
// Login 登录, 登录无异常则注入TOKEN
func (client *Client) Login(username string, password string) error {
buff, err := client.postToPica("auth/sign-in", &LoginRequest{
Email: username,
Password: password,
})
if err != nil {
return err
}
var loginResponse LoginResponse
err = json.Unmarshal(buff, &loginResponse)
if err != nil {
return err
}
client.Token = loginResponse.Data.Token
return nil
}
// UserProfile 用户信息
func (client *Client) UserProfile() (*UserProfile, error) {
buff, err := client.getToPica("users/profile")
if err != nil {
return nil, err
}
var userProfileResponse UserProfileResponse
err = json.Unmarshal(buff, &userProfileResponse)
if err != nil {
return nil, err
}
return &userProfileResponse.Data.User, nil
}
// PunchIn 打哔卡
func (client *Client) PunchIn() (*PunchStatus, error) {
buff, err := client.postToPica("users/punch-in", nil)
if err != nil {
return nil, err
}
var response PunchResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return &response.Data.Res, nil
}
// Categories 获取分类
func (client *Client) Categories() ([]Category, error) {
buff, err := client.getToPica("categories")
if err != nil {
return nil, err
}
var response CategoriesResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return response.Data.Categories, nil
}
// Comics 分类下的漫画
// category 为空字符串则为所有分类
func (client *Client) Comics(category string, tag string, creatorId string, chineseTeam string, sort string, page int) (*ComicsPage, error) {
mUrl := "comics?"
if len(category) > 0 {
mUrl = mUrl + fmt.Sprintf("c=%s&", url.QueryEscape(category))
}
if len(tag) > 0 {
mUrl = mUrl + fmt.Sprintf("t=%s&", url.QueryEscape(tag))
}
if len(creatorId) > 0 {
mUrl = mUrl + fmt.Sprintf("ca=%s&", creatorId)
}
if len(chineseTeam) > 0 {
mUrl = mUrl + fmt.Sprintf("ct=%s&", url.QueryEscape(chineseTeam))
}
buff, err := client.getToPica(mUrl + "s=" + sort + "&page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
var comicsResponse ComicsPageResponse
err = json.Unmarshal(buff, &comicsResponse)
if err != nil {
return nil, err
}
return &comicsResponse.Data.Comics, nil
}
// SearchComics 搜索漫画
// PS : 此接口并没有返回 PagesCount EpsCount
func (client *Client) SearchComics(categories []string, keyword string, sort string, page int) (*ComicsPage, error) {
params := map[string]interface{}{
"keyword": keyword,
"sort": sort,
}
if categories != nil && len(categories) > 0 {
params["categories"] = categories
}
buff, err := client.postToPica("comics/advanced-search?page="+strconv.Itoa(page), params)
if err != nil {
return nil, err
}
var comicsResponse ComicsPageResponse
err = json.Unmarshal(buff, &comicsResponse)
if err != nil {
return nil, err
}
return &comicsResponse.Data.Comics, nil
}
// SearchComicsInCategories 搜索漫画
func (client *Client) SearchComicsInCategories(keyword string, sort string, page int, categories []string) (*ComicsPage, error) {
params := map[string]interface{}{}
params["categories"] = categories
params["keyword"] = keyword
params["sort"] = sort
buff, err := client.postToPica("comics/advanced-search?page="+strconv.Itoa(page), params)
if err != nil {
return nil, err
}
var comicsResponse ComicsPageResponse
err = json.Unmarshal(buff, &comicsResponse)
if err != nil {
return nil, err
}
return &comicsResponse.Data.Comics, nil
}
// RandomComics 随机漫画
func (client *Client) RandomComics() ([]ComicSimple, error) {
buff, err := client.getToPica("comics/random")
if err != nil {
return nil, err
}
var comicsResponse ComicsResponse
err = json.Unmarshal(buff, &comicsResponse)
if err != nil {
return nil, err
}
return comicsResponse.Data.Comics, nil
}
// Leaderboard 排行榜
func (client *Client) Leaderboard(leaderboardType string) ([]ComicSimple, error) {
buff, err := client.getToPica(fmt.Sprintf("comics/leaderboard?tt=%s&ct=VC", leaderboardType))
if err != nil {
return nil, err
}
var comicsResponse ComicsResponse
err = json.Unmarshal(buff, &comicsResponse)
if err != nil {
return nil, err
}
return comicsResponse.Data.Comics, nil
}
// ComicInfo 漫画详情
func (client *Client) ComicInfo(comicId string) (*ComicInfo, error) {
buff, err := client.getToPica("comics/" + comicId)
if err != nil {
return nil, err
}
var comicInfoResponse ComicInfoResponse
err = json.Unmarshal(buff, &comicInfoResponse)
if err != nil {
return nil, err
}
return &comicInfoResponse.Data.Comic, nil
}
// ComicEpPage 漫画EP信息
func (client *Client) ComicEpPage(comicId string, page int) (*EpPage, error) {
buff, err := client.getToPica("comics/" + comicId + "/eps?page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
var epPageResponse EpPageResponse
err = json.Unmarshal(buff, &epPageResponse)
if err != nil {
return nil, err
}
return &epPageResponse.Data.Eps, nil
}
// ComicPicturePage 漫画图片
func (client *Client) ComicPicturePage(comicId string, epOrder int, page int) (*ComicPicturePage, error) {
buff, err := client.getToPica("comics/" + comicId + "/order/" + strconv.Itoa(epOrder) + "/pages?page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
var epPageResponse ComicPicturePageResponse
err = json.Unmarshal(buff, &epPageResponse)
if err != nil {
return nil, err
}
return &epPageResponse.Data.Pages, nil
}
// ComicPicturePageWithQuality 漫画图片
func (client *Client) ComicPicturePageWithQuality(comicId string, epOrder int, page int, quality string) (*ComicPicturePage, error) {
buff, err := client.getToPicaWithQuality("comics/"+comicId+"/order/"+strconv.Itoa(epOrder)+"/pages?page="+strconv.Itoa(page), quality)
if err != nil {
return nil, err
}
var epPageResponse ComicPicturePageResponse
err = json.Unmarshal(buff, &epPageResponse)
if err != nil {
return nil, err
}
return &epPageResponse.Data.Pages, nil
}
// SwitchLike (取消)喜欢漫画
// 第一次喜欢,第二次是取消喜欢 action是最终结果
func (client *Client) SwitchLike(comicId string) (*string, error) {
buff, err := client.postToPica("comics/"+comicId+"/like", nil)
if err != nil {
return nil, err
}
var actionResponse ActionResponse
err = json.Unmarshal(buff, &actionResponse)
if err != nil {
return nil, err
}
return &actionResponse.Data.Action, nil
}
// SwitchFavourite (取消)收藏漫画
// 第一次收藏,第二次是取消收藏 action是最终结果
func (client *Client) SwitchFavourite(comicId string) (*string, error) {
buff, err := client.postToPica("comics/"+comicId+"/favourite", nil)
if err != nil {
return nil, err
}
var actionResponse ActionResponse
err = json.Unmarshal(buff, &actionResponse)
if err != nil {
return nil, err
}
return &actionResponse.Data.Action, nil
}
// FavouriteComics 收藏的漫画
func (client *Client) FavouriteComics(sort string, page int) (*ComicsPage, error) {
buff, err := client.getToPica("users/favourite?s=" + sort + "&page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
var comicsResponse ComicsPageResponse
err = json.Unmarshal(buff, &comicsResponse)
if err != nil {
return nil, err
}
return &comicsResponse.Data.Comics, nil
}
// ComicCommentsPage 漫画的评论
func (client *Client) ComicCommentsPage(comicId string, page int) (*CommentsPage, error) {
buff, err := client.getToPica("comics/" + comicId + "/comments?page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
var commentsResponse CommentsResponse
err = json.Unmarshal(buff, &commentsResponse)
if err != nil {
return nil, err
}
return &commentsResponse.Data.Comments, nil
}
func (client *Client) MyComments(page int) (*MyCommentsPage, error) {
buff, err := client.getToPica(fmt.Sprintf("users/my-comments?page=%d", page))
if err != nil {
return nil, err
}
var response MyCommentsPageResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return &response.Data.Comments, nil
}
// PostComment 对漫画进行评论, 但是评论后无法删除
func (client *Client) PostComment(comicId string, content string) error {
_, err := client.postToPica(fmt.Sprintf("comics/%s/comments", comicId), map[string]string{
"content": content,
})
return err
}
// HideComment 哔咔API里的接口, 不知道做什么用的, 推测是管理员用接口
func (client *Client) HideComment(commentId string) error {
_, err := client.postToPica(fmt.Sprintf("comments/%s/delete", commentId), nil)
return err
}
// CommentChildren 获取子评论
func (client *Client) CommentChildren(commentId string, page int) (*CommentChildrenPage, error) {
buff, err := client.getToPica(fmt.Sprintf("comments/%s/childrens?page=%d", commentId, page))
if err != nil {
return nil, err
}
var response CommentChildrenResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return &response.Data.Comments, nil
}
// PostChildComment 对漫画/游戏的评论进行回复(子评论), 但是评论后无法删除
func (client *Client) PostChildComment(commentId string, content string) error {
_, err := client.postToPica(fmt.Sprintf("comments/%s", commentId), map[string]string{
"content": content,
})
return err
}
// SwitchLikeComment (取消)喜欢评论/子评论
// 第一次喜欢,第二次是取消喜欢 action是最终结果 ( ActionLike or ActionUnlike )
func (client *Client) SwitchLikeComment(commentId string) (*string, error) {
buff, err := client.postToPica("comments/"+commentId+"/like", nil)
if err != nil {
return nil, err
}
var actionResponse ActionResponse
err = json.Unmarshal(buff, &actionResponse)
if err != nil {
return nil, err
}
return &actionResponse.Data.Action, nil
}
// ComicRecommendation 看了这个本子的也在看
func (client *Client) ComicRecommendation(comicId string) ([]ComicSimple, error) {
buff, err := client.getToPica("comics/" + comicId + "/recommendation")
if err != nil {
return nil, err
}
var recommendationResponse ComicsResponse
err = json.Unmarshal(buff, &recommendationResponse)
if err != nil {
return nil, err
}
return recommendationResponse.Data.Comics, nil
}
// HotKeywords 大家都在搜
func (client *Client) HotKeywords() ([]string, error) {
buff, err := client.getToPica("keywords")
if err != nil {
return nil, err
}
var hotKeywordsResponse HotKeywordsResponse
err = json.Unmarshal(buff, &hotKeywordsResponse)
if err != nil {
return nil, err
}
return hotKeywordsResponse.Data.Keywords, nil
}
// LeaderboardOfKnight 骑士榜
func (client *Client) LeaderboardOfKnight() ([]Knight, error) {
buff, err := client.getToPica("comics/knight-leaderboard")
if err != nil {
return nil, err
}
var response LeaderboardOfKnightResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return response.Data.Users, nil
}
// GamePage 游戏列表
func (client *Client) GamePage(page int) (*GamePage, error) {
buff, err := client.getToPica("games?page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
var response GamePageResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return &response.Data.Games, nil
}
// GameInfo 游戏详情
func (client *Client) GameInfo(gameId string) (*GameInfo, error) {
buff, err := client.getToPica("games/" + gameId)
if err != nil {
return nil, err
}
var response GameResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return &response.Data.Game, nil
}
// GameCommentsPage 游戏评论分页
func (client *Client) GameCommentsPage(gameId string, page int) (*GameCommentsPage, error) {
buff, err := client.getToPica("games/" + gameId + "/comments?page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
var commentsResponse GameCommentsResponse
err = json.Unmarshal(buff, &commentsResponse)
if err != nil {
return nil, err
}
return &commentsResponse.Data.Comments, nil
}
// PostGameComment 对游戏进行评论, 但是评论后无法删除
func (client *Client) PostGameComment(gameId string, content string) error {
_, err := client.postToPica(fmt.Sprintf("games/%s/comments", gameId), map[string]string{
"content": content,
})
return err
}
// GameCommentChildren 游戏评论的回复分页 (和漫画接口是同一个, 只有"_comic/_game"字段不一样)
func (client *Client) GameCommentChildren(commentId string, page int) (*GameCommentChildrenPage, error) {
buff, err := client.getToPica(fmt.Sprintf("comments/%s/childrens?page=%d", commentId, page))
if err != nil {
return nil, err
}
var response GameCommentChildrenResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return &response.Data.Comments, nil
}
// UpdatePassword 修改密码
func (client *Client) UpdatePassword(oldPassword string, newPassword string) error {
body := map[string]string{
"old_password": oldPassword,
"new_password": newPassword,
}
_, err := client.putToPica("users/password", body)
return err
}
// UpdateSlogan 修改签名
func (client *Client) UpdateSlogan(slogan string) error {
body := map[string]string{
"slogan": slogan,
}
_, err := client.putToPica("users/profile", body)
return err
}
// UpdateAvatar 修改头像
// 请压缩头像成正方形, 200x200,并尽量减少图片体积, 编码必须为JPEG
func (client *Client) UpdateAvatar(jpegBytes []byte) error {
body := map[string]string{
"avatar": "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(jpegBytes),
}
_, err := client.putToPica("users/avatar", body)
return err
}
func (client *Client) Collections() ([]Collection, error) {
buff, err := client.getToPica("collections")
if err != nil {
return nil, err
}
var response CollectionsResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return response.Data.Collections, nil
}
// ForgotPassword 找回密码, 获取用户问题
func (client *Client) ForgotPassword(email string) (*ForgotPasswordResult, error) {
buff, err := client.postToPica("auth/forgot-password", map[string]string{
"email": email,
})
if err != nil {
return nil, err
}
var response ForgotPasswordResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return &response.Data, nil
}
// ResetPassword 找回密码, 根据问题重置密码
func (client *Client) ResetPassword(email string, questionNo int, answer string) (*ResetPasswordResult, error) {
buff, err := client.postToPica("auth/reset-password", map[string]interface{}{
"email": email,
"questionNo": questionNo,
"answer": answer,
})
if err != nil {
return nil, err
}
var response ResetPasswordResponse
err = json.Unmarshal(buff, &response)
if err != nil {
return nil, err
}
return &response.Data, nil
}
func (client *Client) InitInfo() (*InitInfo, error) {
req, err := http.NewRequest("GET", "http://68.183.234.72/init", nil)
if err != nil {
return nil, err
}
client.header(req)
rsp, err := client.Do(req)
defer rsp.Body.Close()
buff, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
var initInfo InitInfo
err = json.Unmarshal(buff, &initInfo)
if err != nil {
return nil, err
}
return &initInfo, nil
}