-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
434 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "git.vtb.link/eoefans/internal/app/api/apperrors" | ||
| "git.vtb.link/eoefans/internal/app/api/help" | ||
| "git.vtb.link/eoefans/internal/app/api/idl" | ||
| "git.vtb.link/eoefans/internal/app/api/service" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func BilibiliLatestPics(s *service.BilbilPicture) func(ctx *gin.Context) { | ||
| return func(ctx *gin.Context) { | ||
| var req idl.BilibiliPictureLatestReq | ||
| if err := ctx.ShouldBindQuery(&req); err != nil { | ||
| _ = ctx.Error(apperrors.NewValidationError(400, err.Error()).Wrap(err)) | ||
| return | ||
| } | ||
|
|
||
| if resp, err := s.Latest(ctx, req); err != nil { | ||
| _ = ctx.Error(err) | ||
| return | ||
| } else { | ||
| ctx.JSON(http.StatusOK, help.SuccessJson(resp)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BilibiliRecommendPics(s *service.BilbilPicture) func(ctx *gin.Context) { | ||
| return func(ctx *gin.Context) { | ||
| var req idl.BilibiliPictureRecommendReq | ||
| if resp, err := s.Recommend(ctx,req); err != nil { | ||
| _ = ctx.Error(err) | ||
| return | ||
| } else { | ||
| ctx.JSON(http.StatusOK, help.SuccessJson(resp)) | ||
| } | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,92 @@ | ||
| package idl | ||
|
|
||
| type BilibiliPicture struct { | ||
| ID uint64 `gorm:"primarykey"` | ||
| Url string `gorm:"column:url"` | ||
| DynamicID uint64 `gorm:"column:dynamic_id"` | ||
| TopicName string `gorm:"column:topic_name"` | ||
| SentAt uint64 `gorm:"column:sent_at"` | ||
| CreatedAt uint64 `gorm:"autoCreateTime"` | ||
| UpdatedAt uint64 `gorm:"autoUpdateTime"` | ||
| import ( | ||
| "database/sql/driver" | ||
| "encoding/json" | ||
| "errors" | ||
| "time" | ||
| ) | ||
|
|
||
| // 图片来源于动态,以动态为单位 | ||
| type BilibiliDynamic struct { | ||
| ID uint64 `gorm:"primarykey"` | ||
| UID uint64 `gorm:"column:uid"` | ||
| DynamicID uint64 `gorm:"column:dynamic_id"` | ||
| Pictures BilibiliDynamicPictures `gorm:"column:pictures;"` | ||
| TopicName string `gorm:"column:topic_name"` | ||
| TopicID uint64 `gorm:"column:topic_id"` | ||
| View uint64 `gorm:"column:view_nums"` | ||
| Repost uint64 `gorm:"column:repost"` | ||
| Comment uint64 `gorm:"column:comment_nums"` | ||
| Like uint64 `gorm:"column:favor"` | ||
| SentAt uint64 `gorm:"column:sent_at"` | ||
| CreatedAt uint64 `gorm:"autoCreateTime"` | ||
| UpdatedAt uint64 `gorm:"autoUpdateTime"` | ||
| } | ||
|
|
||
| func (p BilibiliDynamicPictures) Value() (driver.Value, error) { | ||
| return json.Marshal(p) | ||
| } | ||
|
|
||
| func (c *BilibiliDynamicPictures) Scan(input interface{}) error { | ||
| data, ok := input.([]byte) | ||
| if !ok { | ||
| return errors.New("invalid input in Scan") | ||
| } | ||
| result := BilibiliDynamicPictures{} | ||
| err := json.Unmarshal(data, &result) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| *c = result | ||
| return nil | ||
| } | ||
|
|
||
| type BilibiliPictureLatestReq struct { | ||
| Page int `form:"page,default=1" binding:"omitempty,gt=0"` | ||
| TopicID int `form:"topic_id"` | ||
| } | ||
|
|
||
| type BilibiliPictureRecommendReq struct { | ||
| TopicID int `form:"topic_id"` | ||
| } | ||
| type BilibiliPicturesCommonResp struct { | ||
| Result []*BilibiliDynamicDTO `json:"result"` | ||
| } | ||
| type BilibiliPicturesLatestResp struct { | ||
| BilibiliPicturesCommonResp | ||
| Page int `json:"page"` | ||
| Total int `json:"total"` | ||
| } | ||
|
|
||
| type BilibiliPicturesRecommendResp struct { | ||
| BilibiliPicturesCommonResp | ||
| Total int `json:"total"` | ||
| } | ||
| type BilibiliDynamicDTO struct { | ||
| DynamicID uint64 `json:"dynamic_id"` | ||
| Pictures BilibiliDynamicPictures `json:"pictures"` | ||
| SentAt uint64 `json:"sent_at"` | ||
| } | ||
|
|
||
| type BilibiliPictureDTO struct { | ||
| ID uint64 `json:"id"` | ||
| Url string `json:"url"` | ||
| CreatedAt uint64 `json:"created_at"` | ||
| type BilibiliDynamicPictures []BilibiliDynamicPicture | ||
| type BilibiliDynamicPicture struct { | ||
| Height float64 `json:"img_height"` | ||
| Size float64 `json:"img_size"` | ||
| Width float64 `json:"img_width"` | ||
| ImgSrc string `json:"image_src"` | ||
| } | ||
|
|
||
| func (BilibiliPicture) TableName() string { | ||
| return "bilibili_pictures" | ||
| func (BilibiliDynamic) TableName() string { | ||
| return "bilibili_dynamics" | ||
| } | ||
|
|
||
| type BilibiliPictureRepository interface { | ||
| Create(items []*BilibiliPicture) error | ||
| Create(items []*BilibiliDynamic) error | ||
| FindMaxDynamicID(topicName string) (*uint64, error) | ||
| Update(updates map[string]interface{}, dynamicID uint64) error | ||
| FindAllByPubDate(from, to time.Time, page, size int64) (list []*BilibiliDynamic, err error) | ||
| Latest(page, size, topicID int) (list []*BilibiliDynamic, err error) | ||
| //推荐暂时先默认50个 | ||
| Recommend(from, to time.Time, size, topicID int) (list []*BilibiliDynamic, err error) | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "git.vtb.link/eoefans/internal/app/api/idl" | ||
| "git.vtb.link/eoefans/internal/repository" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| const ( | ||
| picRecommendDefaultSize = 50 | ||
| ) | ||
|
|
||
| type BilbilPicture struct { | ||
| db *gorm.DB | ||
| } | ||
|
|
||
| func NewBilibiliPicture(db *gorm.DB) *BilbilPicture { | ||
| return &BilbilPicture{db: db} | ||
| } | ||
|
|
||
| func (service *BilbilPicture) Latest(ctx context.Context, req idl.BilibiliPictureLatestReq) (*idl.BilibiliPicturesLatestResp, error) { | ||
| tx := service.db.WithContext(ctx) | ||
| picRepository := repository.NewBilibiliPicture(tx) | ||
| list, err := picRepository.Latest(req.Page, defaultQuerySize, req.TopicID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| resp := idl.BilibiliPicturesLatestResp{ | ||
| Page: req.Page, | ||
| Total: len(list), | ||
| } | ||
| for i := range list { | ||
| resp.Result = append(resp.Result, &idl.BilibiliDynamicDTO{ | ||
| DynamicID: list[i].DynamicID, | ||
| Pictures: list[i].Pictures, | ||
| SentAt: list[i].SentAt, | ||
| }) | ||
| } | ||
| return &resp, nil | ||
| } | ||
|
|
||
| func (service *BilbilPicture) Recommend(ctx context.Context, req idl.BilibiliPictureRecommendReq) (*idl.BilibiliPicturesRecommendResp, error) { | ||
| tx := service.db.WithContext(ctx) | ||
| picRepository := repository.NewBilibiliPicture(tx) | ||
| now := time.Now() | ||
| list, err := picRepository.Recommend(now.Add(-(3 * 24 * time.Hour)), now, picRecommendDefaultSize, req.TopicID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| resp := idl.BilibiliPicturesRecommendResp{ | ||
| Total: len(list), | ||
| } | ||
| for i := range list { | ||
| resp.Result = append(resp.Result, &idl.BilibiliDynamicDTO{ | ||
| DynamicID: list[i].DynamicID, | ||
| Pictures: list[i].Pictures, | ||
| SentAt: list[i].SentAt, | ||
| }) | ||
| } | ||
| return &resp, nil | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.