Skip to content

Commit

Permalink
[feature] #41 基于 GetUserVideos 最小实现
Browse files Browse the repository at this point in the history
  • Loading branch information
RunsTp committed Apr 12, 2024
1 parent 336665b commit bd4d9a3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
21 changes: 20 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

type Client struct {
wbi *WBI
resty *resty.Client
}

Expand All @@ -27,7 +28,10 @@ func New() *Client {

// NewWithClient 接收一个自定义的*resty.Client为参数
func NewWithClient(restyClient *resty.Client) *Client {
return &Client{resty: restyClient}
return &Client{
wbi: NewDefaultWbi(),
resty: restyClient,
}
}

func (c *Client) Resty() *resty.Client {
Expand All @@ -51,6 +55,21 @@ func (c *Client) SetCookiesString(cookiesString string) {
}}}).Cookies())
}

// SetCookies 设置Cookies
func (c *Client) SetCookies(cookies []*http.Cookie) {
c.resty.SetCookies(cookies)
}

// SetRawCookies 这个 RawCookie 可以直接从浏览器 request的header中复制出来,然后直接设置
func (c *Client) SetRawCookies(rawCookies string) {
header := http.Header{}
header.Add("Cookie", rawCookies)
req := http.Request{Header: header}

c.SetCookies(req.Cookies())
}

// GetCookies 获取当前的cookies
func (c *Client) GetCookies() []*http.Cookie {
return c.resty.Cookies
}
Expand Down
18 changes: 12 additions & 6 deletions user.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package bilibili

import (
"github.com/pkg/errors"
)
type GetUserVideosParam struct {
Mid int `json:"mid"` // 目标用户mid
}

type GetUserVideos struct {
}

// GetUserVideos 查询用户投稿视频明细
func (c *Client) GetUserVideos() error {
// https://api.bilibili.com/x/space/wbi/arc/search
return errors.New("wbi还未实现,本接口暂时无法使用")
func (c *Client) GetUserVideos(param GetUserVideosParam) (*GetUserVideos, error) {
const (
method = "GET"
url = "https://api.bilibili.com/x/space/wbi/arc/search"
)
return execute[*GetUserVideos](c, method, url, param, fillWbiHandler(c.wbi, c.GetCookies()))
}

type GetUserCardParam struct {
Expand Down
16 changes: 16 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package bilibili

import (
"encoding/json"
"net/http"
"reflect"
"strings"
"time"
"unicode"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -31,6 +33,20 @@ func fillParam(key, value string) paramHandler {
}
}

func fillWbiHandler(wbi *WBI, cookies []*http.Cookie) func(*resty.Request) error {
return func(r *resty.Request) error {
newQuery, err := wbi.SignQuery(r.QueryParam, time.Now())
if err != nil {
return err
}

r.QueryParam = newQuery
r.Cookies = cookies
r.Header.Del("Referer")
return nil
}
}

// execute 发起请求
func execute[Out any](c *Client, method, url string, in any, handlers ...paramHandler) (out Out, err error) {
r := c.resty.R()
Expand Down

0 comments on commit bd4d9a3

Please sign in to comment.