Skip to content

[feature] bilibili video status check #51

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/app/api/handler/bilbil_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,31 @@ func BilibiliVideoSearch(s *service.BilbilVideo) func(ctx *gin.Context) {
}
}
}

func CheckVideo(s *service.BilbilVideo) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
var req idl.CheckBilibiliVideoReq
if err := ctx.ShouldBindJSON(&req); err != nil {
_ = ctx.Error(apperrors.NewValidationError(400, err.Error()).Wrap(err))
return
}

if resp, err := s.CheckVideo(ctx, req); err != nil {
_ = ctx.Error(err)
return
} else {
ctx.JSON(http.StatusOK, help.SuccessJson(resp))
}
}
}

func GetAnalysis(s *service.BilbilVideo) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
if resp, err := s.GetAnalysis(ctx); err != nil {
_ = ctx.Error(err)
return
} else {
ctx.JSON(http.StatusOK, help.SuccessJson(resp))
}
}
}
9 changes: 9 additions & 0 deletions internal/app/api/idl/bilbil_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import (
"vlink.dev/eoefans/internal/app/api/util/query_parser"
)

type CheckBilibiliVideoReq struct {
BvidList []string `form:"bvid_list" json:"bvid_list" binding:"required,dive,required"`
}

type CheckBilibiliVideoResp struct {
StatusMap map[string]uint8 `json:"status_map"`
}

type BilibiliVideoSearchReq struct {
Order BilibiliVideoOrder `form:"order" binding:"required,oneof=pubdate view score"`
Page int64 `form:"page,default=1" binding:"omitempty,gt=0"`
Expand Down Expand Up @@ -82,6 +90,7 @@ type BilibiliVideoRepository interface {
Failure(bvid string) error

FindAllByBvidList(bvidList []string) (list []*BilibiliVideo, err error)
FindStatusMapByBvidList(bvidList []string) (map[string]uint8, error)
FindAllByPubDate(from time.Time, to time.Time, page, size int64) (list []*BilibiliVideo, total int64, err error)

Search(queryItems []query_parser.QueryItem, order BilibiliVideoOrder, page, size int64) (list []*BilibiliVideo, total int64, err error)
Expand Down
2 changes: 2 additions & 0 deletions internal/app/api/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"vlink.dev/eoefans/internal/app/api/router"
"vlink.dev/eoefans/internal/app/api/service"
"vlink.dev/eoefans/internal/app/api/util/password"
"vlink.dev/eoefans/internal/app/spider/video_analysis"
"vlink.dev/eoefans/internal/pkg/cache"
"vlink.dev/eoefans/internal/pkg/iversion"
"vlink.dev/eoefans/internal/pkg/log"
Expand All @@ -19,6 +20,7 @@ func Provide() fx.Option {
ServiceProvider(),

smsclient.Provide(),
video_analysis.Provide(),
fx.Provide(cache.NewGoCache),
fx.Provide(smsclient.NewRandomNumberCodeGenerator),
fx.Provide(password.NewDefaultPasswordHandler),
Expand Down
7 changes: 7 additions & 0 deletions internal/app/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func InitRouters(

// 视频搜索
r.GET("/v1/video-interface/advanced-search", handler.BilibiliVideoSearch(bvService))

// check 视频
r.POST("/v1/video-interface/check", handler.CheckVideo(bvService))

// 获取视频黑名单
r.GET("/v1/video-interface/analysis", handler.GetAnalysis(bvService))

//图片
picApi := r.Group("/v1/pic")
{
Expand Down
27 changes: 26 additions & 1 deletion internal/app/api/service/bilbil_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package service
import (
"context"
"fmt"
"github.com/pkg/errors"
"strconv"
"strings"

"vlink.dev/eoefans/internal/app/api/apperrors"
"vlink.dev/eoefans/internal/app/api/idl"
"vlink.dev/eoefans/internal/app/api/util/query_parser"
Expand Down Expand Up @@ -110,3 +110,28 @@ func queryCheck() map[string]func(item query_parser.QueryItem) (bool, string) {
"pubdate": _numericCheck,
}
}

func (b *BilbilVideo) CheckVideo(ctx context.Context, req idl.CheckBilibiliVideoReq) (*idl.CheckBilibiliVideoResp, error) {
tx := b.db.WithContext(ctx)
sMap, err := repository.NewBilibiliVideo(tx).FindStatusMapByBvidList(req.BvidList)
if err != nil {
return nil, err
}

return &idl.CheckBilibiliVideoResp{StatusMap: sMap}, nil
}

func (b *BilbilVideo) GetAnalysis(ctx context.Context) (interface{}, error) {
var list []*struct {
Type string `json:"type"`
Key string `json:"key"`
Score int64 `json:"score"`
}

result := b.db.WithContext(ctx).Table("video_analysis").Find(&list)
if result.Error != nil {
return nil, errors.Wrapf(result.Error, "get analysis failed.")
}

return list, nil
}
19 changes: 19 additions & 0 deletions internal/repository/bilbil_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,22 @@ func (impl *BilibiliVideoMysqlImpl) FindAllByBvidList(bvidList []string) (list [

return list, nil
}

func (impl *BilibiliVideoMysqlImpl) FindStatusMapByBvidList(bvidList []string) (map[string]uint8, error) {
list := make([]*struct {
Bvid string
Status uint8
}, 0, len(bvidList))

result := impl.tx.Table(bilibiliVideoTableName).Where("bvid IN (?)", bvidList).Select("bvid", "status").Find(&list)
if result.Error != nil {
return nil, errors.Wrap(result.Error, fmt.Sprintf("select %s error", bilibiliVideoTableName))
}

m := make(map[string]uint8, len(list))
for _, item := range list {
m[item.Bvid] = item.Status
}

return m, nil
}
19 changes: 19 additions & 0 deletions internal/repository/bilbil_video_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,22 @@ func (impl *BilibiliVideoPostgresImpl) FindAllByBvidList(bvidList []string) (lis

return toBilibiliVideoList(newList), nil
}

func (impl *BilibiliVideoPostgresImpl) FindStatusMapByBvidList(bvidList []string) (map[string]uint8, error) {
list := make([]*struct {
Bvid string
Status uint8
}, 0, len(bvidList))

result := impl.tx.Table(bilibiliVideoTableName).Where("bvid IN (?)", bvidList).Select("bvid", "status").Find(&list)
if result.Error != nil {
return nil, errors.Wrap(result.Error, fmt.Sprintf("select %s error", bilibiliVideoTableName))
}

m := make(map[string]uint8, len(list))
for _, item := range list {
m[item.Bvid] = item.Status
}

return m, nil
}