-
Notifications
You must be signed in to change notification settings - Fork 21
/
bili_client.go
2740 lines (2589 loc) · 61.5 KB
/
bili_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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package biligo
import (
"encoding/json"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/iyear/biligo/internal/util"
"github.com/iyear/biligo/proto/dm"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"io"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"time"
)
type BiliClient struct {
Me *Account
auth *CookieAuth
*baseClient
}
type CookieAuth struct {
DedeUserID string // DedeUserID
DedeUserIDCkMd5 string // DedeUserID__ckMd5
SESSDATA string // SESSDATA
BiliJCT string // bili_jct
}
type BiliSetting struct {
// Cookie
Auth *CookieAuth
// 自定义http client
//
// 默认为 http.http.DefaultClient
Client *http.Client
// Debug模式 true将输出请求信息 false不输出
//
// 默认false
DebugMode bool
// 自定义UserAgent
//
// 默认Chrome随机Agent
UserAgent string
}
// NewBiliClient
//
// 带有账户Cookie的Client,用于访问私人操作API
func NewBiliClient(setting *BiliSetting) (*BiliClient, error) {
if setting.Auth == nil {
return nil, errors.New("auth cannot be nil")
}
bili := &BiliClient{
auth: setting.Auth,
baseClient: newBaseClient(&baseSetting{
Client: setting.Client,
DebugMode: setting.DebugMode,
UserAgent: setting.UserAgent,
Prefix: "BiliClient ",
}),
}
account, err := bili.GetMe()
if err != nil {
return nil, err
}
bili.Me = account
return bili, nil
}
// GetMe
//
// 获取个人基本信息
func (b *BiliClient) GetMe() (*Account, error) {
resp, err := b.RawParse(BiliApiURL,
"x/member/web/account",
"GET",
nil,
)
if err != nil {
return nil, err
}
var account *Account
if err = json.Unmarshal(resp.Data, &account); err != nil {
return nil, err
}
return account, nil
}
// SetClient
//
// 设置Client,可以用来更换代理等操作
func (b *BiliClient) SetClient(client *http.Client) {
b.client = client
}
// SetUA
//
// 设置UA
func (b *BiliClient) SetUA(ua string) {
b.ua = ua
}
// Raw
//
// base末尾带/
func (b *BiliClient) Raw(base, endpoint, method string, payload map[string]string) ([]byte, error) {
raw, err := b.raw(base, endpoint, method, payload,
func(d *url.Values) {
switch method {
case "POST":
d.Add("csrf", b.auth.BiliJCT)
}
},
func(r *http.Request) {
r.Header.Add("Cookie", fmt.Sprintf("DedeUserID=%s;SESSDATA=%s;DedeUserID__ckMd5=%s",
b.auth.DedeUserID, b.auth.SESSDATA, b.auth.DedeUserIDCkMd5))
})
if err != nil {
return nil, err
}
return raw, nil
}
// RawParse
//
// base末尾带/
func (b *BiliClient) RawParse(base, endpoint, method string, payload map[string]string) (*Response, error) {
raw, err := b.Raw(base, endpoint, method, payload)
if err != nil {
return nil, err
}
return b.parse(raw)
}
// Upload 上传文件
//
// base末尾带/
func (b *BiliClient) Upload(base, endpoint string, payload map[string]string, files []*FileUpload) ([]byte, error) {
raw, err := b.upload(base, endpoint, payload, files, func(m *multipart.Writer) error {
return m.WriteField("csrf", b.auth.BiliJCT)
}, func(r *http.Request) {
r.Header.Add("Cookie", fmt.Sprintf("DedeUserID=%s;SESSDATA=%s;DedeUserID__ckMd5=%s",
b.auth.DedeUserID, b.auth.SESSDATA, b.auth.DedeUserIDCkMd5))
})
if err != nil {
return nil, err
}
return raw, nil
}
// UploadParse 上传文件
//
// base末尾带/
func (b *BiliClient) UploadParse(base, endpoint string, payload map[string]string, files []*FileUpload) (*Response, error) {
raw, err := b.Upload(base, endpoint, payload, files)
if err != nil {
return nil, err
}
return b.parse(raw)
}
// GetCookieAuth
//
// 获取Cookie信息
func (b *BiliClient) GetCookieAuth() *CookieAuth {
return b.auth
}
// GetNavInfo
//
// 获取我的导航栏信息(大部分的用户信息都在这里了)
func (b *BiliClient) GetNavInfo() (*NavInfo, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/web-interface/nav",
"GET",
nil,
)
if err != nil {
return nil, err
}
var info *NavInfo
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
return info, nil
}
// GetNavStat
//
// 获取我的状态数
func (b *BiliClient) GetNavStat() (*NavStat, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/web-interface/nav/stat",
"GET",
nil,
)
if err != nil {
return nil, err
}
var stat *NavStat
if err = json.Unmarshal(resp.Data, &stat); err != nil {
return nil, err
}
return stat, nil
}
// GetExpRewardStat
//
// 获取每日经验任务和认证相关任务的完成情况
func (b *BiliClient) GetExpRewardStat() (*ExpRewardStat, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/member/web/exp/reward",
"GET",
nil,
)
if err != nil {
return nil, err
}
var stat *ExpRewardStat
if err = json.Unmarshal(resp.Data, &stat); err != nil {
return nil, err
}
return stat, nil
}
// GetExpCoinReward
//
// 获取每日投币获得经验数,不存在延迟 上限50经验
func (b *BiliClient) GetExpCoinReward() (int, error) {
resp, err := b.Raw(
BiliMainURL,
"plus/account/exp.php",
"GET",
nil,
)
if err != nil {
return -1, err
}
var tResp struct {
Response
Number int `json:"number"`
}
if err = json.Unmarshal(resp, &tResp); err != nil {
return -1, err
}
if tResp.Code != 0 {
return -1, fmt.Errorf("(%d) %s", tResp.Code, tResp.Message)
}
return tResp.Number, nil
}
// GetVipStat
//
// 获取大会员信息
func (b *BiliClient) GetVipStat() (*VipStat, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/vip/web/user/info",
"GET",
nil,
)
if err != nil {
return nil, err
}
var info *VipStat
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
return info, nil
}
// GetAccountSafetyStat
//
// 获取账户安全情况
func (b *BiliClient) GetAccountSafetyStat() (*AccountSafetyStat, error) {
resp, err := b.RawParse(
BiliPassportURL,
"web/site/user/info",
"GET",
nil,
)
if err != nil {
return nil, err
}
var info *AccountSafetyStat
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
return info, nil
}
// GetRealNameStat
//
// 获取账户实名认证状态
func (b *BiliClient) GetRealNameStat() (bool, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/member/realname/status",
"GET",
nil,
)
if err != nil {
return false, err
}
var flag struct {
Status int `json:"status,omitempty"`
}
if err = json.Unmarshal(resp.Data, &flag); err != nil {
return false, err
}
return flag.Status == 1, nil
}
// GetRealNameInfo
//
// 获取账户实名认证详细信息
func (b *BiliClient) GetRealNameInfo() (*RealNameInfo, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/member/realname/apply/status",
"GET",
nil,
)
if err != nil {
return nil, err
}
var info *RealNameInfo
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
return info, nil
}
// FollowingsGetMy 获取我的关注列表
//
// mid数组
func (b *BiliClient) FollowingsGetMy() ([]int64, error) {
resp, err := b.RawParse(
BiliVcURL,
"feed/v1/feed/get_attention_list",
"GET",
map[string]string{},
)
if err != nil {
return []int64{}, err
}
var mids struct {
List []int64 `json:"list"`
}
if err = json.Unmarshal(resp.Data, &mids); err != nil {
return []int64{}, err
}
return mids.List, nil
}
// FollowingsGetMyDetail 获取我的详细的关注列表,获取他人的请使用 CommClient
//
// pn 页码
//
// ps 每页项数,最大50
//
// order 1:最常访问 2:最近关注
func (b *BiliClient) FollowingsGetMyDetail(pn int, ps int, order int) (*FollowingsDetail, error) {
var o = map[int]string{
1: "attention",
2: "",
}
resp, err := b.RawParse(
BiliApiURL,
"x/relation/followings",
"GET",
map[string]string{
"vmid": strconv.FormatInt(b.Me.MID, 10),
"pn": strconv.Itoa(pn),
"ps": strconv.Itoa(ps),
"order_type": o[order],
},
)
if err != nil {
return nil, err
}
var detail = &FollowingsDetail{}
if err = json.Unmarshal(resp.Data, &detail); err != nil {
return nil, err
}
return detail, nil
}
// GetCoinLogs
//
// 获取最近一周的硬币变化情况
func (b *BiliClient) GetCoinLogs() ([]*CoinLog, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/member/web/coin/log",
"GET",
nil,
)
if err != nil {
return nil, err
}
var logs struct {
List []*CoinLog `json:"list,omitempty"`
}
if err = json.Unmarshal(resp.Data, &logs); err != nil {
return nil, err
}
return logs.List, nil
}
// GetRelationStat
//
// 获取关系状态数
func (b *BiliClient) GetRelationStat(mid int64) (*RelationStat, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/relation/stat",
"GET",
map[string]string{
"vmid": strconv.FormatInt(mid, 10),
},
)
if err != nil {
return nil, err
}
var stat *RelationStat
if err = json.Unmarshal(resp.Data, &stat); err != nil {
return nil, err
}
return stat, nil
}
// GetUpStat
//
// 获取UP主状态数,该接口需要任意用户登录,否则不会返回任何数据
func (b *BiliClient) GetUpStat(mid int64) (*UpStat, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/space/upstat",
"GET",
map[string]string{
"mid": strconv.FormatInt(mid, 10),
},
)
if err != nil {
return nil, err
}
var stat *UpStat
if err = json.Unmarshal(resp.Data, &stat); err != nil {
return nil, err
}
return stat, nil
}
// GetMsgUnread 获取未读消息数
//
// Link:https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/message/msg.md#%E9%80%9A%E7%9F%A5%E6%B6%88%E6%81%AF
func (b *BiliClient) GetMsgUnread() (*MsgUnRead, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/msgfeed/unread",
"GET",
nil,
)
if err != nil {
return nil, err
}
var unread *MsgUnRead
if err = json.Unmarshal(resp.Data, &unread); err != nil {
return nil, err
}
return unread, nil
}
// SpaceSetTopArchive 设置置顶视频
//
// reason 备注 最大40字符
func (b *BiliClient) SpaceSetTopArchive(aid int64, reason string) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/top/arc/set",
"POST",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
"reason": reason,
},
)
if err != nil {
return err
}
return nil
}
// SpaceCancelTopArchive
//
// 取消置顶视频
func (b *BiliClient) SpaceCancelTopArchive() error {
_, err := b.RawParse(
BiliApiURL,
"x/space/top/arc/cancel",
"POST",
nil,
)
return err
}
// SpaceAddMasterpieces 添加代表作 上限为3个稿件
//
// reason 备注 最大40字符
func (b *BiliClient) SpaceAddMasterpieces(aid int64, reason string) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/masterpiece/add",
"POST",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
"reason": reason,
},
)
return err
}
// SpaceCancelMasterpiece
//
// 取消代表作视频
func (b *BiliClient) SpaceCancelMasterpiece(aid int64) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/masterpiece/cancel",
"POST",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
},
)
return err
}
// SpaceSetTags 设置用户个人TAG TAG里不要包含逗号会被当作分隔符
//
// 经过测试好像可以建54个TAG 各TAG长度小于10字符
//
// 删除TAG留空或省去即可 重复TAG会重复创建
//
// 感觉这个功能已经被废弃了
func (b *BiliClient) SpaceSetTags(tags []string) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/acc/tags/set",
"POST",
map[string]string{
"tags": util.StringSliceToString(tags, ","),
},
)
return err
}
// SpaceSetNotice 修改公告内容
//
// 删除公告留空即可 少于150字符
func (b *BiliClient) SpaceSetNotice(notice string) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/notice/set",
"POST",
map[string]string{
"notice": notice,
},
)
return err
}
// SpaceGetMyLastPlayGame
//
// 获取我的空间近期玩的游戏
func (b *BiliClient) SpaceGetMyLastPlayGame() ([]*SpaceGame, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/space/lastplaygame",
"GET",
map[string]string{
"mid": strconv.FormatInt(b.Me.MID, 10),
},
)
if err != nil {
return nil, err
}
var games []*SpaceGame
if err = json.Unmarshal(resp.Data, &games); err != nil {
return nil, err
}
return games, nil
}
// SpaceGetMyLastVideoCoin
//
// 获取我的最近投币的视频明细
func (b *BiliClient) SpaceGetMyLastVideoCoin() ([]*SpaceVideoCoin, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/space/coin/video",
"GET",
map[string]string{
"vmid": strconv.FormatInt(b.Me.MID, 10),
},
)
if err != nil {
return nil, err
}
var info []*SpaceVideoCoin
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
if info == nil {
return []*SpaceVideoCoin{}, nil
}
return info, nil
}
// ChanGetMy
//
// 获取我的频道列表
func (b *BiliClient) ChanGetMy() (*ChannelList, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/space/channel/list",
"GET",
map[string]string{
"mid": strconv.FormatInt(b.Me.MID, 10),
},
)
if err != nil {
return nil, err
}
var list *ChannelList
if err = json.Unmarshal(resp.Data, &list); err != nil {
return nil, err
}
return list, nil
}
// ChanAdd
//
// 创建频道 创建成功后会返回新建频道的id
//
// name 频道名
//
// intro 频道介绍
func (b *BiliClient) ChanAdd(name string, intro string) (int64, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/space/channel/add",
"POST",
map[string]string{
"name": name,
"intro": intro,
},
)
if err != nil {
return -1, err
}
var result struct {
CID int64 `json:"cid,omitempty"`
}
if err = json.Unmarshal(resp.Data, &result); err != nil {
return -1, err
}
return result.CID, nil
}
// ChanEdit 编辑频道
//
// cid 频道id
//
// name 频道名
//
// intro 频道介绍
func (b *BiliClient) ChanEdit(cid int64, name string, intro string) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/channel/edit",
"POST",
map[string]string{
"cid": strconv.FormatInt(cid, 10),
"name": name,
"intro": intro,
},
)
return err
}
// ChanDel 删除频道
//
// cid 频道id
func (b *BiliClient) ChanDel(cid int64) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/channel/del",
"POST",
map[string]string{
"cid": strconv.FormatInt(cid, 10),
},
)
return err
}
// ChanAddVideo 频道添加视频,返回添加错误的视频aid
//
// 仅能添加自己是UP主的视频
//
// 如添加多个视频,仅会添加正确的
func (b *BiliClient) ChanAddVideo(cid int64, aids []int64) ([]int64, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/space/channel/video/add",
"POST",
map[string]string{
"cid": strconv.FormatInt(cid, 10),
"aids": util.Int64SliceToString(aids, ","),
},
)
if err != nil {
return nil, err
}
var result []int64
if err = json.Unmarshal(resp.Data, &result); err != nil {
return nil, err
}
// 完成后需要使用接口「查询用户频道中的视频」刷新
if _, err := b.ChanGetMyVideo(cid, 1, 1); err != nil {
return nil, err
}
return result, nil
}
// ChanDelVideo
//
// 删除频道视频
func (b *BiliClient) ChanDelVideo(cid int64, aid int64) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/channel/video/del",
"POST",
map[string]string{
"cid": strconv.FormatInt(cid, 10),
"aid": strconv.FormatInt(aid, 10),
},
)
if err != nil {
return err
}
// 完成后需要使用接口「查询用户频道中的视频」刷新
if _, err = b.ChanGetMyVideo(cid, 1, 1); err != nil {
return err
}
return nil
}
// ChanSetVideoSort 调整频道视频顺序
//
// to 视频排序倒数位置 1为列表底部,视频总数为首端,与显示顺序恰好相反
func (b *BiliClient) ChanSetVideoSort(cid int64, aid int64, to int) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/channel/video/sort",
"POST",
map[string]string{
"cid": strconv.FormatInt(cid, 10),
"aid": strconv.FormatInt(aid, 10),
"to": strconv.Itoa(to),
},
)
return err
}
// ChanHasInvalidVideo
//
// 检查频道是否有失效视频,若有以错误形式返回(错误码:53005)
//
// 若Err为nil则没有无效视频
func (b *BiliClient) ChanHasInvalidVideo(cid int64) error {
_, err := b.RawParse(
BiliApiURL,
"x/space/channel/video/check",
"GET",
map[string]string{
"cid": strconv.FormatInt(cid, 10),
},
)
return err
}
// ChanGetMyVideo
//
// 获取我的频道视频
//
// cid 频道ID
//
// pn 页码
//
// ps 每页项数
func (b *BiliClient) ChanGetMyVideo(cid int64, pn int, ps int) (*ChanVideo, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/space/channel/video",
"GET",
map[string]string{
"mid": strconv.FormatInt(b.Me.MID, 10),
"cid": strconv.FormatInt(cid, 10),
"pn": strconv.Itoa(pn),
"ps": strconv.Itoa(ps),
},
)
if err != nil {
return nil, err
}
var videos *ChanVideo
if err = json.Unmarshal(resp.Data, &videos); err != nil {
return nil, err
}
return videos, nil
}
// FavGetMy
//
// 获取我的收藏夹列表
func (b *BiliClient) FavGetMy() (*FavoritesList, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/v3/fav/folder/created/list-all",
"GET",
map[string]string{
"up_mid": strconv.FormatInt(b.Me.MID, 10),
},
)
if err != nil {
return nil, err
}
var list = &FavoritesList{}
if err = json.Unmarshal(resp.Data, &list); err != nil {
return nil, err
}
return list, nil
}
// FavGetDetail
//
// 获取收藏夹详细信息
func (b *BiliClient) FavGetDetail(mlid int64) (*FavDetail, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/v3/fav/folder/info",
"GET",
map[string]string{
"media_id": strconv.FormatInt(mlid, 10),
},
)
if err != nil {
return nil, err
}
var detail = &FavDetail{}
if err = json.Unmarshal(resp.Data, &detail); err != nil {
return nil, err
}
return detail, nil
}
// FavAdd 新建收藏夹
//
// title 收藏夹标题
//
// intro 收藏夹简介
//
// privacy 是否私密 true:私密 false:公开
//
// cover 封面图url,会审核,不需要请留空
func (b *BiliClient) FavAdd(title string, intro string, privacy bool, cover string) (*FavDetail, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/v3/fav/folder/add",
"POST",
map[string]string{
"title": title,
"intro": intro,
"privacy": util.IF(privacy, "1", "0").(string),
"cover": cover,
},
)
if err != nil {
return nil, err
}
var detail = &FavDetail{}
if err = json.Unmarshal(resp.Data, &detail); err != nil {
return nil, err
}
return detail, nil
}
// FavEdit
//
// 编辑收藏夹 参数注释与 FavAdd 相同
func (b *BiliClient) FavEdit(mlid int64, title string, intro string, privacy bool, cover string) (*FavDetail, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/v3/fav/folder/edit",
"POST",
map[string]string{
"media_id": strconv.FormatInt(mlid, 10),
"title": title,
"intro": intro,
"privacy": util.IF(privacy, "1", "0").(string),
"cover": cover,
},
)
if err != nil {
return nil, err
}
var detail = &FavDetail{}
if err = json.Unmarshal(resp.Data, &detail); err != nil {
return nil, err
}
return detail, nil
}
// FavDel
//
// 删除收藏夹,传入需要删除的mlid数组
func (b *BiliClient) FavDel(mlids []int64) error {
_, err := b.RawParse(
BiliApiURL,
"x/v3/fav/folder/del",
"POST",
map[string]string{
"media_ids": util.Int64SliceToString(mlids, ","),
},
)
return err
}
// FavGetRes
//
// 获取收藏夹全部内容id
func (b *BiliClient) FavGetRes(mlid int64) ([]*FavRes, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/v3/fav/resource/ids",
"GET",
map[string]string{
"media_id": strconv.FormatInt(mlid, 10),
},
)
if err != nil {
return nil, err
}
var r = make([]*FavRes, 0)
if err = json.Unmarshal(resp.Data, &r); err != nil {
return nil, err
}
return r, nil
}
// FavGetResDetail 获取收藏夹内容详细内容,带过滤功能
//
// tid 分区id,用于筛选,传入0代表所有分区
//
// keyword 关键词筛选 可留空
//
// order 留空默认按收藏时间
//
// 按收藏时间:mtime
// 按播放量: view
// 按投稿时间:pubtime
//
// tp 内容类型 不知道作用,传入0即可
//
// pn 页码
//
// ps 每页项数 ps不能太大,会报错
func (b *BiliClient) FavGetResDetail(mlid int64, tid int, keyword string, order string, tp int, pn int, ps int) (*FavResDetail, error) {
resp, err := b.RawParse(
BiliApiURL,
"x/v3/fav/resource/list",
"GET",
map[string]string{
"media_id": strconv.FormatInt(mlid, 10),
"tid": strconv.Itoa(tid),
"keyword": keyword,