-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
121 lines (108 loc) · 3.26 KB
/
channel.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
package channel
import (
"net/http/cookiejar"
"strconv"
"time"
"emperror.dev/errors"
"github.com/go-resty/resty/v2"
)
var (
userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
)
type Channel struct {
http *resty.Client
channelID int
}
func New(cookieJar *cookiejar.Jar, channelID int) *Channel {
http := resty.New().
SetCookieJar(cookieJar).
SetRetryCount(3).
SetRetryWaitTime(5*time.Second).
SetRetryMaxWaitTime(20*time.Second).
SetHostURL("https://api.bilibili.com").
SetHeader("User-Agent", userAgent)
channel := Channel{
http: http,
channelID: channelID,
}
return &channel
}
func (c *Channel) newlist(pageNum, pageSize int) (*NewListResult, error) {
rsp, err := c.http.R().
SetQueryParams(map[string]string{
"rid": strconv.Itoa(c.channelID),
"type": "0",
"pn": strconv.Itoa(pageNum),
"ps": strconv.Itoa(pageSize),
"jsonp": "json",
"_": strconv.FormatInt(time.Now().UnixNano()/1000000, 10),
}).
SetResult(&NewListResult{}).
Get("/x/web-interface/newlist")
if err != nil {
return nil, errors.WithMessage(err, "newlist http error")
}
result := rsp.Result().(*NewListResult)
if result.Code != 0 {
return result, errors.Errorf("newlist result: errorCode=%d errorMessage=%s", result.Code, result.Message)
}
return result, nil
}
func (c *Channel) archivesByTag(tagID, pageNum, pageSize int) (*NewListResult, error) {
rsp, err := c.http.R().
SetQueryParams(map[string]string{
"tag_id": strconv.Itoa(tagID),
"rid": strconv.Itoa(c.channelID),
"type": "0",
"pn": strconv.Itoa(pageNum),
"ps": strconv.Itoa(pageSize),
"jsonp": "json",
"_": strconv.FormatInt(time.Now().UnixNano()/1000000, 10),
}).
SetResult(&NewListResult{}).
Get("/x/tag/ranking/archives")
if err != nil {
return nil, errors.WithMessage(err, "archivesByTag http error")
}
result := rsp.Result().(*NewListResult)
if result.Code != 0 {
return result, errors.Errorf(
"archivesByTag result: errorCode=%d errorMessage=%s",
result.Code, result.Message,
)
}
return result, nil
}
// order: click 播放数, scores 评论数, stow 收藏数, coin 硬币数, dm 弹幕数
// time format: 20200224/20200302
func (c *Channel) search(order string, pageNum, pageSize int, timeFrom, timeTo string, tagName string) (*SearchResult, error) {
queryParams := map[string]string{
"main_ver": "v3",
"search_type": "video",
"view_type": "hot_rank",
"order": order,
"copy_right": "-1",
"cate_id": strconv.Itoa(c.channelID),
"page": strconv.Itoa(pageNum),
"pagesize": strconv.Itoa(pageSize),
"jsonp": "json",
"time_from": timeFrom,
"time_to": timeTo,
"_": strconv.FormatInt(time.Now().UnixNano()/1000000, 10),
}
if tagName != "" {
queryParams["keyword"] = tagName
}
rsp, err := c.http.R().
SetQueryParams(queryParams).
SetResult(&SearchResult{}).
Get("https://s.search.bilibili.com/cate/search")
if err != nil {
return nil, errors.WithMessage(err, "search http error")
}
searchResult := rsp.Result().(*SearchResult)
if searchResult.Code != 0 || searchResult.Msg != "success" {
return searchResult, errors.New(searchResult.Msg)
}
return searchResult, nil
}