From bd4d9a3f2a900c9a9b5896da9335202402ff6683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=AD=A3?= Date: Fri, 12 Apr 2024 18:13:28 +0800 Subject: [PATCH] =?UTF-8?q?[feature]=20#41=20=E5=9F=BA=E4=BA=8E=20GetUserV?= =?UTF-8?q?ideos=20=E6=9C=80=E5=B0=8F=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 21 ++++++++++++++++++++- user.go | 18 ++++++++++++------ util.go | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 5703b05..136d4a0 100644 --- a/client.go +++ b/client.go @@ -9,6 +9,7 @@ import ( ) type Client struct { + wbi *WBI resty *resty.Client } @@ -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 { @@ -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 } diff --git a/user.go b/user.go index 2601e89..86d69c0 100644 --- a/user.go +++ b/user.go @@ -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 { diff --git a/util.go b/util.go index 155bcfd..9558aec 100644 --- a/util.go +++ b/util.go @@ -2,8 +2,10 @@ package bilibili import ( "encoding/json" + "net/http" "reflect" "strings" + "time" "unicode" "github.com/go-resty/resty/v2" @@ -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()