282 lines
7.8 KiB
Go
282 lines
7.8 KiB
Go
package sitemap
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"videoweb/pub/common/utils"
|
|
"videoweb/pub/models/contenttagmod"
|
|
"videoweb/pub/models/playlethomeconfigmod"
|
|
"videoweb/pub/models/playletthememod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
const (
|
|
h5SortHot = "hot"
|
|
h5SortLatest = "latest"
|
|
h5TopicSort = "editorial"
|
|
h5RankPlaylet = "drama"
|
|
h5RankVideo = "video"
|
|
)
|
|
|
|
// getUrl 按 Next H5 路由生成 sitemap 分片:页面、短剧详情、视频详情。
|
|
func getUrl(sg *SitemapGenerator) {
|
|
webAddr := strings.TrimRight(strings.TrimSpace(sg.WebAddr), "/")
|
|
if webAddr == "" {
|
|
return
|
|
}
|
|
|
|
ctx := context.Background()
|
|
var (
|
|
wg sync.WaitGroup
|
|
|
|
videoEntries []videoEntry
|
|
videoTag map[int64]struct{}
|
|
|
|
playletItems []idWithTime
|
|
playletTagIDs map[int64]struct{}
|
|
|
|
themes []idWithTime
|
|
homeModules []idWithTime
|
|
)
|
|
|
|
wg.Add(4)
|
|
go func() {
|
|
defer wg.Done()
|
|
videoEntries, videoTag, _ = getVideoList(ctx)
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
playletItems, _, playletTagIDs = getPlayletList(ctx)
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
themes = getEnabledPlayletThemes(ctx)
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
homeModules = getEnabledHomeModules(ctx)
|
|
}()
|
|
wg.Wait()
|
|
|
|
videoCatItems := getVideoCategoryList(ctx, videoTag, len(videoEntries) > 0)
|
|
videoTagItems := getContentTagList(ctx, videoTag, contenttagmod.ContentTagTypeVideo)
|
|
playletTagItems := getContentTagList(ctx, playletTagIDs, contenttagmod.ContentTagTypePlaylet)
|
|
|
|
now := time.Now()
|
|
pages := make([]URL, 0, 64)
|
|
seen := make(map[string]struct{})
|
|
add := func(dst *[]URL, loc, changeFreq string, priority float32, lastMod time.Time) {
|
|
loc = strings.TrimSpace(loc)
|
|
if loc == "" {
|
|
return
|
|
}
|
|
if _, ok := seen[loc]; ok {
|
|
return
|
|
}
|
|
seen[loc] = struct{}{}
|
|
if lastMod.IsZero() {
|
|
lastMod = now
|
|
}
|
|
*dst = append(*dst, URL{
|
|
Loc: loc,
|
|
LastMod: lastMod.Format(time.DateOnly),
|
|
ChangeFreq: changeFreq,
|
|
Priority: priority,
|
|
})
|
|
}
|
|
|
|
for _, spec := range h5StaticPages() {
|
|
add(&pages, h5Abs(webAddr, spec.path), spec.changeFreq, spec.priority, now)
|
|
}
|
|
for _, sort := range []string{h5SortHot, h5SortLatest} {
|
|
add(&pages, h5Abs(webAddr, "/short-drama", sort), "daily", 0.85, now)
|
|
add(&pages, h5Abs(webAddr, "/video", sort), "daily", 0.85, now)
|
|
for _, theme := range themes {
|
|
seg := detailSegment(theme.Slug, theme.Id)
|
|
if seg == "" || isReservedListSegment(seg) {
|
|
continue
|
|
}
|
|
add(&pages, h5Abs(webAddr, "/short-drama", sort, seg), "daily", 0.7, theme.LastMod)
|
|
}
|
|
for _, category := range videoCatItems {
|
|
seg := detailSegment(category.Slug, category.Id)
|
|
if seg == "" || isReservedListSegment(seg) {
|
|
continue
|
|
}
|
|
add(&pages, h5Abs(webAddr, "/video", sort, seg), "daily", 0.7, category.LastMod)
|
|
}
|
|
}
|
|
for _, tag := range playletTagItems {
|
|
seg := detailSegment(tag.Slug, tag.Id)
|
|
if seg == "" {
|
|
continue
|
|
}
|
|
add(&pages, h5Abs(webAddr, "/tag/playlet", seg), "weekly", 0.65, tag.LastMod)
|
|
}
|
|
for _, theme := range themes {
|
|
seg := detailSegment(theme.Slug, theme.Id)
|
|
if seg == "" {
|
|
continue
|
|
}
|
|
add(&pages, h5Abs(webAddr, "/topic/playlet", seg, h5TopicSort), "weekly", 0.7, theme.LastMod)
|
|
}
|
|
for _, tag := range videoTagItems {
|
|
seg := detailSegment(tag.Slug, tag.Id)
|
|
if seg == "" {
|
|
continue
|
|
}
|
|
add(&pages, h5Abs(webAddr, "/tag/video", seg), "weekly", 0.65, tag.LastMod)
|
|
}
|
|
for _, category := range videoCatItems {
|
|
seg := detailSegment(category.Slug, category.Id)
|
|
if seg == "" {
|
|
continue
|
|
}
|
|
add(&pages, h5Abs(webAddr, "/topic/video", seg, h5TopicSort), "weekly", 0.7, category.LastMod)
|
|
}
|
|
for _, module := range homeModules {
|
|
if module.Id <= 0 {
|
|
continue
|
|
}
|
|
add(&pages, h5Abs(webAddr, "/more", moreSegment(module.Slug, module.Id)), "daily", 0.65, module.LastMod)
|
|
}
|
|
if len(pages) > 0 {
|
|
_ = sg.writeSitemapFile("sitemap-pages.xml", pages)
|
|
}
|
|
|
|
playletURLs := make([]URL, 0, len(playletItems))
|
|
for _, item := range playletItems {
|
|
seg := detailSegment(item.Slug, item.Id)
|
|
if seg == "" || isReservedListSegment(seg) {
|
|
continue
|
|
}
|
|
add(&playletURLs, h5Abs(webAddr, "/short-drama", seg), "weekly", 0.8, item.LastMod)
|
|
}
|
|
if len(playletURLs) > 0 {
|
|
_ = sg.writeSitemapFiles("sitemap-playletdetail", playletURLs)
|
|
}
|
|
|
|
videoURLs := make([]URL, 0, len(videoEntries))
|
|
for _, item := range videoEntries {
|
|
seg := detailSegment(item.Slug, item.Id)
|
|
if seg == "" || isReservedListSegment(seg) {
|
|
continue
|
|
}
|
|
add(&videoURLs, h5Abs(webAddr, "/video", seg), "weekly", 0.8, item.LastMod)
|
|
}
|
|
if len(videoURLs) > 0 {
|
|
_ = sg.writeSitemapFiles("sitemap-videodetail", videoURLs)
|
|
}
|
|
}
|
|
|
|
type h5StaticPage struct {
|
|
path string
|
|
changeFreq string
|
|
priority float32
|
|
}
|
|
|
|
func h5StaticPages() []h5StaticPage {
|
|
return []h5StaticPage{
|
|
{path: "/", changeFreq: "daily", priority: 1},
|
|
{path: "/welfare", changeFreq: "weekly", priority: 0.6},
|
|
{path: "/nav", changeFreq: "weekly", priority: 0.7},
|
|
{path: "/way-home", changeFreq: "weekly", priority: 0.7},
|
|
{path: "/about", changeFreq: "monthly", priority: 0.4},
|
|
{path: "/terms", changeFreq: "monthly", priority: 0.3},
|
|
{path: "/privacy", changeFreq: "monthly", priority: 0.3},
|
|
{path: "/help", changeFreq: "monthly", priority: 0.3},
|
|
{path: "/ranking/" + h5RankPlaylet, changeFreq: "daily", priority: 0.8},
|
|
{path: "/ranking/" + h5RankVideo, changeFreq: "daily", priority: 0.8},
|
|
}
|
|
}
|
|
|
|
func isReservedListSegment(seg string) bool {
|
|
return seg == h5SortHot || seg == h5SortLatest || seg == h5RankPlaylet || seg == h5RankVideo
|
|
}
|
|
|
|
// moreSegment 首页更多页使用配置 slug。历史数据没有 slug 时使用 backfill 的字母标识。
|
|
func moreSegment(slug string, id int64) string {
|
|
if s := strings.TrimSpace(slug); s != "" {
|
|
return s
|
|
}
|
|
if id > 0 {
|
|
return fmt.Sprintf("home-config-%d", id)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// h5Abs 拼出与前端 encodeURIComponent 路径段一致的绝对地址。
|
|
func h5Abs(webAddr string, parts ...string) string {
|
|
segments := make([]string, 0, len(parts))
|
|
for _, part := range parts {
|
|
part = strings.Trim(part, "/")
|
|
if part == "" {
|
|
continue
|
|
}
|
|
for _, seg := range strings.Split(part, "/") {
|
|
seg = strings.TrimSpace(seg)
|
|
if seg == "" {
|
|
continue
|
|
}
|
|
segments = append(segments, url.PathEscape(seg))
|
|
}
|
|
}
|
|
path := "/"
|
|
if len(segments) > 0 {
|
|
path = "/" + strings.Join(segments, "/")
|
|
}
|
|
return utils.JoinUrlPathKeepSlash(webAddr, path)
|
|
}
|
|
|
|
func getEnabledPlayletThemes(ctx context.Context) []idWithTime {
|
|
filter := bson.M{playletthememod.FN.PlayletTheme.Status: true}
|
|
pageSize := int64(1000)
|
|
items := make([]idWithTime, 0)
|
|
for i := int64(0); i < 100; i++ {
|
|
list, _ := playletthememod.FindManyPlayletTheme(ctx, filter, options.Find().
|
|
SetSort(bson.M{playletthememod.FN.PlayletTheme.Rank: -1}).
|
|
SetSkip(i*pageSize).
|
|
SetLimit(pageSize).
|
|
SetProjection(bson.M{
|
|
playletthememod.FN.PlayletTheme.Id: 1,
|
|
playletthememod.FN.PlayletTheme.Slug: 1,
|
|
playletthememod.FN.PlayletTheme.UpdatedAt: 1,
|
|
}))
|
|
for _, info := range list {
|
|
if info == nil || info.Id <= 0 {
|
|
continue
|
|
}
|
|
items = append(items, idWithTime{Id: info.Id, Slug: info.Slug, LastMod: info.UpdatedAt})
|
|
}
|
|
if len(list) < int(pageSize) {
|
|
break
|
|
}
|
|
}
|
|
return items
|
|
}
|
|
|
|
func getEnabledHomeModules(ctx context.Context) []idWithTime {
|
|
filter := bson.M{playlethomeconfigmod.FN.PlayletHomeConfig.Enabled: true}
|
|
list, _ := playlethomeconfigmod.FindManyPlayletHomeConfig(ctx, filter, options.Find().
|
|
SetProjection(bson.M{
|
|
playlethomeconfigmod.FN.PlayletHomeConfig.Id: 1,
|
|
playlethomeconfigmod.FN.PlayletHomeConfig.Slug: 1,
|
|
playlethomeconfigmod.FN.PlayletHomeConfig.UpdatedAt: 1,
|
|
}))
|
|
items := make([]idWithTime, 0, len(list))
|
|
for _, info := range list {
|
|
if info == nil || info.Id <= 0 {
|
|
continue
|
|
}
|
|
items = append(items, idWithTime{Id: info.Id, Slug: info.Slug, LastMod: info.UpdatedAt})
|
|
}
|
|
return items
|
|
}
|