Skip to content

Commit

Permalink
[perf] 被删除的视频也会同步删除
Browse files Browse the repository at this point in the history
  • Loading branch information
runs committed Jan 26, 2023
1 parent 042a2e3 commit e223bb0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion internal/app/api/idl/bilbil_video.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package idl

import (
"git.vtb.link/eoefans/internal/app/api/util/query_parser"
"time"

"git.vtb.link/eoefans/internal/app/api/util/query_parser"
)

type BilibiliVideoSearchReq struct {
Expand All @@ -20,6 +21,7 @@ type BilibiliVideoSearchResp struct {
}

const (
BilibiliVideoFailureStatus = 2
BilibiliVideoEnabledStatus = 1
BilibiliVideoDisabledStatus = 0
)
Expand Down Expand Up @@ -64,6 +66,7 @@ const (
type BilibiliVideoRepository interface {
Save(e *BilibiliVideo) error
Shield(bvid string) error
Failure(bvid string) error

FindAllByBvidList(bvidList []string) (list []*BilibiliVideo, err error)
FindAllByPubDate(from time.Time, to time.Time, page, size int64) (list []*BilibiliVideo, total int64, err error)
Expand Down
17 changes: 12 additions & 5 deletions internal/app/spider/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ func (u *Update) spider() error {
if err != nil {
if bErr, ok := err.(*bilibili.Error); ok {
//bErr.Code == 62022 // 视频被删除
u.logger.Warn("VideoWebInfo error", zap.String("bvid", video.Bvid), zap.Int("code", bErr.Code), zap.String("message", bErr.Message))
} else {
u.logger.Error("VideoWebInfo error", zap.String("bvid", video.Bvid), zap.Error(err))
if bErr.Code == 62022 {
if err := repository.NewBilibiliVideo(tx).Failure(vInfo.Bvid); err != nil {
u.logger.Error("update error", zap.String("bvid", video.Bvid), zap.Error(err))
}
} else {
u.logger.Warn("VideoWebInfo error", zap.String("bvid", video.Bvid), zap.Int("code", bErr.Code), zap.String("message", bErr.Message))
}
continue
}

u.logger.Error("VideoWebInfo error", zap.String("bvid", video.Bvid), zap.Error(err))
continue
}

Expand All @@ -118,8 +125,8 @@ func (u *Update) spider() error {
continue
}

if err := insertDB(tx, vInfo, strings.Join(tags, ",")); err != nil {
u.logger.Error("insertDB error", zap.String("bvid", video.Bvid), zap.Error(err))
if err := InsertDB(tx, vInfo, strings.Join(tags, ",")); err != nil {
u.logger.Error("InsertDB error", zap.String("bvid", video.Bvid), zap.Error(err))
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/app/spider/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func (v *Video) spider() error {
continue
}

if err := insertDB(v.db.WithContext(context.TODO()), info, sInfo.Tag); err != nil {
v.logger.Error("insertDB error", zap.String("bvid", sInfo.Bvid), zap.Error(err))
if err := InsertDB(v.db.WithContext(context.TODO()), info, sInfo.Tag); err != nil {
v.logger.Error("InsertDB error", zap.String("bvid", sInfo.Bvid), zap.Error(err))
_, _ = failBvFile.WriteString(sInfo.Bvid + "\n")
}

Expand All @@ -147,7 +147,7 @@ func (v *Video) spider() error {
return nil
}

func insertDB(tx *gorm.DB, info *bilibili.VideoInfoResponse, strTag string) error {
func InsertDB(tx *gorm.DB, info *bilibili.VideoInfoResponse, strTag string) error {
e := &idl.BilibiliVideo{
Bvid: info.Bvid,
Aid: uint64(info.Aid),
Expand Down
12 changes: 12 additions & 0 deletions internal/repository/bilbil_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ func (impl *BilibiliVideoMysqlImpl) Shield(bvid string) error {
return nil
}

func (impl *BilibiliVideoMysqlImpl) Failure(bvid string) error {
result := impl.tx.Table(bilibiliVideoTableName).
Where("bvid = ? AND status = ?", bvid, idl.BilibiliVideoEnabledStatus).
UpdateColumn("status", idl.BilibiliVideoFailureStatus)

if result.Error != nil {
return errors.Wrap(result.Error, fmt.Sprintf("update %s fail", bilibiliVideoTableName))
}

return nil
}

func (impl *BilibiliVideoMysqlImpl) Save(e *idl.BilibiliVideo) error {
return impl.tx.Transaction(func(_tx *gorm.DB) error {
result := _tx.Table(bilibiliVideoTableName).Where("bvid = ?", e.Bvid).Select("id").Find(&e.Id)
Expand Down

0 comments on commit e223bb0

Please sign in to comment.