@@ -0,0 +1,186 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"videoweb/app/service/upstream"
|
||||
"videoweb/pub/common/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
videoTopicListPath = "/api/app/video/topic/list"
|
||||
|
||||
// mediaType:0全部 1影集 2动画
|
||||
MediaAll = 0
|
||||
MediaFilm = 1
|
||||
MediaAnime = 2
|
||||
|
||||
// sortType:1最新 2最热 3收藏最多
|
||||
SortNewest = 1
|
||||
SortHeat = 2
|
||||
SortCollect = 3
|
||||
|
||||
defaultSeriesPageSize = 16
|
||||
)
|
||||
|
||||
// SeriesListQuery 剧集页筛选:对应 video/all/list + topic/list 的 mediaType。
|
||||
type SeriesListQuery struct {
|
||||
MediaType int
|
||||
TopicID int64
|
||||
SortType int
|
||||
PageNum int
|
||||
PageSize int
|
||||
}
|
||||
|
||||
// Normalize 校正默认值。
|
||||
func (q *SeriesListQuery) Normalize() {
|
||||
if q.PageNum <= 0 {
|
||||
q.PageNum = 1
|
||||
}
|
||||
if q.PageSize <= 0 {
|
||||
q.PageSize = defaultSeriesPageSize
|
||||
}
|
||||
if q.PageSize > 50 {
|
||||
q.PageSize = 50
|
||||
}
|
||||
switch q.MediaType {
|
||||
case MediaFilm, MediaAnime:
|
||||
default:
|
||||
q.MediaType = MediaAll
|
||||
}
|
||||
switch q.SortType {
|
||||
case SortHeat, SortCollect:
|
||||
default:
|
||||
q.SortType = SortNewest
|
||||
}
|
||||
if q.TopicID < 0 {
|
||||
q.TopicID = 0
|
||||
}
|
||||
}
|
||||
|
||||
// ParseSeriesListQuery 从路径解析剧集筛选。
|
||||
// 路由:/series/ 、/series/:media/:topic/:sortType/ 、
|
||||
//
|
||||
// /series/:media/:topic/:sortType/page/:page/
|
||||
func ParseSeriesListQuery(c *gin.Context) SeriesListQuery {
|
||||
q := SeriesListQuery{PageSize: defaultSeriesPageSize}
|
||||
q.MediaType = parseMediaSegment(c.Param("media"))
|
||||
topic := strings.TrimSpace(c.Param("topic"))
|
||||
if topic != "" && !strings.EqualFold(topic, "all") {
|
||||
if id, err := strconv.ParseInt(topic, 10, 64); err == nil && id > 0 {
|
||||
q.TopicID = id
|
||||
}
|
||||
}
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(c.Param("sortType"))); err == nil {
|
||||
q.SortType = n
|
||||
}
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(c.Param("page"))); err == nil {
|
||||
q.PageNum = n
|
||||
}
|
||||
q.Normalize()
|
||||
return q
|
||||
}
|
||||
|
||||
func parseMediaSegment(raw string) int {
|
||||
raw = strings.TrimSpace(strings.ToLower(raw))
|
||||
switch raw {
|
||||
case "", "all", "0":
|
||||
return MediaAll
|
||||
case "film", "1", "yingji":
|
||||
return MediaFilm
|
||||
case "anime", "2", "donghua":
|
||||
return MediaAnime
|
||||
default:
|
||||
if n, err := strconv.Atoi(raw); err == nil {
|
||||
return n
|
||||
}
|
||||
return MediaAll
|
||||
}
|
||||
}
|
||||
|
||||
func mediaSegment(mediaType int) string {
|
||||
switch mediaType {
|
||||
case MediaFilm:
|
||||
return "film"
|
||||
case MediaAnime:
|
||||
return "anime"
|
||||
default:
|
||||
return "all"
|
||||
}
|
||||
}
|
||||
|
||||
func topicSegment(topicID int64) string {
|
||||
if topicID > 0 {
|
||||
return strconv.FormatInt(topicID, 10)
|
||||
}
|
||||
return "all"
|
||||
}
|
||||
|
||||
// SeriesListURL 生成剧集页筛选链接(纯路径)。
|
||||
func SeriesListURL(mediaType int, topicID int64, sortType, pageNum int) string {
|
||||
q := SeriesListQuery{
|
||||
MediaType: mediaType,
|
||||
TopicID: topicID,
|
||||
SortType: sortType,
|
||||
PageNum: pageNum,
|
||||
PageSize: defaultSeriesPageSize,
|
||||
}
|
||||
q.Normalize()
|
||||
return q.URL()
|
||||
}
|
||||
|
||||
// URL 输出:/series/ 或 /series/{media}/{topic}/{sort}/[page/{n}/]
|
||||
func (q SeriesListQuery) URL() string {
|
||||
q.Normalize()
|
||||
defaults := q.MediaType == MediaAll && q.TopicID == 0 && q.SortType == SortNewest
|
||||
if defaults && q.PageNum <= 1 {
|
||||
return "/series"
|
||||
}
|
||||
path := "/series/" + mediaSegment(q.MediaType) + "/" + topicSegment(q.TopicID) + "/" + strconv.Itoa(q.SortType)
|
||||
if q.PageNum > 1 {
|
||||
path += "/page/" + strconv.Itoa(q.PageNum)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// FetchVideoTopicList 拉取 GET /api/app/video/topic/list。
|
||||
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/get_api_app_video_topic_list
|
||||
func FetchVideoTopicList(c *gin.Context, mediaType int) ([]VideoTopicInfo, error) {
|
||||
switch mediaType {
|
||||
case MediaFilm, MediaAnime:
|
||||
default:
|
||||
mediaType = MediaAll
|
||||
}
|
||||
params := url.Values{}
|
||||
params.Set("mediaType", strconv.Itoa(mediaType))
|
||||
|
||||
var env APIEnvelope[VideoTopicListResp]
|
||||
if err := upstream.GetPublicJSON(c, videoTopicListPath+"?"+params.Encode(), &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if env.Code != 200 {
|
||||
return nil, fmt.Errorf("video topic list code=%d msg=%s", env.Code, env.Msg)
|
||||
}
|
||||
return env.Data.List, nil
|
||||
}
|
||||
|
||||
// FetchVideoTopicListOrEmpty 失败时返回空题材。
|
||||
func FetchVideoTopicListOrEmpty(c *gin.Context, mediaType int) []VideoTopicInfo {
|
||||
list, err := FetchVideoTopicList(c, mediaType)
|
||||
if err != nil {
|
||||
log.Error("fetch video topic list failed", log.E(err))
|
||||
return nil
|
||||
}
|
||||
out := make([]VideoTopicInfo, 0, len(list))
|
||||
for _, item := range list {
|
||||
if item.ID <= 0 || item.DisplayName() == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user