-
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.
Merge in EOEFANS/eoefans-api from feature/dynamic to master * commit 'dcc2ee21951d8a81241bd6573b55b1c82945e9a4': [fix] 修复更新时错误的列名 [feature] 调整图片推荐接口 [feature] 动态图片爬虫与api [feature] 新增picture到启动项 [feat] 爬取图文动态的图片 [feat] 增加话题动态接口
- Loading branch information
Showing
13 changed files
with
813 additions
and
3 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,43 @@ | ||
| 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 err := ctx.ShouldBindQuery(&req); err != nil { | ||
| _ = ctx.Error(apperrors.NewValidationError(400, err.Error()).Wrap(err)) | ||
| return | ||
| } | ||
| 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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package idl | ||
|
|
||
| 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 { | ||
| Page int `form:"page,default=1" binding:"omitempty,gt=0"` | ||
| 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 | ||
| Page int `json:"page"` | ||
| Total int `json:"total"` | ||
| } | ||
| type BilibiliDynamicDTO struct { | ||
| DynamicID uint64 `json:"dynamic_id"` | ||
| Pictures BilibiliDynamicPictures `json:"pictures"` | ||
| SentAt uint64 `json:"sent_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:"img_src"` | ||
| } | ||
|
|
||
| func (BilibiliDynamic) TableName() string { | ||
| return "bilibili_dynamics" | ||
| } | ||
|
|
||
| type BilibiliPictureRepository interface { | ||
| 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) | ||
| Recommend(from, to time.Time, page, 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,65 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "git.vtb.link/eoefans/internal/app/api/idl" | ||
| "git.vtb.link/eoefans/internal/repository" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| const ( | ||
| picRecommendDefaultSize = 20 | ||
| ) | ||
|
|
||
| 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, req.Page, picRecommendDefaultSize, req.TopicID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| resp := idl.BilibiliPicturesRecommendResp{ | ||
| Total: len(list), | ||
| Page: req.Page, | ||
| } | ||
| 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.