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(`` + "\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(" \n") fmt.Fprintf(&b, " %s\n", xmlEscapeVideoSitemap(loc)) fmt.Fprintf(&b, " %s\n", xmlEscapeVideoSitemap(lastMod)) b.WriteString(" weekly\n") b.WriteString(" 0.8\n") b.WriteString(" \n") fmt.Fprintf(&b, " %s\n", xmlEscapeVideoSitemap(thumb)) fmt.Fprintf(&b, " %s\n", title) fmt.Fprintf(&b, " %s\n", desc) if contentLoc := strings.TrimSpace(e.ContentLoc); contentLoc != "" { fmt.Fprintf(&b, " %s\n", xmlEscapeVideoSitemap(contentLoc)) } fmt.Fprintf(&b, " %s\n", xmlEscapeVideoSitemap(playerLoc)) b.WriteString(" no\n") b.WriteString(" \n") b.WriteString(" \n") } b.WriteString("\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]) }