Skip to content

Commit

Permalink
feat:图片使用新表的数据
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrie committed Feb 5, 2023
1 parent d05d877 commit 25778e7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
4 changes: 3 additions & 1 deletion internal/app/api/idl/bilibili_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ type BilibiliRandomPicture struct {
Size float64 `json:"img_size"`
Width float64 `json:"img_width"`
ImgSrc string `json:"img_src"`
ImgID uint64 `json:"img_id"`
DynamicID uint64 `json:"dynamic_id"`
Tags []BilibiliRandomPictureTag `json:"tags"`
Verify bool `json:"verify"`
Expand All @@ -179,6 +180,7 @@ type BilibiliDynamicPictures []BilibiliDynamicPicture
type BilibiliDynamicPicture struct {
BilibiliPictureAttr
ImgSrc string `json:"img_src"`
ImgID uint64 `json:"img_id,omitempty"`
}

type BilibiliPictureAttr struct {
Expand Down Expand Up @@ -208,6 +210,6 @@ type BilibiliPictureRepository interface {
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)
Random(rand float64) (list []*BilibiliDynamic, err error)
Random(rand float64) (list []*BilibiliPicture, err error)
FeedBack(feedback DynamicFeedback, dynamicID uint64) error
}
82 changes: 48 additions & 34 deletions internal/app/api/service/bilbil_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,25 @@ func (c *RandomPicsCache) flush() {
pictures := make(idl.BilibiliRandomPictures, 0)
for i := range list {
tags := make([]idl.BilibiliRandomPictureTag, 0)
if list[i].TopicDetails != nil {
for _, v := range *list[i].TopicDetails {
if list[i].Dynamic != nil && list[i].Dynamic.TopicDetails != nil {
for _, v := range *list[i].Dynamic.TopicDetails {
tags = append(tags, idl.BilibiliRandomPictureTag{
TagID: v.TopicID,
TagName: v.TopicName,
})
}
}
for j := range list[i].Pictures {
pictures = append(pictures,
idl.BilibiliRandomPicture{
Height: list[i].Pictures[j].Height,
Size: list[i].Pictures[j].Size,
Width: list[i].Pictures[j].Width,
ImgSrc: list[i].Pictures[j].ImgSrc,
DynamicID: list[i].DynamicID,
Tags: tags,
Verify: list[i].Verify,
})
}
pictures = append(pictures,
idl.BilibiliRandomPicture{
ImgID: list[i].ID,
Height: list[i].ImgAttr.Height,
Size: list[i].ImgAttr.Size,
Width: list[i].ImgAttr.Width,
ImgSrc: list[i].ImgSrc,
DynamicID: list[i].DynamicID,
Tags: tags,
Verify: list[i].Verify,
})
}
c.syncMap.Store(overall, pictures)
c.success = true
Expand All @@ -125,12 +124,20 @@ func (service *BilbilPicture) Latest(ctx context.Context, req idl.BilibiliPictur
Total: len(list),
}
for i := range list {
resp.Result = append(resp.Result, &idl.BilibiliDynamicDTO{
dto := &idl.BilibiliDynamicDTO{
DynamicID: list[i].DynamicID,
Pictures: list[i].Pictures,
SentAt: list[i].SentAt,
DynamicIDStr: fmt.Sprintf("%d", list[i].DynamicID),
})
}
for j := range list[i].Pics {
pic := idl.BilibiliDynamicPicture{
ImgSrc: list[i].Pics[j].ImgSrc,
ImgID: list[i].Pics[j].ID,
}
pic.BilibiliPictureAttr = list[i].Pics[j].ImgAttr
dto.Pictures = append(dto.Pictures, pic)
}
resp.Result = append(resp.Result, dto)
}
return &resp, nil
}
Expand All @@ -148,12 +155,20 @@ func (service *BilbilPicture) Recommend(ctx context.Context, req idl.BilibiliPic
Page: req.Page,
}
for i := range list {
resp.Result = append(resp.Result, &idl.BilibiliDynamicDTO{
dto := &idl.BilibiliDynamicDTO{
DynamicID: list[i].DynamicID,
Pictures: list[i].Pictures,
SentAt: list[i].SentAt,
DynamicIDStr: fmt.Sprintf("%d", list[i].DynamicID),
})
}
for j := range list[i].Pics {
pic := idl.BilibiliDynamicPicture{
ImgSrc: list[i].Pics[j].ImgSrc,
ImgID: list[i].Pics[j].ID,
}
pic.BilibiliPictureAttr = list[i].Pics[j].ImgAttr
dto.Pictures = append(dto.Pictures, pic)
}
resp.Result = append(resp.Result, dto)
}
return &resp, nil
}
Expand Down Expand Up @@ -187,26 +202,25 @@ func (service *BilbilPicture) Random(ctx context.Context) (*idl.BilibiliPictureR
pictures := make(idl.BilibiliRandomPictures, 0)
for i := range list {
tags := make([]idl.BilibiliRandomPictureTag, 0)
if list[i].TopicDetails != nil {
for _, v := range *list[i].TopicDetails {
if list[i].Dynamic != nil {
for _, v := range *list[i].Dynamic.TopicDetails {
tags = append(tags, idl.BilibiliRandomPictureTag{
TagID: v.TopicID,
TagName: v.TopicName,
})
}
}
for j := range list[i].Pictures {
pictures = append(pictures,
idl.BilibiliRandomPicture{
Height: list[i].Pictures[j].Height,
Size: list[i].Pictures[j].Size,
Width: list[i].Pictures[j].Width,
ImgSrc: list[i].Pictures[j].ImgSrc,
DynamicID: list[i].DynamicID,
Tags: tags,
Verify: list[i].Verify,
})
}
pictures = append(pictures,
idl.BilibiliRandomPicture{
ImgID: list[i].ID,
Height: list[i].ImgAttr.Height,
Size: list[i].ImgAttr.Size,
Width: list[i].ImgAttr.Width,
ImgSrc: list[i].ImgSrc,
DynamicID: list[i].DynamicID,
Tags: tags,
Verify: list[i].Verify,
})
}
idx := rand.Intn(len(pictures))
resp := &idl.BilibiliPictureRandomResp{}
Expand Down
22 changes: 12 additions & 10 deletions internal/repository/bilibili_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ func (impl *BilibiliPictureMysqlImpl) FindAllByPubDate(from, to time.Time, page,
func (impl *BilibiliPictureMysqlImpl) Latest(page, size, topicID int) (list []*idl.BilibiliDynamic, err error) {
conn := impl.tx.Table(idl.BilibiliDynamic{}.TableName())
if topicID != 0 {
conn = conn.Where("topic_id = ? or cp_topic_id = ?", topicID, topicID)
conn = conn.Where("(topic_id = ? or cp_topic_id = ?)", topicID, topicID)
}
offset := (page - 1) * size
if offset < 0 {
offset = -1
}
err = conn.Select("dynamic_id,pictures,sent_at").
err = conn.Where("pictures_num <> 0").Select("dynamic_id,sent_at").
Order("sent_at DESC").
Offset(offset).
Limit(size).Find(&list).Error
Limit(size).Preload("Pics").Find(&list).Error
if err != nil {
return nil, err
}
Expand All @@ -118,25 +118,25 @@ func (impl *BilibiliPictureMysqlImpl) Latest(page, size, topicID int) (list []*i
func (impl *BilibiliPictureMysqlImpl) Recommend(from, to time.Time, page, size, topicID int) (list []*idl.BilibiliDynamic, err error) {
conn := impl.tx.Table(idl.BilibiliDynamic{}.TableName())
if topicID != 0 {
conn = conn.Where("topic_id = ? or cp_topic_id = ?", topicID, topicID)
conn = conn.Where("(topic_id = ? or cp_topic_id = ?)", topicID, topicID)
}
offset := (page - 1) * size
if offset < 0 {
offset = -1
}
err = conn.Select("dynamic_id,pictures,sent_at").
Where("sent_at >= ? AND sent_at <= ?", from.Unix(), to.Unix()).
err = conn.Select("dynamic_id,sent_at").
Where("sent_at >= ? AND sent_at <= ? AND pictures_num <> 0", from.Unix(), to.Unix()).
Order("favor DESC").
Offset(offset).
Limit(size).Find(&list).Error
Limit(size).Preload("Pics").Find(&list).Error
if err != nil {
return nil, err
}
return list, nil
}

func (impl *BilibiliPictureMysqlImpl) Random(rand float64) (list []*idl.BilibiliDynamic, err error) {
conn := impl.tx.Table(idl.BilibiliDynamic{}.TableName())
func (impl *BilibiliPictureMysqlImpl) Random(rand float64) (list []*idl.BilibiliPicture, err error) {
conn := impl.tx.Table(idl.BilibiliPicture{}.TableName())
type Pair struct {
MaxId float64
MinId float64
Expand All @@ -147,7 +147,9 @@ func (impl *BilibiliPictureMysqlImpl) Random(rand float64) (list []*idl.Bilibili
return nil, err
}
offset := math.Floor((pair.MaxId - pair.MinId) * rand)
err = conn.Where("id >= ?", uint64(pair.MinId+offset)).Select("dynamic_id,pictures,topic_details,verify").Limit(5).Find(&list).Error
err = conn.Where("id >= ?", uint64(pair.MinId+offset)).
Select("id,dynamic_id,img_src,img_attr,verify").
Preload("Dynamic").Limit(5).Find(&list).Error
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 25778e7

Please sign in to comment.