181 lines
5.2 KiB
Go
181 lines
5.2 KiB
Go
package sitemap
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"videoweb/pub/common/netutil"
|
|
"videoweb/pub/common/utils"
|
|
)
|
|
|
|
const (
|
|
videoSitemapShardPrefix = "sitemap-videos-"
|
|
videoSitemapDefaultThumb = "/static/images/og-default.jpg"
|
|
videoSitemapDefaultDesc = "高清成人视频在线观看。"
|
|
videoSitemapEmbedPathFmt = "/video/embed?id=%d"
|
|
)
|
|
|
|
type videoEntry struct {
|
|
Id int64
|
|
Slug string
|
|
Title string
|
|
LastMod time.Time
|
|
UnencryptedPreMp4 string
|
|
ContentLoc string
|
|
}
|
|
|
|
// prepareVideoSitemapEntries 为全部视频补齐 sitemap 字段;有 UnencryptedPreMp4 时额外设置 content_loc。
|
|
func prepareVideoSitemapEntries(entries []videoEntry, cdn string) []videoEntry {
|
|
cdn = strings.TrimSpace(cdn)
|
|
out := make([]videoEntry, 0, len(entries))
|
|
for _, e := range entries {
|
|
if e.Id <= 0 {
|
|
continue
|
|
}
|
|
if cdn != "" {
|
|
if preMp4 := strings.TrimSpace(e.UnencryptedPreMp4); preMp4 != "" {
|
|
if contentLoc := netutil.MergeURL(cdn, preMp4); contentLoc != "" {
|
|
e.ContentLoc = contentLoc
|
|
}
|
|
}
|
|
}
|
|
out = append(out, e)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (sg *SitemapGenerator) writeVideoSitemapFiles(entries []videoEntry) error {
|
|
if len(entries) == 0 {
|
|
sg.cleanupVideoSitemapShards(0)
|
|
return nil
|
|
}
|
|
|
|
limit := sg.maxURLsPerFile()
|
|
shardCount := (len(entries) + limit - 1) / limit
|
|
sg.cleanupVideoSitemapShards(shardCount)
|
|
|
|
thumb := strings.TrimRight(sg.WebAddr, "/") + videoSitemapDefaultThumb
|
|
for i := 0; i < len(entries); i += limit {
|
|
end := i + limit
|
|
if end > len(entries) {
|
|
end = len(entries)
|
|
}
|
|
name := fmt.Sprintf("%s%d.xml", videoSitemapShardPrefix, i/limit)
|
|
if err := sg.writeOneVideoSitemap(name, entries[i:end], thumb); err != nil {
|
|
return err
|
|
}
|
|
sg.SitemapFiles = append(sg.SitemapFiles, name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sg *SitemapGenerator) cleanupVideoSitemapShards(shardCount int) {
|
|
entries, err := os.ReadDir(sg.OutputDir)
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
continue
|
|
}
|
|
name := entry.Name()
|
|
if !strings.HasPrefix(name, videoSitemapShardPrefix) || !strings.HasSuffix(name, ".xml") {
|
|
continue
|
|
}
|
|
idxText := strings.TrimSuffix(strings.TrimPrefix(name, videoSitemapShardPrefix), ".xml")
|
|
idx, err := strconv.Atoi(idxText)
|
|
if err != nil || shardCount <= 0 || idx >= shardCount {
|
|
_ = os.Remove(filepath.Join(sg.OutputDir, name))
|
|
}
|
|
}
|
|
}
|
|
|
|
func (sg *SitemapGenerator) writeOneVideoSitemap(filename string, entries []videoEntry, thumb string) error {
|
|
var b bytes.Buffer
|
|
b.WriteString(xml.Header)
|
|
b.WriteString(stylesheetURLSet)
|
|
b.WriteString(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"` + "\n")
|
|
b.WriteString(` xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">` + "\n")
|
|
for _, e := range entries {
|
|
if e.Id <= 0 {
|
|
continue
|
|
}
|
|
loc := videoEntryLoc(sg.WebAddr, e)
|
|
if loc == "" {
|
|
continue
|
|
}
|
|
playerLoc := videoEntryPlayerLoc(sg.WebAddr, e.Id)
|
|
title := xmlEscapeVideoSitemap(videoEntryTitle(e))
|
|
desc := xmlEscapeVideoSitemap(videoSitemapDefaultDesc)
|
|
lastMod := videoEntryLastMod(e)
|
|
|
|
b.WriteString(" <url>\n")
|
|
fmt.Fprintf(&b, " <loc>%s</loc>\n", xmlEscapeVideoSitemap(loc))
|
|
fmt.Fprintf(&b, " <lastmod>%s</lastmod>\n", xmlEscapeVideoSitemap(lastMod))
|
|
b.WriteString(" <changefreq>weekly</changefreq>\n")
|
|
b.WriteString(" <priority>0.8</priority>\n")
|
|
b.WriteString(" <video:video>\n")
|
|
fmt.Fprintf(&b, " <video:thumbnail_loc>%s</video:thumbnail_loc>\n", xmlEscapeVideoSitemap(thumb))
|
|
fmt.Fprintf(&b, " <video:title>%s</video:title>\n", title)
|
|
fmt.Fprintf(&b, " <video:description>%s</video:description>\n", desc)
|
|
if contentLoc := strings.TrimSpace(e.ContentLoc); contentLoc != "" {
|
|
fmt.Fprintf(&b, " <video:content_loc>%s</video:content_loc>\n", xmlEscapeVideoSitemap(contentLoc))
|
|
}
|
|
fmt.Fprintf(&b, " <video:player_loc>%s</video:player_loc>\n", xmlEscapeVideoSitemap(playerLoc))
|
|
b.WriteString(" <video:family_friendly>no</video:family_friendly>\n")
|
|
b.WriteString(" </video:video>\n")
|
|
b.WriteString(" </url>\n")
|
|
}
|
|
b.WriteString("</urlset>\n")
|
|
|
|
path := filepath.Join(sg.OutputDir, filename)
|
|
return os.WriteFile(path, b.Bytes(), 0644)
|
|
}
|
|
|
|
func videoEntryLoc(webAddr string, e videoEntry) string {
|
|
seg := detailSegment(e.Slug, e.Id)
|
|
if seg == "" {
|
|
return ""
|
|
}
|
|
return utils.JoinUrlPathKeepSlash(webAddr, "/video-detail/"+seg)
|
|
}
|
|
|
|
func videoEntryPlayerLoc(webAddr string, id int64) string {
|
|
return utils.JoinUrlPathKeepSlash(webAddr, fmt.Sprintf(videoSitemapEmbedPathFmt, id))
|
|
}
|
|
|
|
func videoEntryTitle(e videoEntry) string {
|
|
if title := strings.TrimSpace(e.Title); title != "" {
|
|
return truncateRunesVideoSitemap(title, 100)
|
|
}
|
|
return fmt.Sprintf("视频 %d", e.Id)
|
|
}
|
|
|
|
func videoEntryLastMod(e videoEntry) string {
|
|
if e.LastMod.IsZero() {
|
|
return time.Now().Format(time.DateOnly)
|
|
}
|
|
return e.LastMod.Format(time.DateOnly)
|
|
}
|
|
|
|
func xmlEscapeVideoSitemap(s string) string {
|
|
var buf bytes.Buffer
|
|
_ = xml.EscapeText(&buf, []byte(s))
|
|
return buf.String()
|
|
}
|
|
|
|
func truncateRunesVideoSitemap(s string, max int) string {
|
|
if max <= 0 || utf8.RuneCountInString(s) <= max {
|
|
return s
|
|
}
|
|
runes := []rune(s)
|
|
return string(runes[:max])
|
|
}
|