Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
vclass committed May 12, 2024
2 parents 4539029 + c69cecc commit 61ead65
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 82 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ services:
- 8080:8080
````
## 同步逻辑
- 收藏夹
- 视频稿件失效:已下载的视频稿件文件夹会被打上`[已失效]`标识
- 视频稿件取消收藏:已下载视频保留。未下载视频不下载。
- 删除收藏夹:已同步的整个收藏夹移动到回收站中
- 收藏夹改名:如当前收藏夹视频正在下载,等下载完毕后更改收藏夹名称
- 收藏和订阅
- 视频稿件失效:已下载的视频稿件文件夹会被打上`[已失效]`标识
- 取消订阅:已同步的整个订阅移动到回收站中
- 订阅合集中视频稿件被移除合集:保留已下载视频稿件
- 稍后再看
- 手动删除稍后再看的视频稿件:已下载的视频稿件文件夹移动到回收站中
- 视频稿件失效:已下载的视频稿件文件夹会被打上`[已失效]`标识

## 预览图
![账号列表](./.assets/1.png)
![设置收藏夹同步](./.assets/2.png)
Expand Down
8 changes: 8 additions & 0 deletions bobo/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (c *Client) RefreshWbiKey(nav *Navigation) error {
if len(c.cookies) < 1 {
return errors.New("未登录")
}
if nav == nil {
return errors.New("未获取到导航信息")
}
imgUrl := strings.Split(nav.WbiImg.ImgUrl, "/")
subUrl := strings.Split(nav.WbiImg.SubUrl, "/")
c.imgKey = strings.Split(imgUrl[len(imgUrl)-1], ".")[0]
Expand Down Expand Up @@ -161,3 +164,8 @@ func (c *Client) resty() *resty.Client {
func (c *Client) GetResty() *resty.Client {
return c.resty()
}

func (c *Client) CheckVideo(bvid string) bool {
_, code, _ := c.GetVideoInfoByBvidCode(bvid)
return code == 0
}
2 changes: 1 addition & 1 deletion bobo/client/fav_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type FavourList struct {
Name string `json:"name"` // UP主昵称
Face string `json:"face"` // UP主头像url
} `json:"upper"`
Attr int `json:"attr"` // 属性位(?)
Attr int `json:"attr"` // 属性位(?) 0:正常 9:失效-UP主删除
CntInfo struct { // 状态数
Collect int `json:"collect"` // 收藏数
Play int `json:"play"` // 播放数
Expand Down
25 changes: 21 additions & 4 deletions bobo/client/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,35 @@ func (c *Client) GetVideoInfoByAvid(avid int) (*VideoInfo, error) {

// GetVideoInfoByBvid 通过Bvid获取视频信息
func (c *Client) GetVideoInfoByBvid(bvid string) (*VideoInfo, error) {
// resp, err := c.resty().R().SetHeader("Content-Type", "application/x-www-form-urlencoded").
// SetQueryParam("bvid", bvid).Get("https://api.bilibili.com/x/web-interface/view")
// if err != nil {
// return nil, errors.WithStack(err)
// }
// data, err := getRespData(resp, "获取视频详细信息")
// if err != nil {
// return nil, err
// }
// var ret *VideoInfo
// err = json.Unmarshal(data, &ret)
// return ret, errors.WithStack(err)
ret, _, err := c.GetVideoInfoByBvidCode(bvid)
return ret, err
}

func (c *Client) GetVideoInfoByBvidCode(bvid string) (*VideoInfo, int64, error) {
resp, err := c.resty().R().SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetQueryParam("bvid", bvid).Get("https://api.bilibili.com/x/web-interface/view")
if err != nil {
return nil, errors.WithStack(err)
return nil, 0, errors.WithStack(err)
}
data, err := getRespData(resp, "获取视频详细信息")
data, code, err := getRespDataWithCode(resp, "获取视频详细信息")
if err != nil {
return nil, err
return nil, code, err
}
var ret *VideoInfo
err = json.Unmarshal(data, &ret)
return ret, errors.WithStack(err)
return ret, 0, errors.WithStack(err)
}

// GetVideoInfoByShortUrl 通过短链接获取视频信息
Expand Down
4 changes: 4 additions & 0 deletions bobo/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func downloadHandler(c *client.Client, video *models.Videos, basePath, path stri
services.SetVideoStatus(video.ID, videoStatus)
return
}
if !c.CheckVideo(video.Bvid) {
services.SetVideoStatus(video.ID, videoStatus)
return
}

tmpFilePath := filepath.Join(basePath, ".tmp")
fileName := fmt.Sprintf("%d_%d_%s_%d", mid, video.SourceId, video.Bvid, video.Cid)
Expand Down
14 changes: 13 additions & 1 deletion bobo/refreshCollected.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bilibo/log"
"bilibo/services"
"fmt"
"slices"
)

func (b *BoBo) RefreshCollected(mid int) *client.CollectedInfo {
Expand Down Expand Up @@ -39,13 +40,15 @@ func (b *BoBo) RefreshCollectedVideo(mid int, data *client.CollectedInfo) map[st
logger := log.GetLogger()
logger.Infof("user: %d collected video list", mid)
videosInfoMap := make(map[string]*services.VideoInfo)

if client, err := b.GetClient(mid); err == nil {
if data != nil {
for _, collected := range data.List {
videosMap := make(map[string]*services.Video)
invalidVideosBvidList := make([]string, 0)
if fret, err := client.GetCollectedVideoList(collected.Id); err == nil {
for _, media := range fret.Medias {
if vret, err := client.GetVideoInfoByBvid(media.BvId); err == nil {
if vret, code, err := client.GetVideoInfoByBvidCode(media.BvId); err == nil {
for _, page := range vret.Pages {
videosMapKey := fmt.Sprintf("%d_%s_%d", collected.Id, media.BvId, page.Cid)
videosMap[videosMapKey] = &services.Video{
Expand All @@ -68,9 +71,18 @@ func (b *BoBo) RefreshCollectedVideo(mid int, data *client.CollectedInfo) map[st
Rotate: vret.Dimension.Rotate,
}
}
} else if code == 62002 {
logger.Infof("用户: %d 收藏和订阅: %s 无效视频bvid: %s", mid, collected.Title, media.BvId)
if !slices.Contains(invalidVideosBvidList, media.BvId) {
invalidVideosBvidList = append(invalidVideosBvidList, media.BvId)
}
}
}
}
if len(invalidVideosBvidList) > 0 {
logger.Infof("用户: %d 收藏和订阅: %s 有 %d 个无效视频", mid, collected.Title, len(invalidVideosBvidList))
services.SetInvalidVideos(mid, collected.Id, invalidVideosBvidList, consts.VIDEO_TYPE_COLLECTED)
}
if len(videosMap) > 0 {
services.SetVideos(mid, collected.Id, videosMap, consts.VIDEO_TYPE_COLLECTED)
}
Expand Down
66 changes: 44 additions & 22 deletions bobo/refreshFav.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bilibo/log"
"bilibo/services"
"fmt"
"slices"
)

func (b *BoBo) RefreshFav(mid int) *client.AllFavourFolderInfo {
Expand Down Expand Up @@ -45,35 +46,56 @@ func (b *BoBo) RefreshFavVideo(mid int, data *client.AllFavourFolderInfo) map[st
if data != nil {
for _, fav := range data.List {
videosMap := make(map[string]*services.Video)
invalidVideosBvidList := make([]string, 0)
mlid := fav.Id
if fret, err := client.GetFavourList(mlid, 0, "", "", 0, 20, 1, "web"); err == nil {
for _, media := range fret.Medias {
if vret, err := client.GetVideoInfoByBvid(media.BvId); err == nil {
for _, page := range vret.Pages {
videosMapKey := fmt.Sprintf("%d_%s_%d", mlid, media.BvId, page.Cid)
videosMap[videosMapKey] = &services.Video{
Bvid: media.BvId,
SourceId: mlid,
Mid: mid,
Cid: page.Cid,
Type: consts.VIDEO_TYPE_FAVOUR,
}
pn := 1
for {
if fret, err := client.GetFavourList(mlid, 0, "", "", 0, 20, pn, "web"); err == nil {
for _, media := range fret.Medias {
if vret, code, err := client.GetVideoInfoByBvidCode(media.BvId); err == nil {
for _, page := range vret.Pages {
videosMapKey := fmt.Sprintf("%d_%s_%d", mlid, media.BvId, page.Cid)
videosMap[videosMapKey] = &services.Video{
Bvid: media.BvId,
SourceId: mlid,
Mid: mid,
Cid: page.Cid,
Type: consts.VIDEO_TYPE_FAVOUR,
}

videosInfoMapKey := fmt.Sprintf("%s_%d", media.BvId, page.Cid)
videosInfoMap[videosInfoMapKey] = &services.VideoInfo{
Bvid: media.BvId,
Cid: page.Cid,
Page: page.Page,
Title: vret.Title,
Part: page.Part,
Width: vret.Dimension.Width,
Height: vret.Dimension.Height,
Rotate: vret.Dimension.Rotate,
videosInfoMapKey := fmt.Sprintf("%s_%d", media.BvId, page.Cid)
videosInfoMap[videosInfoMapKey] = &services.VideoInfo{
Bvid: media.BvId,
Cid: page.Cid,
Page: page.Page,
Title: vret.Title,
Part: page.Part,
Width: vret.Dimension.Width,
Height: vret.Dimension.Height,
Rotate: vret.Dimension.Rotate,
}
}
} else if code == 62002 {
logger.Infof("用户: %d 收藏夹: %s 无效视频bvid: %s", mid, fav.Title, media.BvId)
if !slices.Contains(invalidVideosBvidList, media.BvId) {
invalidVideosBvidList = append(invalidVideosBvidList, media.BvId)
}
}
}
logger.Infof("已获取 用户: %d 收藏夹: %s 第 %d 页数据", mid, fav.Title, pn)
if fret.HasMore {
pn++
} else {
break
}
} else {
break
}
}
if len(invalidVideosBvidList) > 0 {
logger.Infof("用户: %d 收藏夹: %s 有 %d 个无效视频", mid, fav.Title, len(invalidVideosBvidList))
services.SetInvalidVideos(mid, mlid, invalidVideosBvidList, consts.VIDEO_TYPE_FAVOUR)
}
if len(videosMap) > 0 {
services.SetVideos(mid, mlid, videosMap, consts.VIDEO_TYPE_FAVOUR)
}
Expand Down
15 changes: 14 additions & 1 deletion bobo/refreshToView.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package bobo

import (
"bilibo/consts"
"bilibo/log"
"bilibo/services"
"fmt"
"slices"
)

func (b *BoBo) RefreshToView(mid int) map[string]*services.VideoInfo {
logger := log.GetLogger()
videosMap := make(map[string]*services.Video)
videosInfoMap := make(map[string]*services.VideoInfo)
invalidVideosBvidList := make([]string, 0)
if client, err := b.GetClient(mid); err == nil {
if toViewData, err := client.GetToView(); err == nil {
for _, data := range toViewData.List {
if vret, err := client.GetVideoInfoByBvid(data.Bvid); err == nil {
if vret, code, err := client.GetVideoInfoByBvidCode(data.Bvid); err == nil {
for _, page := range vret.Pages {
videosMapKey := fmt.Sprintf("%d_%s_%d", 0, data.Bvid, page.Cid)
videosMap[videosMapKey] = &services.Video{
Expand All @@ -34,10 +38,19 @@ func (b *BoBo) RefreshToView(mid int) map[string]*services.VideoInfo {
Rotate: vret.Dimension.Rotate,
}
}
} else if code == 62002 {
logger.Infof("用户: %d 稍后再看 无效视频: %s", mid, data.Bvid)
if !slices.Contains(invalidVideosBvidList, data.Bvid) {
invalidVideosBvidList = append(invalidVideosBvidList, data.Bvid)
}
}
}
}
}
if len(invalidVideosBvidList) > 0 {
logger.Infof("用户: %d 稍后再看 有 %d 个无效视频", mid, len(invalidVideosBvidList))
services.SetInvalidVideos(mid, 0, invalidVideosBvidList, consts.VIDEO_TYPE_WATCH_LATER)
}
if len(videosMap) > 0 {
services.SetVideos(mid, 0, videosMap, consts.VIDEO_TYPE_WATCH_LATER)
}
Expand Down
5 changes: 5 additions & 0 deletions consts/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import "errors"

var ERROR_DOWNLOAD_403 = errors.New("download failed,status code: 403")

const (
VIDEO_ATTR_NORMAL = 0
VIDEO_ATTR_INVALID = 9
)

const (
WATCH_LATER_NOT_SYNC = 0
WATCH_LATER_NEED_SYNC = 1
Expand Down
Loading

0 comments on commit 61ead65

Please sign in to comment.