-
Notifications
You must be signed in to change notification settings - Fork 21
/
comm_client.go
1386 lines (1321 loc) · 30.5 KB
/
comm_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"
"github.com/golang/protobuf/proto"
"github.com/iyear/biligo/internal/util"
"github.com/iyear/biligo/proto/dm"
"github.com/tidwall/gjson"
"net/http"
"strconv"
)
type CommClient struct {
*baseClient
}
type CommSetting struct {
// 自定义http client
//
// 默认为 http.http.DefaultClient
Client *http.Client
// Debug模式 true将输出请求信息 false不输出
//
// 默认false
DebugMode bool
// 自定义UserAgent
//
// 默认Chrome随机Agent
UserAgent string
}
// NewCommClient
//
// Setting的Auth属性可以随意填写或传入nil,Auth不起到作用,用于访问公共API
func NewCommClient(setting *CommSetting) *CommClient {
return &CommClient{baseClient: newBaseClient(&baseSetting{
Client: setting.Client,
DebugMode: setting.DebugMode,
UserAgent: setting.UserAgent,
Prefix: "CommClient ",
})}
}
// SetClient
//
// 设置Client,可以用来更换代理等操作
func (c *CommClient) SetClient(client *http.Client) {
c.client = client
}
// SetUA
//
// 设置UA
func (c *CommClient) SetUA(ua string) {
c.ua = ua
}
// Raw
//
// base末尾带/
func (c *CommClient) Raw(base, endpoint, method string, payload map[string]string) ([]byte, error) {
// 不用侵入处理则传入nil
raw, err := c.raw(base, endpoint, method, payload, nil, nil)
if err != nil {
return nil, err
}
return raw, nil
}
// RawParse
//
// base末尾带/
func (c *CommClient) RawParse(base, endpoint, method string, payload map[string]string) (*Response, error) {
raw, err := c.Raw(base, endpoint, method, payload)
if err != nil {
return nil, err
}
return c.parse(raw)
}
// GetGeoInfo 调用哔哩哔哩API获取地理位置等信息
//
// 会受到自定义 http.Client 代理的影响
func (c *CommClient) GetGeoInfo() (*GeoInfo, error) {
resp, err := c.RawParse(BiliApiURL,
"x/web-interface/zone",
"GET",
nil,
)
if err != nil {
return nil, err
}
var info *GeoInfo
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
return info, nil
}
// FollowingsGetDetail 获取个人详细的关注列表
//
// pn 页码
//
// ps 每页项数,最大50
func (c *CommClient) FollowingsGetDetail(mid int64, pn int, ps int) (*FollowingsDetail, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/relation/followings",
"GET",
map[string]string{
"vmid": strconv.FormatInt(mid, 10),
"pn": strconv.Itoa(pn),
"ps": strconv.Itoa(ps),
},
)
if err != nil {
return nil, err
}
var detail = &FollowingsDetail{}
if err = json.Unmarshal(resp.Data, &detail); err != nil {
return nil, err
}
return detail, nil
}
// VideoGetStat
//
// 获取稿件状态数
func (c *CommClient) VideoGetStat(aid int64) (*VideoSingleStat, error) {
resp, err := c.RawParse(BiliApiURL,
"x/web-interface/archive/stat",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
},
)
if err != nil {
return nil, err
}
var stat *VideoSingleStat
if err = json.Unmarshal(resp.Data, &stat); err != nil {
return nil, err
}
return stat, nil
}
// VideoGetInfo 返回视频详细信息,数据较多,可以使用单独的接口获取部分数据
//
// 限制游客访问的视频会返回错误,请使用 BiliClient 发起请求
func (c *CommClient) VideoGetInfo(aid int64) (*VideoInfo, error) {
resp, err := c.RawParse(BiliApiURL,
"x/web-interface/view",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
},
)
if err != nil {
return nil, err
}
var info *VideoInfo
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
return info, nil
}
// VideoGetDescription
//
// 获取稿件简介
func (c *CommClient) VideoGetDescription(aid int64) (string, error) {
resp, err := c.RawParse(BiliApiURL,
"x/web-interface/archive/desc",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
},
)
if err != nil {
return "", err
}
var desc string
if err = json.Unmarshal(resp.Data, &desc); err != nil {
return "", err
}
return desc, nil
}
// VideoGetPageList
//
// 获取分P列表
func (c *CommClient) VideoGetPageList(aid int64) ([]*VideoPage, error) {
resp, err := c.RawParse(BiliApiURL,
"x/player/pagelist",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
},
)
if err != nil {
return nil, err
}
var list []*VideoPage
if err = json.Unmarshal(resp.Data, &list); err != nil {
return nil, err
}
return list, nil
}
// VideoGetOnlineNum
//
// 返回所有终端总计在线观看人数和WEB端在线观看人数 (用类似10万+的文字表示) cid用于分P标识
func (c *CommClient) VideoGetOnlineNum(aid int64, cid int64) (total string, web string, e error) {
resp, err := c.RawParse(BiliApiURL,
"x/player/online/total",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
"cid": strconv.FormatInt(cid, 10),
},
)
if err != nil {
return "", "", err
}
var num struct {
Total string `json:"total,omitempty"`
Count string `json:"count,omitempty"`
}
if err = json.Unmarshal(resp.Data, &num); err != nil {
return "", "", err
}
return num.Total, num.Count, nil
}
// VideoTags
//
// 未登录无法获取 IsAtten,Liked,Hated 字段
func (c *CommClient) VideoTags(aid int64) ([]*VideoTag, error) {
resp, err := c.RawParse(BiliApiURL,
"x/tag/archive/tags",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
},
)
if err != nil {
return nil, err
}
var tags []*VideoTag
if err = json.Unmarshal(resp.Data, &tags); err != nil {
return nil, err
}
return tags, nil
}
// VideoGetRecommend 获取视频的相关视频推荐
//
// 最多获取40条推荐视频
func (c *CommClient) VideoGetRecommend(aid int64) ([]*VideoRecommendInfo, error) {
resp, err := c.RawParse(BiliApiURL,
"x/web-interface/archive/related",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
},
)
if err != nil {
return nil, err
}
var videos []*VideoRecommendInfo
if err = json.Unmarshal(resp.Data, &videos); err != nil {
return nil, err
}
return videos, nil
}
// VideoGetPlayURL 获取视频取流地址
//
// 所有参数、返回信息和取流方法的说明请直接前往:https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/video/videostream_url.md
func (c *CommClient) VideoGetPlayURL(aid int64, cid int64, qn int, fnval int) (*VideoPlayURLResult, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/player/playurl",
"GET",
map[string]string{
"avid": strconv.FormatInt(aid, 10),
"cid": strconv.FormatInt(cid, 10),
"qn": strconv.Itoa(qn),
"fnval": strconv.Itoa(fnval),
"fnver": "0",
"fourk": "1",
},
)
if err != nil {
return nil, err
}
var r *VideoPlayURLResult
if err = json.Unmarshal(resp.Data, &r); err != nil {
return nil, err
}
return r, nil
}
// VideoShot 获取视频快照
//
// cid属性非必须 传入0表示1P
//
// index为JSON数组截取时间表 true:需要 false:不需要 传入false则Index属性为空
func (c *CommClient) VideoShot(aid int64, cid int64, index bool) (*VideoShot, error) {
resp, err := c.RawParse(BiliApiURL,
"x/player/videoshot",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
"cid": util.IF(cid == 0, "", strconv.FormatInt(cid, 10)).(string),
"index": util.IF(index, "1", "0").(string),
},
)
if err != nil {
return nil, err
}
var shot *VideoShot
if err = json.Unmarshal(resp.Data, &shot); err != nil {
return nil, err
}
return shot, nil
}
// DanmakuGetLikes 获取弹幕点赞数,一次可以获取多条弹幕
//
// Link:https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/danmaku/action.md#%E6%9F%A5%E8%AF%A2%E5%BC%B9%E5%B9%95%E7%82%B9%E8%B5%9E%E6%95%B0
//
// 返回一个map,key为dmid,value为相关信息
// 未登录时UserLike属性恒为0
func (c *CommClient) DanmakuGetLikes(cid int64, dmids []uint64) (map[uint64]*DanmakuGetLikesResult, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/v2/dm/thumbup/stats",
"GET",
map[string]string{
"oid": strconv.FormatInt(cid, 10),
"ids": util.Uint64SliceToString(dmids, ","),
},
)
if err != nil {
return nil, err
}
var result = make(map[uint64]*DanmakuGetLikesResult)
for _, dmid := range dmids {
var r *DanmakuGetLikesResult
if err = json.Unmarshal([]byte(gjson.Get(string(resp.Data), strconv.FormatUint(dmid, 10)).Raw), &r); err != nil {
return nil, err
}
result[dmid] = r
}
return result, nil
}
// GetRelationStat
//
// 获取关系状态数,Whisper和Black恒为0
func (c *CommClient) GetRelationStat(mid int64) (*RelationStat, error) {
resp, err := c.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
}
// SpaceGetTopArchive
//
// 获取空间置顶稿件
func (c *CommClient) SpaceGetTopArchive(mid int64) (*SpaceVideo, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/top/arc",
"GET",
map[string]string{
"vmid": strconv.FormatInt(mid, 10),
},
)
if err != nil {
return nil, err
}
var top *SpaceVideo
if err = json.Unmarshal(resp.Data, &top); err != nil {
return nil, err
}
return top, nil
}
// SpaceGetMasterpieces
//
// 获取UP代表作
func (c *CommClient) SpaceGetMasterpieces(mid int64) ([]*SpaceVideo, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/masterpiece",
"GET",
map[string]string{
"vmid": strconv.FormatInt(mid, 10),
},
)
if err != nil {
return nil, err
}
var mp []*SpaceVideo
if err = json.Unmarshal(resp.Data, &mp); err != nil {
return nil, err
}
return mp, nil
}
// SpaceGetTags
//
// 获取空间用户个人TAG 上限5条,且内容由用户自定义 带有转义
func (c *CommClient) SpaceGetTags(mid int64) ([]string, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/acc/tags",
"GET",
map[string]string{
"mid": strconv.FormatInt(mid, 10),
},
)
if err != nil {
return nil, err
}
// B站这写的是个啥玩意儿
var tags []string
for _, tag := range gjson.Get(string(resp.Data), "0.tags").Array() {
tags = append(tags, tag.String())
}
return tags, nil
}
// SpaceGetNotice
//
// 获取空间公告内容
func (c *CommClient) SpaceGetNotice(mid int64) (string, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/notice",
"GET",
map[string]string{
"mid": strconv.FormatInt(mid, 10),
},
)
if err != nil {
return "", err
}
// 新建一个变量再 unmarshal 可以把转义部分转回来
// 直接返回 resp.Data 会带转义符
var notice string
if err = json.Unmarshal(resp.Data, ¬ice); err != nil {
return "", err
}
return notice, nil
}
// SpaceGetLastPlayGame
//
// 获取用户空间近期玩的游戏
func (c *CommClient) SpaceGetLastPlayGame(mid int64) ([]*SpaceGame, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/lastplaygame",
"GET",
map[string]string{
"mid": strconv.FormatInt(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
}
// SpaceGetLastVideoCoin
//
// 获取用户最近投币的视频明细 如设置隐私查看自己的使用 BiliClient 访问
func (c *CommClient) SpaceGetLastVideoCoin(mid int64) ([]*SpaceVideoCoin, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/coin/video",
"GET",
map[string]string{
"vmid": strconv.FormatInt(mid, 10),
},
)
if err != nil {
return nil, err
}
var info []*SpaceVideoCoin
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
return info, nil
}
// SpaceSearchVideo
//
// 获取用户投稿视频明细
//
// order 排序方式 默认为pubdate 可留空
//
// 最新发布:pubdate
// 最多播放:click
// 最多收藏:stow
//
// tid 筛选分区 0:不进行分区筛选
//
// keyword 关键词 可留空
//
// pn 页码
//
// ps 每页项数
func (c *CommClient) SpaceSearchVideo(mid int64, order string, tid int, keyword string, pn int, ps int) (*SpaceVideoSearchResult, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/wbi/arc/search",
"GET",
map[string]string{
"mid": strconv.FormatInt(mid, 10),
"order": order,
"tid": strconv.Itoa(tid),
"keyword": keyword,
"pn": strconv.Itoa(pn),
"ps": strconv.Itoa(ps),
},
)
if err != nil {
return nil, err
}
var result *SpaceVideoSearchResult
if err = json.Unmarshal(resp.Data, &result); err != nil {
return nil, err
}
return result, nil
}
// ChanGet
//
// 获取用户频道列表
func (c *CommClient) ChanGet(mid int64) (*ChannelList, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/channel/list",
"GET",
map[string]string{
"mid": strconv.FormatInt(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
}
// ChanGetVideo
//
// 获取用户频道视频
//
// cid 频道ID
//
// pn 页码
//
// ps 每页项数
func (c *CommClient) ChanGetVideo(mid int64, cid int64, pn int, ps int) (*ChanVideo, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/space/channel/video",
"GET",
map[string]string{
"mid": strconv.FormatInt(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
}
// FavGet
//
// 获取用户的公开收藏夹列表
func (c *CommClient) FavGet(mid int64) (*FavoritesList, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/v3/fav/folder/created/list-all",
"GET",
map[string]string{
"up_mid": strconv.FormatInt(mid, 10),
},
)
if err != nil {
return nil, err
}
var list *FavoritesList
if err = json.Unmarshal(resp.Data, &list); err != nil {
return nil, err
}
if list == nil {
return &FavoritesList{}, nil
}
return list, nil
}
// FavGetDetail
//
// 获取收藏夹详细信息,部分信息需要登录,请使用 BiliClient 请求
func (c *CommClient) FavGetDetail(mlid int64) (*FavDetail, error) {
resp, err := c.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
}
// FavGetRes
//
// 获取收藏夹全部内容id 查询权限收藏夹时请使用 BiliClient 请求
func (c *CommClient) FavGetRes(mlid int64) ([]*FavRes, error) {
resp, err := c.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 []*FavRes
if err = json.Unmarshal(resp.Data, &r); err != nil {
return nil, err
}
return r, nil
}
// FavGetResDetail 获取收藏夹内容详细内容,带过滤功能
//
// 查询权限收藏夹时请使用 BiliClient 请求
//
// tid 分区id,用于筛选,传入0代表所有分区
//
// keyword 关键词筛选 可留空
//
// order 留空默认按收藏时间
//
// 按收藏时间:mtime
// 按播放量: view
// 按投稿时间:pubtime
//
// tp 内容类型 不知道作用,传入0即可
//
// pn 页码
//
// ps 每页项数 ps不能太大,会报错
func (c *CommClient) FavGetResDetail(mlid int64, tid int, keyword string, order string, tp int, pn int, ps int) (*FavResDetail, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/v3/fav/resource/list",
"GET",
map[string]string{
"media_id": strconv.FormatInt(mlid, 10),
"tid": strconv.Itoa(tid),
"keyword": keyword,
"order": order,
"type": strconv.Itoa(tp),
"ps": strconv.Itoa(ps),
"pn": strconv.Itoa(pn),
},
)
if err != nil {
return nil, err
}
var detail *FavResDetail
if err = json.Unmarshal(resp.Data, &detail); err != nil {
return nil, err
}
return detail, nil
}
// GetDailyNum
//
// 获取每日分区投稿数
func (c *CommClient) GetDailyNum() (map[int]int, error) {
resp, err := c.RawParse(BiliApiURL, "x/web-interface/online", "GET", nil)
if err != nil {
return nil, err
}
var result = make(map[int]int)
gjson.Get(string(resp.Data), "region_count").ForEach(func(key, value gjson.Result) bool {
result[int(key.Int())] = int(value.Int())
return true
})
return result, nil
}
// GetUnixNow
//
// 获取服务器的Unix时间戳
func (c *CommClient) GetUnixNow() (int64, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/report/click/now",
"GET",
nil,
)
if err != nil {
return -1, err
}
var t struct {
Now int64 `json:"now,omitempty"`
}
if err = json.Unmarshal(resp.Data, &t); err != nil {
return -1, err
}
return t.Now, nil
}
// DanmakuGetByPb
//
// 获取实时弹幕(protobuf接口)
func (c *CommClient) DanmakuGetByPb(tp int, cid int64, seg int) (*DanmakuResp, error) {
resp, err := c.Raw(
BiliApiURL,
"x/v2/dm/web/seg.so",
"GET",
map[string]string{
"type": strconv.Itoa(tp),
"oid": strconv.FormatInt(cid, 10),
"segment_index": strconv.Itoa(seg),
},
)
if err != nil {
return nil, err
}
var reply dm.DmSegMobileReply
var r = &DanmakuResp{}
if err := proto.Unmarshal(resp, &reply); err != nil {
return nil, err
}
for _, elem := range reply.GetElems() {
r.Danmaku = append(r.Danmaku, &Danmaku{
ID: uint64(elem.Id),
Progress: int64(elem.Progress),
Mode: int(elem.Mode),
FontSize: int(elem.Fontsize),
Color: int(elem.Color),
MidHash: elem.MidHash,
Content: elem.Content,
Ctime: elem.Ctime,
Weight: int(elem.Weight),
Action: elem.Action,
Pool: int(elem.Pool),
IDStr: elem.IdStr,
Attr: int(elem.Attr),
})
}
return r, nil
}
// DanmakuGetShot
//
// 获取弹幕快照(最新的几条弹幕)
func (c *CommClient) DanmakuGetShot(aid int64) ([]string, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/v2/dm/ajax",
"GET",
map[string]string{
"aid": strconv.FormatInt(aid, 10),
},
)
if err != nil {
return nil, err
}
var strings []string
if err = json.Unmarshal(resp.Data, &strings); err != nil {
return nil, err
}
return strings, nil
}
// EmoteGetFreePack 获取免费表情包列表
//
// business 使用场景 reply:评论区 dynamic:动态
//
// 全为免费表情包,如需获取个人专属表情包请使用 BiliClient 请求
func (c *CommClient) EmoteGetFreePack(business string) ([]*EmotePack, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/emote/user/panel/web",
"GET",
map[string]string{
"business": business,
},
)
if err != nil {
return nil, err
}
var pack struct {
Packages []*EmotePack `json:"packages,omitempty"`
}
if err = json.Unmarshal(resp.Data, &pack); err != nil {
return nil, err
}
return pack.Packages, nil
}
// EmoteGetPackDetail 获取指定表情包明细
//
// business 使用场景 reply:评论区 dynamic:动态
//
// ids 多个表情包id的数组
func (c *CommClient) EmoteGetPackDetail(business string, ids []int64) ([]*EmotePack, error) {
resp, err := c.RawParse(
BiliApiURL,
"x/emote/package",
"GET",
map[string]string{
"business": business,
"ids": util.Int64SliceToString(ids, ","),
},
)
if err != nil {
return nil, err
}
var packs struct {
Packages []*EmotePack `json:"packages,omitempty"`
}
if err = json.Unmarshal(resp.Data, &packs); err != nil {
return nil, err
}
return packs.Packages, nil
}
// AudioGetInfo
//
// 获取音频信息 部分属性需要登录,请使用 BiliClient 请求
func (c *CommClient) AudioGetInfo(auid int64) (*AudioInfo, error) {
resp, err := c.RawParse(
BiliMainURL,
"audio/music-service-c/web/song/info",
"GET",
map[string]string{
"sid": strconv.FormatInt(auid, 10),
},
)
if err != nil {
return nil, err
}
var info *AudioInfo
if err = json.Unmarshal(resp.Data, &info); err != nil {
return nil, err
}
return info, nil
}
// AudioGetTags 获取音频TAGs
//
// 根据页面显示观察,应该是歌曲分类
func (c *CommClient) AudioGetTags(auid int64) ([]*AudioTag, error) {
resp, err := c.RawParse(
BiliMainURL,
"audio/music-service-c/web/tag/song",
"GET",
map[string]string{
"sid": strconv.FormatInt(auid, 10),
},
)
if err != nil {
return nil, err
}
var tags []*AudioTag
if err = json.Unmarshal(resp.Data, &tags); err != nil {
return nil, err
}
return tags, nil
}
// AudioGetMembers
//
// 获取音频创作者信息
func (c *CommClient) AudioGetMembers(auid int64) ([]*AudioMember, error) {
resp, err := c.RawParse(
BiliMainURL,
"audio/music-service-c/web/member/song",
"GET",
map[string]string{
"sid": strconv.FormatInt(auid, 10),
},
)
if err != nil {
return nil, err
}
var members []*AudioMember
if err = json.Unmarshal(resp.Data, &members); err != nil {
return nil, err
}
return members, nil
}
// AudioGetLyric 获取音频歌词
//
// 同 AudioGetInfo 中的lrc歌词
func (c *CommClient) AudioGetLyric(auid int64) (string, error) {
resp, err := c.RawParse(
BiliMainURL,
"audio/music-service-c/web/song/lyric",
"GET",
map[string]string{
"sid": strconv.FormatInt(auid, 10),
},
)
if err != nil {
return "", err
}
var lrc string
if err = json.Unmarshal(resp.Data, &lrc); err != nil {
return "", err
}
return lrc, nil
}
// AudioGetStat 获取歌曲状态数
//
// 没有投币数 获取投币数请使用 AudioGetInfo
func (c *CommClient) AudioGetStat(auid int64) (*AudioInfoStat, error) {
resp, err := c.RawParse(
BiliMainURL,
"audio/music-service-c/web/stat/song",
"GET",
map[string]string{
"sid": strconv.FormatInt(auid, 10),
},
)
if err != nil {
return nil, err
}
var stat = &AudioInfoStat{}
if err = json.Unmarshal(resp.Data, &stat); err != nil {
return nil, err
}
return stat, nil
}
// AudioGetPlayURL 获取音频流URL
//
// 最多获取到
//
// qn 音质
//
// 0 流畅 128K
//
// 1 标准 192K