-
Notifications
You must be signed in to change notification settings - Fork 15
/
client.go
100 lines (86 loc) · 2.77 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
package bilibili
import (
"net/http"
"strings"
"time"
"github.com/go-resty/resty/v2"
)
type Client struct {
wbi *WBI
resty *resty.Client
}
// New 返回一个默认的 bilibili.Client
func New() *Client {
restyClient := resty.New().
SetRedirectPolicy(resty.NoRedirectPolicy()).
SetTimeout(20*time.Second).
SetHeader("Accept", "application/json").
SetHeader("Accept-Language", "zh-CN,zh;q=0.9").
SetHeader("Origin", "https://www.bilibili.com").
SetHeader("Referer", "https://www.bilibili.com/").
SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0")
return NewWithClient(restyClient)
}
// NewWithClient 接收一个自定义的*resty.Client为参数
func NewWithClient(restyClient *resty.Client) *Client {
return &Client{
wbi: NewDefaultWbi(),
resty: restyClient,
}
}
func (c *Client) Resty() *resty.Client {
return c.resty
}
// GetCookiesString 获取字符串格式的cookies,方便自行存储后下次使用。配合下面的 SetCookiesString 使用。
func (c *Client) GetCookiesString() string {
cookies := c.resty.Cookies
cookieStrings := make([]string, 0, len(cookies))
for _, cookie := range c.resty.Cookies {
cookieStrings = append(cookieStrings, cookie.String())
}
return strings.Join(cookieStrings, "\n")
}
// SetCookiesString 设置Cookies,但是是字符串格式,配合 GetCookiesString 使用。有些功能必须登录或设置Cookies后才能使用。
func (c *Client) SetCookiesString(cookiesString string) {
c.SetCookies((&resty.Response{RawResponse: &http.Response{Header: http.Header{
"Set-Cookie": strings.Split(cookiesString, "\n"),
}}}).Cookies())
}
// SetRawCookies 如果你是从浏览器request的header中直接复制出来的cookies,调用这个函数。
func (c *Client) SetRawCookies(rawCookies string) {
header := http.Header{}
header.Add("Cookie", rawCookies)
req := http.Request{Header: header}
c.SetCookies(req.Cookies())
}
// SetCookie 设置单个cookie
func (c *Client) SetCookie(cookie *http.Cookie) {
for i, c0 := range c.resty.Cookies {
if c0.Name == cookie.Name {
c.resty.Cookies[i] = cookie
return
}
}
c.resty.Cookies = append(c.resty.Cookies, cookie)
}
// SetCookies 设置cookies
func (c *Client) SetCookies(cookies []*http.Cookie) {
for _, cookie := range cookies {
c.SetCookie(cookie)
}
}
// GetCookies 获取当前的cookies
func (c *Client) GetCookies() []*http.Cookie {
return c.resty.Cookies
}
// 根据key获取指定的cookie值
func (c *Client) getCookie(name string) string {
now := time.Now()
// 查找指定name的cookie
for _, cookie := range c.resty.Cookies {
if cookie.Name == name && (cookie.Expires.IsZero() || cookie.Expires.After(now)) {
return cookie.Value
}
}
return ""
}