Skip to content

Commit

Permalink
优化部分接口
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed Apr 3, 2024
1 parent c1ea228 commit a3dc96f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 149 deletions.
88 changes: 44 additions & 44 deletions dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func (c *Client) UploadDynamicBfs(fileName string, file io.Reader, category stri
ImageHeight int `json:"image_height"`
}]
if err = json.Unmarshal(resp.Body(), &response); err != nil {
return "", Size{}, err
return "", Size{}, errors.WithStack(err)
}
if response.Code != 0 {
return "", Size{}, errors.Errorf("错误码: %d, 错误信息: %s", response.Code, response.Message)
Expand Down Expand Up @@ -562,7 +562,7 @@ type DynamicItem struct {
} `json:"like_icon"`
RidStr string `json:"rid_str"`
} `json:"basic"`
IdStr interface{} `json:"id_str"` // 这个字段,B站返回的数据有时是number,有时是string,不知道为什么。这里用interface{}会带来一个问题,number会解析成为float64,有可能存在丢失精度问题。请谨慎使用
IdStr any `json:"id_str"` // 这个字段,B站返回的数据有时是number,有时是string,不知道为什么。这里用any会带来一个问题,number会解析成为float64,有可能存在丢失精度问题。请谨慎使用
Modules struct {
ModuleAuthor struct {
Avatar struct {
Expand Down Expand Up @@ -630,13 +630,13 @@ type DynamicItem struct {
} `json:"fallback_layers"`
Mid string `json:"mid"`
} `json:"avatar"`
Face string `json:"face"`
FaceNft bool `json:"face_nft"`
Following interface{} `json:"following"`
JumpUrl string `json:"jump_url"`
Label string `json:"label"`
Mid int `json:"mid"`
Name string `json:"name"`
Face string `json:"face"`
FaceNft bool `json:"face_nft"`
Following any `json:"following"`
JumpUrl string `json:"jump_url"`
Label string `json:"label"`
Mid int `json:"mid"`
Name string `json:"name"`
OfficialVerify struct {
Desc string `json:"desc"`
Type int `json:"type"`
Expand Down Expand Up @@ -680,14 +680,14 @@ type DynamicItem struct {
} `json:"vip"`
} `json:"module_author"`
ModuleDynamic struct {
Additional interface{} `json:"additional"`
Additional any `json:"additional"`
Desc *struct {
RichTextNodes []struct {
OrigText string `json:"orig_text"`
Text string `json:"text"`
Type string `json:"type"`
JumpUrl string `json:"jump_url,omitempty"`
Style interface{} `json:"style"`
OrigText string `json:"orig_text"`
Text string `json:"text"`
Type string `json:"type"`
JumpUrl string `json:"jump_url,omitempty"`
Style any `json:"style"`
Emoji struct {
IconUrl string `json:"icon_url"`
Size int `json:"size"`
Expand All @@ -702,21 +702,21 @@ type DynamicItem struct {
Draw struct {
Id int `json:"id"`
Items []struct {
Height int `json:"height"`
Size float64 `json:"size"`
Src string `json:"src"`
Tags []interface{} `json:"tags"`
Width int `json:"width"`
Height int `json:"height"`
Size float64 `json:"size"`
Src string `json:"src"`
Tags []any `json:"tags"`
Width int `json:"width"`
} `json:"items"`
} `json:"draw,omitempty"`
Type string `json:"type"`
Archive struct {
Aid string `json:"aid"`
Badge struct {
BgColor string `json:"bg_color"`
Color string `json:"color"`
IconUrl interface{} `json:"icon_url"`
Text string `json:"text"`
BgColor string `json:"bg_color"`
Color string `json:"color"`
IconUrl any `json:"icon_url"`
Text string `json:"text"`
} `json:"badge"`
Bvid string `json:"bvid"`
Cover string `json:"cover"`
Expand All @@ -732,7 +732,7 @@ type DynamicItem struct {
Type int `json:"type"`
} `json:"archive,omitempty"`
} `json:"major"`
Topic interface{} `json:"topic"`
Topic any `json:"topic"`
} `json:"module_dynamic"`
ModuleMore struct {
ThreePointItems []struct {
Expand Down Expand Up @@ -768,7 +768,7 @@ type DynamicItem struct {
} `json:"like_icon"`
RidStr string `json:"rid_str"`
} `json:"basic"`
IdStr interface{} `json:"id_str"`
IdStr any `json:"id_str"`
Modules struct {
ModuleAuthor struct {
Avatar struct {
Expand Down Expand Up @@ -847,13 +847,13 @@ type DynamicItem struct {
Name string `json:"name"`
Type int `json:"type"`
} `json:"decorate,omitempty"`
Face string `json:"face"`
FaceNft bool `json:"face_nft"`
Following interface{} `json:"following"`
JumpUrl string `json:"jump_url"`
Label string `json:"label"`
Mid int `json:"mid"`
Name string `json:"name"`
Face string `json:"face"`
FaceNft bool `json:"face_nft"`
Following any `json:"following"`
JumpUrl string `json:"jump_url"`
Label string `json:"label"`
Mid int `json:"mid"`
Name string `json:"name"`
OfficialVerify struct {
Desc string `json:"desc"`
Type int `json:"type"`
Expand Down Expand Up @@ -896,7 +896,7 @@ type DynamicItem struct {
} `json:"vip"`
} `json:"module_author"`
ModuleDynamic struct {
Additional interface{} `json:"additional"`
Additional any `json:"additional"`
Desc *struct {
RichTextNodes []struct {
JumpUrl string `json:"jump_url,omitempty"`
Expand All @@ -916,10 +916,10 @@ type DynamicItem struct {
Archive struct {
Aid string `json:"aid"`
Badge struct {
BgColor string `json:"bg_color"`
Color string `json:"color"`
IconUrl interface{} `json:"icon_url"`
Text string `json:"text"`
BgColor string `json:"bg_color"`
Color string `json:"color"`
IconUrl any `json:"icon_url"`
Text string `json:"text"`
} `json:"badge"`
Bvid string `json:"bvid"`
Cover string `json:"cover"`
Expand All @@ -938,15 +938,15 @@ type DynamicItem struct {
Draw struct {
Id int `json:"id"`
Items []struct {
Height int `json:"height"`
Size float64 `json:"size"`
Src string `json:"src"`
Tags []interface{} `json:"tags"`
Width int `json:"width"`
Height int `json:"height"`
Size float64 `json:"size"`
Src string `json:"src"`
Tags []any `json:"tags"`
Width int `json:"width"`
} `json:"items"`
} `json:"draw,omitempty"`
} `json:"major"`
Topic interface{} `json:"topic"`
Topic any `json:"topic"`
} `json:"module_dynamic"`
} `json:"modules"`
Type string `json:"type"`
Expand Down
164 changes: 62 additions & 102 deletions fav.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,17 @@ func (c *Client) DeleteFavourResources(mediaId int, resources []Resource, platfo
return err
}

type MediaIdParam struct {
MediaId int `json:"media_id"` // 目标收藏夹id
}

// CleanFavourResources 清空所有失效收藏内容
func (c *Client) CleanFavourResources(mediaId int) error {
biliJct := c.getCookie("bili_jct")
if len(biliJct) == 0 {
return errors.New("B站登录过期")
}
resp, err := c.resty.R().SetQueryParams(map[string]string{
"media_id": strconv.Itoa(mediaId),
"csrf": biliJct,
}).Post("https://api.bilibili.com/x/v3/fav/resource/clean")
if err != nil {
return errors.WithStack(err)
}
_, err = getRespData(resp, "清空所有失效收藏内容")
func (c *Client) CleanFavourResources(param MediaIdParam) error {
const (
method = resty.MethodPost
url = "https://api.bilibili.com/x/v3/fav/resource/clean"
)
_, err := execute[any](c, method, url, param, fillCsrf(c))
return err
}

Expand Down Expand Up @@ -191,19 +188,18 @@ type FavourFolderInfo struct {
}

// GetFavourFolderInfo 获取收藏夹元数据
func (c *Client) GetFavourFolderInfo(mediaId int) (*FavourFolderInfo, error) {
resp, err := c.resty.R().
SetQueryParam("media_id", strconv.Itoa(mediaId)).Get("https://api.bilibili.com/x/v3/fav/folder/info")
if err != nil {
return nil, errors.WithStack(err)
}
data, err := getRespData(resp, "获取收藏夹元数据")
if err != nil {
return nil, errors.WithStack(err)
}
var ret *FavourFolderInfo
err = json.Unmarshal(data, &ret)
return ret, errors.WithStack(err)
func (c *Client) GetFavourFolderInfo(param MediaIdParam) (*FavourFolderInfo, error) {
const (
method = resty.MethodGet
url = "https://api.bilibili.com/x/v3/fav/folder/info"
)
return execute[*FavourFolderInfo](c, method, url, param)
}

type GetAllFavourFolderInfoParam struct {
UpMid int `json:"up_mid"` // 目标用户mid
Type int `json:"type,omitempty" request:"query,omitempty"` // 目标内容属性。默认为全部。0:全部。2:视频稿件
Rid int `json:"rid,omitempty" request:"query,omitempty"` // 目标内容id。视频稿件:视频稿件avid
}

type AllFavourFolderInfo struct {
Expand All @@ -220,26 +216,12 @@ type AllFavourFolderInfo struct {
}

// GetAllFavourFolderInfo 获取指定用户创建的所有收藏夹信息

func (c *Client) GetAllFavourFolderInfo(upMid, attrType, rid int) (*AllFavourFolderInfo, error) {
r := c.resty.R().SetQueryParams(map[string]string{
"up_mid": strconv.Itoa(upMid),
"type": strconv.Itoa(attrType),
})
if rid != 0 {
r = r.SetQueryParam("rid", strconv.Itoa(rid))
}
resp, err := r.Get("https://api.bilibili.com/x/v3/fav/folder/created/list-all")
if err != nil {
return nil, errors.WithStack(err)
}
data, err := getRespData(resp, "获取指定用户创建的所有收藏夹信息")
if err != nil {
return nil, errors.WithStack(err)
}
var ret *AllFavourFolderInfo
err = json.Unmarshal(data, &ret)
return ret, errors.WithStack(err)
func (c *Client) GetAllFavourFolderInfo(param GetAllFavourFolderInfoParam) (*AllFavourFolderInfo, error) {
const (
method = resty.MethodGet
url = "https://api.bilibili.com/x/v3/fav/folder/created/list-all"
)
return execute[*AllFavourFolderInfo](c, method, url, param)
}

type FavourInfo struct {
Expand All @@ -261,17 +243,16 @@ type FavourInfo struct {
Play int `json:"play"`
Danmaku int `json:"danmaku"`
} `json:"cnt_info"`
Link string `json:"link"`
Ctime int `json:"ctime"`
Pubtime int `json:"pubtime"`
FavTime int `json:"fav_time"`
BvId string `json:"bv_id"`
Bvid string `json:"bvid"`
Season interface{} `json:"season"`
Link string `json:"link"`
Ctime int `json:"ctime"`
Pubtime int `json:"pubtime"`
FavTime int `json:"fav_time"`
BvId string `json:"bv_id"`
Bvid string `json:"bvid"`
Season any `json:"season"`
}

// GetFavourInfo 获取收藏内容

func (c *Client) GetFavourInfo(resources []Resource, platform string) ([]*FavourInfo, error) {
resourcesStr := make([]string, 0, len(resources))
for _, resource := range resources {
Expand All @@ -294,6 +275,17 @@ func (c *Client) GetFavourInfo(resources []Resource, platform string) ([]*Favour
return ret, errors.WithStack(err)
}

type GetFavourListParam struct {
MediaId int `json:"media_id"` // 目标收藏夹mlid(完整id)
Tid int `json:"tid,omitempty" request:"query,omitempty"` // 分区tid。默认为全部分区。0:全部分区
Keyword string `json:"keyword,omitempty" request:"query,omitempty"` // 搜索关键字
Order string `json:"order,omitempty" request:"query,omitempty"` // 排序方式。按收藏时间:mtime。按播放量: view。按投稿时间:pubtime
Type int `json:"type,omitempty" request:"query,omitempty"` // 查询范围。0:当前收藏夹(对应media_id)。 1:全部收藏夹
Ps int `json:"ps"` // 每页数量。定义域:1-20
Pn int `json:"pn,omitempty" request:"query,omitempty"` // 页码。默认为1
Platform string `json:"platform,omitempty" request:"query,omitempty"` // 平台标识。可为web(影响内容列表类型)
}

type FavourList struct {
Info struct { // 收藏夹元数据
Id int `json:"id"` // 收藏夹mlid(完整id),收藏夹原始id+创建者mid尾号2位
Expand Down Expand Up @@ -356,38 +348,17 @@ type FavourList struct {
}

// GetFavourList 获取收藏夹内容明细列表
func (c *Client) GetFavourList(param GetFavourListParam) (*FavourList, error) {
const (
method = resty.MethodGet
url = "https://api.bilibili.com/x/v3/fav/resource/list"
)
return execute[*FavourList](c, method, url, param)
}

func (c *Client) GetFavourList(mediaId, tid int, keyword, order string, searchType, ps, pn int, platform string) (*FavourList, error) {
if pn == 0 {
pn = 1
}
r := c.resty.R().SetQueryParams(map[string]string{
"media_id": strconv.Itoa(mediaId),
"tid": strconv.Itoa(tid),
"type": strconv.Itoa(searchType),
"ps": strconv.Itoa(ps),
"pn": strconv.Itoa(pn),
})
if len(keyword) > 0 {
r = r.SetQueryParam("keyword", keyword)
}
if len(order) > 0 {
r = r.SetQueryParam("order", order)
}
if len(platform) > 0 {
r = r.SetQueryParam("platform", platform)
}
resp, err := r.Get("https://api.bilibili.com/x/v3/fav/resource/list")
if err != nil {
return nil, errors.WithStack(err)
}
data, err := getRespData(resp, "获取收藏夹内容明细列表")
if err != nil {
return nil, errors.WithStack(err)
}
var ret *FavourList
err = json.Unmarshal(data, &ret)
return ret, errors.WithStack(err)
type GetFavourIdsParam struct {
MediaId int `json:"media_id"` // 目标收藏夹mlid(完整id)
Platform string `json:"platform,omitempty" request:"query,omitempty"` // 平台标识。可为web(影响内容列表类型)
}

type FavourId struct {
Expand All @@ -398,21 +369,10 @@ type FavourId struct {
}

// GetFavourIds 获取收藏夹全部内容id

func (c *Client) GetFavourIds(mediaId int, platform string) ([]*FavourId, error) {
r := c.resty.R().SetQueryParam("media_id", strconv.Itoa(mediaId))
if len(platform) > 0 {
r = r.SetQueryParam("platform", platform)
}
resp, err := r.Get("https://api.bilibili.com/x/v3/fav/resource/ids")
if err != nil {
return nil, errors.WithStack(err)
}
data, err := getRespData(resp, "获取收藏夹全部内容id")
if err != nil {
return nil, errors.WithStack(err)
}
var ret []*FavourId
err = json.Unmarshal(data, &ret)
return ret, errors.WithStack(err)
func (c *Client) GetFavourIds(param GetFavourIdsParam) ([]FavourId, error) {
const (
method = resty.MethodGet
url = "https://api.bilibili.com/x/v3/fav/resource/ids"
)
return execute[[]FavourId](c, method, url, param)
}
Loading

0 comments on commit a3dc96f

Please sign in to comment.