Skip to content

Commit

Permalink
[feature] 随机图片接口增加动态链接
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrie committed Jan 27, 2023
1 parent 3fd19c2 commit 2c6809f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
10 changes: 6 additions & 4 deletions internal/app/api/idl/bilibili_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type BilibiliPicturesRecommendResp struct {

type BilibiliPictureRandomResp struct {
BilibiliDynamicPicture
DynamicURL string `json:"dynamic_url"`
}
type BilibiliDynamicDTO struct {
DynamicID uint64 `json:"dynamic_id"`
Expand All @@ -99,10 +100,11 @@ type BilibiliDynamicDTO struct {

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"`
Height float64 `json:"img_height"`
Size float64 `json:"img_size"`
Width float64 `json:"img_width"`
ImgSrc string `json:"img_src"`
DynamicID uint64 `json:"dynamic_id"`
}

type BilibiliDynamicTopicDetails []BilibiliDynamicTopicDetail
Expand Down
9 changes: 5 additions & 4 deletions internal/app/api/middlewares/cors.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package middlewares

import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/gin-gonic/gin"
)

func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin")
origin := c.Request.Header.Get("Origin")
if origin != "" {
c.Header("Access-Control-Allow-Origin", "*") // 可将将 * 替换为指定的域名
c.Header("Access-Control-Allow-Origin", "*") // 可将将 * 替换为指定的域名
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
Expand All @@ -20,4 +21,4 @@ func Cors() gin.HandlerFunc {
}
c.Next()
}
}
}
14 changes: 13 additions & 1 deletion internal/app/api/service/bilbil_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
"time"
Expand All @@ -21,6 +22,8 @@ type RandomPicKey string

const (
overall RandomPicKey = "overall"

DynamicURL = "https://t.bilibili.com/%d"
)

var (
Expand Down Expand Up @@ -77,7 +80,14 @@ func (c *RandomPicsCache) flush() {
pictures := make(idl.BilibiliDynamicPictures, 0)
for i := range list {
for j := range list[i].Pictures {
pictures = append(pictures, list[i].Pictures[j])
pictures = append(pictures,
idl.BilibiliDynamicPicture{
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,
})
}
}
c.syncMap.Store(overall, pictures)
Expand Down Expand Up @@ -144,6 +154,7 @@ func (service *BilbilPicture) Random(ctx context.Context) (*idl.BilibiliPictureR
idx := rand.Intn(len(pictures))
resp := &idl.BilibiliPictureRandomResp{}
resp.BilibiliDynamicPicture = pictures[idx]
resp.DynamicURL = fmt.Sprintf(DynamicURL, pictures[idx].DynamicID)
return resp, nil
}
}
Expand All @@ -168,5 +179,6 @@ func (service *BilbilPicture) Random(ctx context.Context) (*idl.BilibiliPictureR
idx := rand.Intn(len(pictures))
resp := &idl.BilibiliPictureRandomResp{}
resp.BilibiliDynamicPicture = pictures[idx]
resp.DynamicURL = fmt.Sprintf(DynamicURL, pictures[idx].DynamicID)
return resp, nil
}
1 change: 0 additions & 1 deletion internal/pkg/bilibili/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ func (sdk *SDK) TopicDynamics(topicName string, offsetDynamicId uint64) (data *D

func (sdk *SDK) Dynamic(dynamicId uint64) (data *Dynamic, err error) {
url := fmt.Sprintf(dynamic, dynamicId)
fmt.Println(url)
if err = sdk.fastGet(url, &data); err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/httpserver/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func NewRouter(c *Config, logger *zap.Logger, init InitRouters) *gin.Engine {
r.Use(gin.Recovery())
r.Use(ginZap.Ginzap(logger, time.RFC3339, true))
r.Use(ginZap.RecoveryWithZap(logger, true))

if c.MaxMultipartMemory != 0 {
// 最大上传文件大小 mb
r.MaxMultipartMemory = int64(c.MaxMultipartMemory) << 20
Expand Down
2 changes: 1 addition & 1 deletion internal/repository/bilibili_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ 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("pictures").Limit(5).Find(&list).Error
err = conn.Where("id >= ?", uint64(pair.MinId+offset)).Select("dynamic_id,pictures").Limit(5).Find(&list).Error
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2c6809f

Please sign in to comment.