@@ -0,0 +1,180 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsIndexablePath 是否为需要收录的公开页。
|
||||
// 采用白名单策略:新增页面只有明确归类后才允许进入索引,避免搜索、账户、
|
||||
// 排序变体和工具页因遗漏配置被搜索引擎收录。
|
||||
func IsIndexablePath(raw string) bool {
|
||||
path := normalizedPath(raw)
|
||||
if path == "/" {
|
||||
return true
|
||||
}
|
||||
switch path {
|
||||
case "/about", "/help", "/terms", "/privacy", "/2257", "/dmca":
|
||||
return true
|
||||
}
|
||||
|
||||
segments := splitPath(path)
|
||||
if len(segments) == 0 {
|
||||
return false
|
||||
}
|
||||
switch segments[0] {
|
||||
case "list":
|
||||
return isIndexablePlayletList(segments)
|
||||
case "series":
|
||||
return isIndexableSeriesList(segments)
|
||||
case "topic":
|
||||
return isIndexableSimplePagination(segments, "topic")
|
||||
case "topic-detail", "tags":
|
||||
return isIndexableNamedCollection(segments)
|
||||
case "rank", "popular-recommend":
|
||||
return isIndexableRanking(segments)
|
||||
case "more-list", "more-series-list":
|
||||
return isIndexableCuratedCollection(segments)
|
||||
case "recently-added":
|
||||
return isIndexableSimplePagination(segments, "recently-added")
|
||||
case "play":
|
||||
return isIndexablePlayletPlayback(segments)
|
||||
case "series-play":
|
||||
return len(segments) == 2 && segments[1] != ""
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// IsPrivatePath 标记不应跟踪链接的账户、认证和交易页面。
|
||||
func IsPrivatePath(raw string) bool {
|
||||
path := normalizedPath(raw)
|
||||
for _, prefix := range []string{
|
||||
"/login", "/register", "/profile", "/history", "/favorites",
|
||||
"/purchased", "/setting", "/settings", "/recharge", "/feedback",
|
||||
} {
|
||||
if path == prefix || strings.HasPrefix(path, prefix+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func normalizedPath(raw string) string {
|
||||
path := strings.TrimSpace(raw)
|
||||
if u, err := url.Parse(path); err == nil && u.Path != "" {
|
||||
path = u.Path
|
||||
}
|
||||
path = "/" + strings.Trim(strings.TrimSpace(path), "/")
|
||||
if path == "/" {
|
||||
return path
|
||||
}
|
||||
return strings.TrimSuffix(path, "/")
|
||||
}
|
||||
|
||||
func splitPath(path string) []string {
|
||||
path = strings.Trim(path, "/")
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(path, "/")
|
||||
}
|
||||
|
||||
func positiveInt(raw string) bool {
|
||||
n, err := strconv.Atoi(raw)
|
||||
return err == nil && n > 0
|
||||
}
|
||||
|
||||
func pageNumber(raw string) bool {
|
||||
n, err := strconv.Atoi(raw)
|
||||
return err == nil && n > 1
|
||||
}
|
||||
|
||||
func isIndexableSimplePagination(segments []string, root string) bool {
|
||||
if len(segments) == 1 && segments[0] == root {
|
||||
return true
|
||||
}
|
||||
return len(segments) == 3 && segments[0] == root && segments[1] == "page" && pageNumber(segments[2])
|
||||
}
|
||||
|
||||
func isIndexablePlayletList(segments []string) bool {
|
||||
if len(segments) == 1 {
|
||||
return true
|
||||
}
|
||||
if len(segments) == 2 {
|
||||
return segments[1] != "" && segments[1] != "all"
|
||||
}
|
||||
base, hasPage := withoutPageSuffix(segments)
|
||||
if len(base) != 4 || base[1] == "" {
|
||||
return false
|
||||
}
|
||||
updateStatus, updateErr := strconv.Atoi(base[2])
|
||||
sortType, sortErr := strconv.Atoi(base[3])
|
||||
if updateErr != nil || sortErr != nil || updateStatus < 0 || updateStatus > 2 || sortType != 1 {
|
||||
return false
|
||||
}
|
||||
// 默认第一页已有更短规范地址 /list 或 /list/:theme。
|
||||
return hasPage || updateStatus != 0
|
||||
}
|
||||
|
||||
func isIndexableSeriesList(segments []string) bool {
|
||||
if len(segments) == 1 {
|
||||
return true
|
||||
}
|
||||
base, hasPage := withoutPageSuffix(segments)
|
||||
if len(base) != 4 {
|
||||
return false
|
||||
}
|
||||
media, topic, sortType := base[1], base[2], base[3]
|
||||
if media != "all" && media != "film" && media != "anime" {
|
||||
return false
|
||||
}
|
||||
if topic != "all" && !positiveInt(topic) {
|
||||
return false
|
||||
}
|
||||
if sortType != "1" {
|
||||
return false
|
||||
}
|
||||
// 默认第一页已有更短规范地址 /series。
|
||||
return hasPage || media != "all" || topic != "all"
|
||||
}
|
||||
|
||||
func isIndexableNamedCollection(segments []string) bool {
|
||||
if len(segments) == 2 && segments[1] != "" {
|
||||
return true
|
||||
}
|
||||
return len(segments) == 4 && segments[1] != "" && segments[2] == "page" && pageNumber(segments[3])
|
||||
}
|
||||
|
||||
func isIndexableRanking(segments []string) bool {
|
||||
if len(segments) == 1 {
|
||||
return true
|
||||
}
|
||||
if len(segments) == 2 {
|
||||
return segments[1] == "2" || segments[1] == "3"
|
||||
}
|
||||
if len(segments) == 3 {
|
||||
return segments[1] == "page" && pageNumber(segments[2])
|
||||
}
|
||||
return len(segments) == 4 && (segments[1] == "2" || segments[1] == "3") && segments[2] == "page" && pageNumber(segments[3])
|
||||
}
|
||||
|
||||
func isIndexableCuratedCollection(segments []string) bool {
|
||||
if len(segments) == 2 {
|
||||
return segments[1] != ""
|
||||
}
|
||||
return len(segments) == 4 && segments[1] != "" && segments[2] == "page" && pageNumber(segments[3])
|
||||
}
|
||||
|
||||
func isIndexablePlayletPlayback(segments []string) bool {
|
||||
// 规范入口是 /play/{slug};各集 /play/{slug}/{ep} 近重复且多为薄播放页,不收录。
|
||||
return len(segments) == 2 && segments[1] != ""
|
||||
}
|
||||
|
||||
func withoutPageSuffix(segments []string) ([]string, bool) {
|
||||
if len(segments) < 3 || segments[len(segments)-2] != "page" || !pageNumber(segments[len(segments)-1]) {
|
||||
return segments, false
|
||||
}
|
||||
return segments[:len(segments)-2], true
|
||||
}
|
||||
@@ -0,0 +1,940 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"videoweb/app/service/applistser"
|
||||
"videoweb/app/service/configser"
|
||||
"videoweb/app/service/playletser"
|
||||
"videoweb/app/service/videoser"
|
||||
)
|
||||
|
||||
const brandName = "黄果短剧"
|
||||
|
||||
type ldThing map[string]any
|
||||
|
||||
// FAQItem 同时驱动页面可见内容与 FAQPage JSON-LD,避免标记内容与页面正文不一致。
|
||||
type FAQItem struct {
|
||||
Question string
|
||||
Answer string
|
||||
}
|
||||
|
||||
// PageJSONLD 按页面数据生成可被 Schema.org / 富媒体测试校验的 JSON-LD。
|
||||
// 仅收录页生成;非收录页返回空。pageCanonical 为当前请求完整 URL。
|
||||
func PageJSONLD(siteURL, pageCanonical, path string, data map[string]any, cfg configser.AppConfigVM) template.JS {
|
||||
siteURL = strings.TrimRight(strings.TrimSpace(siteURL), "/")
|
||||
pageCanonical = strings.TrimSpace(pageCanonical)
|
||||
path = strings.TrimSpace(path)
|
||||
if !IsIndexablePath(path) {
|
||||
return ""
|
||||
}
|
||||
pageURL := pageCanonical
|
||||
if pageURL == "" {
|
||||
pageURL = normalizePageURL(AbsURL(siteURL, path))
|
||||
}
|
||||
graph := make([]any, 0, 8)
|
||||
graph = append(graph, organizationNode(siteURL, cfg), websiteNode(siteURL, cfg))
|
||||
|
||||
if detail, ok := asPlaylet(data["detail"]); ok {
|
||||
graph = append(graph, detailNodes(siteURL, pageURL, detail)...)
|
||||
} else if playlet, hasPlaylet := asPlaylet(data["playlet"]); hasPlaylet || data["chapter"] != nil {
|
||||
chapter, _ := asChapter(data["chapter"])
|
||||
graph = append(graph, playNodes(siteURL, pageURL, playlet, chapter)...)
|
||||
} else if video, ok := asVideo(data["video"]); ok {
|
||||
graph = append(graph, seriesPlayNodes(siteURL, pageURL, video)...)
|
||||
} else {
|
||||
pageNode := webpageNode(siteURL, pageURL, data, cfg)
|
||||
graph = append(graph, pageNode)
|
||||
if crumb := genericBreadcrumb(siteURL, pageURL, data); crumb != nil {
|
||||
pageNode["breadcrumb"] = ldThing{"@id": pageURL + "#breadcrumb"}
|
||||
graph = append(graph, crumb)
|
||||
}
|
||||
if items := itemListNode(siteURL, pageURL, data); items != nil {
|
||||
pageNode["mainEntity"] = ldThing{"@id": pageURL + "#itemlist"}
|
||||
graph = append(graph, items)
|
||||
}
|
||||
if faq := faqPageNode(siteURL, pageURL, data); faq != nil {
|
||||
pageNode["hasPart"] = ldThing{"@id": pageURL + "#faq"}
|
||||
graph = append(graph, faq)
|
||||
}
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(ldThing{
|
||||
"@context": "https://schema.org",
|
||||
"@graph": graph,
|
||||
})
|
||||
if err != nil {
|
||||
return template.JS("{}")
|
||||
}
|
||||
return template.JS(raw)
|
||||
}
|
||||
|
||||
func organizationNode(siteURL string, cfg configser.AppConfigVM) ldThing {
|
||||
// /static/favicon.png is embedded and served by this application. Keep the
|
||||
// declared dimensions aligned with the real asset so validators can fetch and
|
||||
// verify the Organization logo instead of following a non-existent root path.
|
||||
logo := AbsURL(siteURL, "/static/favicon.png")
|
||||
node := ldThing{
|
||||
"@type": "Organization",
|
||||
"@id": siteURL + "/#organization",
|
||||
"name": siteName(cfg),
|
||||
"url": AbsURL(siteURL, "/"),
|
||||
"logo": ldThing{
|
||||
"@type": "ImageObject",
|
||||
"@id": siteURL + "/#logo",
|
||||
"url": logo,
|
||||
"contentUrl": logo,
|
||||
"width": 132,
|
||||
"height": 132,
|
||||
},
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func websiteNode(siteURL string, cfg configser.AppConfigVM) ldThing {
|
||||
name := siteName(cfg)
|
||||
desc := strings.TrimSpace(cfg.SeoInfo.Description)
|
||||
if desc == "" {
|
||||
desc = name + "汇集热门短剧、今日更新与精选推荐,免费在线观看高清短剧。"
|
||||
}
|
||||
node := ldThing{
|
||||
"@type": "WebSite",
|
||||
"@id": siteURL + "/#website",
|
||||
"name": siteName(cfg),
|
||||
"url": AbsURL(siteURL, "/"),
|
||||
"description": desc,
|
||||
"inLanguage": "zh-CN",
|
||||
"publisher": ldThing{"@id": siteURL + "/#organization"},
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func detailNodes(siteURL, pageURL string, d *playletser.PlayletInfoResp) []any {
|
||||
detailURL := pageURL
|
||||
playURL := AbsURL(siteURL, playletPath("/play", d.Slug, d.ID))
|
||||
name := firstNonEmpty(d.Title, d.SeoInfo.Title, "短剧详情")
|
||||
desc := firstNonEmpty(d.SeoInfo.Description, d.Desc, fmt.Sprintf("在线观看《%s》,高清短剧全集免费看。", d.Title))
|
||||
cover := absMedia(siteURL, firstNonEmpty(d.Cover, d.Banner))
|
||||
seriesID := detailURL + "#tvseries"
|
||||
|
||||
series := ldThing{
|
||||
"@type": "TVSeries",
|
||||
"@id": seriesID,
|
||||
"name": name,
|
||||
"description": desc,
|
||||
"url": detailURL,
|
||||
"inLanguage": "zh-CN",
|
||||
"publisher": ldThing{"@id": siteURL + "/#organization"},
|
||||
"potentialAction": ldThing{
|
||||
"@type": "WatchAction",
|
||||
"target": ldThing{
|
||||
"@type": "EntryPoint",
|
||||
"urlTemplate": playURL,
|
||||
},
|
||||
},
|
||||
}
|
||||
if cover != "" {
|
||||
series["image"] = []string{cover}
|
||||
series["thumbnailUrl"] = []string{cover}
|
||||
}
|
||||
if d.TotalEpisode > 0 {
|
||||
series["numberOfEpisodes"] = d.TotalEpisode
|
||||
}
|
||||
if published := isoDate(firstNonEmpty(d.OnlineAt, d.CreatedAt)); published != "" {
|
||||
series["datePublished"] = published
|
||||
}
|
||||
if modified := isoDate(firstNonEmpty(d.UpdatedAt, d.MediaUpdatedAt)); modified != "" {
|
||||
series["dateModified"] = modified
|
||||
}
|
||||
if genres := uniqueStrings(append(append([]string{}, d.SeoInfo.Keywords...), append(d.TagNames, d.ThemeNames...)...)); len(genres) > 0 {
|
||||
series["genre"] = genres
|
||||
}
|
||||
if actor := strings.TrimSpace(d.Anchor); actor != "" {
|
||||
series["actor"] = ldThing{"@type": "Person", "name": actor}
|
||||
}
|
||||
|
||||
crumb := breadcrumb(siteURL, [][2]string{
|
||||
{"首页", AbsURL(siteURL, "/")},
|
||||
{"短剧", AbsURL(siteURL, "/list/ai-short-play")},
|
||||
{name, detailURL},
|
||||
})
|
||||
page := contentWebPageNode(siteURL, detailURL, name, desc, "WebPage")
|
||||
page["mainEntity"] = ldThing{"@id": seriesID}
|
||||
page["breadcrumb"] = ldThing{"@id": detailURL + "#breadcrumb"}
|
||||
return []any{page, crumb, series}
|
||||
}
|
||||
|
||||
func playNodes(siteURL, pageURL string, playlet *playletser.PlayletInfoResp, chapter *playletser.PlayletChapterInfoResp) []any {
|
||||
playletID := int64(0)
|
||||
playletTitle := "短剧"
|
||||
cover := ""
|
||||
desc := "高清在线播放。"
|
||||
published := ""
|
||||
modified := ""
|
||||
contentURL := ""
|
||||
playURL := pageURL
|
||||
detailURL := AbsURL(siteURL, "/")
|
||||
seriesID := ""
|
||||
totalEpisodes := 0
|
||||
genres := []string(nil)
|
||||
|
||||
if playlet != nil {
|
||||
playletID = playlet.ID
|
||||
playletTitle = firstNonEmpty(playlet.Title, playlet.SeoInfo.Title, playletTitle)
|
||||
cover = absMedia(siteURL, firstNonEmpty(playlet.Cover, playlet.Banner))
|
||||
desc = firstNonEmpty(chapterSEODesc(chapter), playlet.SeoInfo.Description, playlet.Desc, fmt.Sprintf("正在播放《%s》", playlet.Title))
|
||||
published = isoDateTime(firstNonEmpty(
|
||||
playlet.OnlineAt,
|
||||
playlet.MediaCreatedAt,
|
||||
playlet.CreatedAt,
|
||||
))
|
||||
modified = isoDateTime(firstNonEmpty(playlet.MediaUpdatedAt, playlet.UpdatedAt))
|
||||
detailURL = AbsURL(siteURL, playletPath("/play", playlet.Slug, playlet.ID))
|
||||
seriesID = detailURL + "#tvseries"
|
||||
totalEpisodes = playlet.TotalEpisode
|
||||
genres = uniqueStrings(append(append([]string{}, playlet.TagNames...), playlet.ThemeNames...))
|
||||
}
|
||||
episode := 0
|
||||
duration := 0
|
||||
if chapter != nil {
|
||||
episode = chapter.CurrentEpisode
|
||||
duration = chapter.MediaTime
|
||||
contentURL = videoContentURL(siteURL, chapter.VideoURL)
|
||||
if playletID == 0 && chapter.PlayletID > 0 {
|
||||
playletID = chapter.PlayletID
|
||||
detailURL = AbsURL(siteURL, playletPath("/play", "", playletID))
|
||||
}
|
||||
}
|
||||
|
||||
videoName := playletTitle
|
||||
if episode > 0 {
|
||||
videoName = fmt.Sprintf("%s - 第%d集", playletTitle, episode)
|
||||
}
|
||||
|
||||
crumbs := [][2]string{{"首页", AbsURL(siteURL, "/")}}
|
||||
if playletID > 0 {
|
||||
crumbs = append(crumbs, [2]string{playletTitle, detailURL})
|
||||
}
|
||||
crumbs = append(crumbs, [2]string{videoName, playURL})
|
||||
|
||||
page := contentWebPageNode(siteURL, playURL, videoName, desc, "WebPage")
|
||||
page["breadcrumb"] = ldThing{"@id": playURL + "#breadcrumb"}
|
||||
nodes := []any{page, breadcrumb(siteURL, crumbs)}
|
||||
|
||||
if playlet != nil {
|
||||
series := ldThing{
|
||||
"@type": "TVSeries",
|
||||
"@id": seriesID,
|
||||
"name": playletTitle,
|
||||
"description": firstNonEmpty(playlet.SeoInfo.Description, playlet.Desc, desc),
|
||||
"url": detailURL,
|
||||
"inLanguage": "zh-CN",
|
||||
"publisher": ldThing{"@id": siteURL + "/#organization"},
|
||||
}
|
||||
if cover != "" {
|
||||
series["image"] = []string{cover}
|
||||
series["thumbnailUrl"] = []string{cover}
|
||||
}
|
||||
if totalEpisodes > 0 {
|
||||
series["numberOfEpisodes"] = totalEpisodes
|
||||
}
|
||||
if len(genres) > 0 {
|
||||
series["genre"] = genres
|
||||
}
|
||||
if published != "" {
|
||||
series["datePublished"] = published
|
||||
}
|
||||
if modified != "" {
|
||||
series["dateModified"] = modified
|
||||
}
|
||||
nodes = append(nodes, series)
|
||||
}
|
||||
|
||||
// Google 的 VideoObject 富媒体结果要求 name、thumbnailUrl、uploadDate。
|
||||
// 缺少真实封面或发布日期时只输出 WebPage,避免用虚构值凑齐必填字段。
|
||||
if cover == "" || published == "" {
|
||||
return nodes
|
||||
}
|
||||
|
||||
videoID := playURL + "#video"
|
||||
video := ldThing{
|
||||
"@type": "VideoObject",
|
||||
"@id": videoID,
|
||||
"name": videoName,
|
||||
"description": desc,
|
||||
"url": playURL,
|
||||
"mainEntityOfPage": ldThing{"@id": playURL + "#webpage"},
|
||||
"inLanguage": "zh-CN",
|
||||
"isFamilyFriendly": false,
|
||||
"contentRating": "adult",
|
||||
"isPartOf": ldThing{"@id": siteURL + "/#website"},
|
||||
"publisher": ldThing{"@id": siteURL + "/#organization"},
|
||||
"potentialAction": ldThing{
|
||||
"@type": "WatchAction",
|
||||
"target": ldThing{
|
||||
"@type": "EntryPoint",
|
||||
"urlTemplate": playURL,
|
||||
},
|
||||
},
|
||||
}
|
||||
video["thumbnailUrl"] = []string{cover}
|
||||
video["image"] = []string{cover}
|
||||
video["uploadDate"] = published
|
||||
if modified != "" {
|
||||
video["dateModified"] = modified
|
||||
}
|
||||
if contentURL != "" {
|
||||
video["contentUrl"] = contentURL
|
||||
}
|
||||
if duration > 0 {
|
||||
video["duration"] = fmt.Sprintf("PT%dS", duration)
|
||||
}
|
||||
if chapter != nil && chapter.CountBrowse > 0 {
|
||||
video["interactionStatistic"] = ldThing{
|
||||
"@type": "InteractionCounter",
|
||||
"interactionType": ldThing{"@type": "WatchAction"},
|
||||
"userInteractionCount": chapter.CountBrowse,
|
||||
}
|
||||
}
|
||||
if len(genres) > 0 {
|
||||
video["genre"] = genres
|
||||
video["keywords"] = strings.Join(genres, ",")
|
||||
}
|
||||
if chapter != nil {
|
||||
if chapter.Width > 0 {
|
||||
video["width"] = chapter.Width
|
||||
}
|
||||
if chapter.Height > 0 {
|
||||
video["height"] = chapter.Height
|
||||
}
|
||||
if chapter.MediaSize > 0 {
|
||||
video["contentSize"] = fmt.Sprintf("%d bytes", chapter.MediaSize)
|
||||
}
|
||||
}
|
||||
|
||||
page["mainEntity"] = ldThing{"@id": videoID}
|
||||
nodes = append(nodes, video)
|
||||
if episode > 0 {
|
||||
// 集数和所属剧集是 Episode 的属性;视频媒体通过 associatedMedia 与本集关联。
|
||||
ep := ldThing{
|
||||
"@type": "TVEpisode",
|
||||
"@id": playURL + "#episode",
|
||||
"name": videoName,
|
||||
"description": desc,
|
||||
"episodeNumber": episode,
|
||||
"url": playURL,
|
||||
"associatedMedia": ldThing{"@id": videoID},
|
||||
}
|
||||
if seriesID != "" {
|
||||
ep["partOfSeries"] = ldThing{"@id": seriesID}
|
||||
} else {
|
||||
ep["partOfSeries"] = ldThing{"@type": "TVSeries", "name": playletTitle, "url": detailURL}
|
||||
}
|
||||
if cover != "" {
|
||||
ep["image"] = []string{cover}
|
||||
}
|
||||
if published != "" {
|
||||
ep["datePublished"] = published
|
||||
}
|
||||
nodes = append(nodes, ep)
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
func seriesPlayNodes(siteURL, pageURL string, video *videoser.VideoInfoResp) []any {
|
||||
name := firstNonEmpty(video.SeoInfo.Title, video.Title, "在线播放")
|
||||
desc := firstNonEmpty(video.SeoInfo.Description, fmt.Sprintf("在线观看《%s》。", name))
|
||||
cover := absMedia(siteURL, firstNonEmpty(video.CoverImg, video.Video.Cover))
|
||||
published := isoDateTime(firstNonEmpty(video.AddedTime, video.CreatedAt))
|
||||
contentURL := videoContentURL(siteURL, video.Video.URL)
|
||||
page := contentWebPageNode(siteURL, pageURL, name, desc, "WebPage")
|
||||
crumbs := [][2]string{
|
||||
{"首页", AbsURL(siteURL, "/")},
|
||||
{"剧集", AbsURL(siteURL, "/series")},
|
||||
{name, pageURL},
|
||||
}
|
||||
page["breadcrumb"] = ldThing{"@id": pageURL + "#breadcrumb"}
|
||||
nodes := []any{page, breadcrumb(siteURL, crumbs)}
|
||||
if cover == "" || published == "" {
|
||||
return nodes
|
||||
}
|
||||
videoID := pageURL + "#video"
|
||||
item := ldThing{
|
||||
"@type": "VideoObject",
|
||||
"@id": videoID,
|
||||
"name": name,
|
||||
"description": desc,
|
||||
"url": pageURL,
|
||||
"mainEntityOfPage": ldThing{"@id": pageURL + "#webpage"},
|
||||
"inLanguage": "zh-CN",
|
||||
"isFamilyFriendly": false,
|
||||
"contentRating": "adult",
|
||||
"isPartOf": ldThing{"@id": siteURL + "/#website"},
|
||||
"publisher": ldThing{"@id": siteURL + "/#organization"},
|
||||
"thumbnailUrl": []string{cover},
|
||||
"image": []string{cover},
|
||||
"uploadDate": published,
|
||||
"potentialAction": ldThing{
|
||||
"@type": "WatchAction",
|
||||
"target": ldThing{
|
||||
"@type": "EntryPoint",
|
||||
"urlTemplate": pageURL,
|
||||
},
|
||||
},
|
||||
}
|
||||
if contentURL != "" {
|
||||
item["contentUrl"] = contentURL
|
||||
}
|
||||
if video.Video.VideoTime > 0 {
|
||||
item["duration"] = fmt.Sprintf("PT%dS", video.Video.VideoTime)
|
||||
}
|
||||
if video.Video.Width > 0 {
|
||||
item["width"] = video.Video.Width
|
||||
}
|
||||
if video.Video.Height > 0 {
|
||||
item["height"] = video.Video.Height
|
||||
}
|
||||
if video.WatchTimes > 0 {
|
||||
item["interactionStatistic"] = ldThing{
|
||||
"@type": "InteractionCounter",
|
||||
"interactionType": ldThing{"@type": "WatchAction"},
|
||||
"userInteractionCount": video.WatchTimes,
|
||||
}
|
||||
}
|
||||
page["mainEntity"] = ldThing{"@id": videoID}
|
||||
return append(nodes, item)
|
||||
}
|
||||
|
||||
func webpageNode(siteURL, pageURL string, data map[string]any, cfg configser.AppConfigVM) ldThing {
|
||||
name := strFrom(data["title"])
|
||||
if name == "" {
|
||||
name = strFrom(data["pageName"])
|
||||
}
|
||||
if name == "" {
|
||||
name = siteName(cfg)
|
||||
}
|
||||
pathOnly := pageURL
|
||||
if i := strings.IndexByte(pathOnly, '?'); i >= 0 {
|
||||
pathOnly = pathOnly[:i]
|
||||
}
|
||||
if j := strings.Index(pathOnly, "://"); j >= 0 {
|
||||
if k := strings.IndexByte(pathOnly[j+3:], '/'); k >= 0 {
|
||||
pathOnly = pathOnly[j+3+k:]
|
||||
} else {
|
||||
pathOnly = "/"
|
||||
}
|
||||
}
|
||||
pathOnly = strings.TrimSuffix(pathOnly, "/")
|
||||
if pathOnly == "" {
|
||||
pathOnly = "/"
|
||||
}
|
||||
brand := siteName(cfg)
|
||||
if !strings.Contains(name, brand) && pathOnly != "/" {
|
||||
name = name + " - " + brand
|
||||
}
|
||||
desc := pageDescription(data, cfg, pathOnly)
|
||||
if desc == "" {
|
||||
desc = brandName + "短剧在线观看。"
|
||||
}
|
||||
typ := "WebPage"
|
||||
switch {
|
||||
case isCollectionPath(pathOnly):
|
||||
typ = "CollectionPage"
|
||||
case pathOnly == "/about-us" || pathOnly == "/aboutUs":
|
||||
typ = "AboutPage"
|
||||
}
|
||||
return contentWebPageNode(siteURL, pageURL, name, desc, typ)
|
||||
}
|
||||
|
||||
func siteName(cfg configser.AppConfigVM) string {
|
||||
return firstNonEmpty(cfg.SeoInfo.WebSiteName, brandName)
|
||||
}
|
||||
|
||||
func contentWebPageNode(siteURL, pageURL, name, desc, typ string) ldThing {
|
||||
return ldThing{
|
||||
"@type": typ,
|
||||
"@id": pageURL + "#webpage",
|
||||
"name": name,
|
||||
"description": desc,
|
||||
"url": pageURL,
|
||||
"inLanguage": "zh-CN",
|
||||
"isPartOf": ldThing{"@id": siteURL + "/#website"},
|
||||
"publisher": ldThing{"@id": siteURL + "/#organization"},
|
||||
}
|
||||
}
|
||||
|
||||
func isCollectionPath(path string) bool {
|
||||
for _, prefix := range []string{"/list", "/series", "/rank", "/popular-recommend", "/recently-added", "/topic", "/topic-detail", "/tags", "/category"} {
|
||||
if path == prefix || strings.HasPrefix(path, prefix+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return path == "/application" || path == "/nav"
|
||||
}
|
||||
|
||||
// itemListNode 为首页及列表型页面输出当前 SSR 首屏实际展示的站内详情链接。
|
||||
// Google 的 summary-page ItemList 只需要 position 与唯一的站内详情 URL。
|
||||
func itemListNode(siteURL, pageURL string, data map[string]any) ldThing {
|
||||
urls := make([]string, 0, 24)
|
||||
appendPlaylet := func(slug string, id int64) {
|
||||
path := playletser.PlayPath(slug, id)
|
||||
if path != "#" {
|
||||
urls = append(urls, AbsURL(siteURL, normalizePageURL(path)))
|
||||
}
|
||||
}
|
||||
|
||||
if list, ok := data["list"].([]playletser.CommonData); ok {
|
||||
for _, item := range list {
|
||||
appendPlaylet(item.Slug, item.ID)
|
||||
}
|
||||
}
|
||||
if list, ok := data["list"].([]videoser.VideoAllListItemResp); ok {
|
||||
for _, item := range list {
|
||||
if path := videoser.VideoPlayURL(item.Slug, item.ID); path != "/series" {
|
||||
urls = append(urls, AbsURL(siteURL, path))
|
||||
}
|
||||
}
|
||||
}
|
||||
if list, ok := data["list"].([]playletser.VideoItemResp); ok {
|
||||
for _, item := range list {
|
||||
if path := videoser.VideoPlayURL(item.Slug, item.ID); path != "/series" {
|
||||
urls = append(urls, AbsURL(siteURL, path))
|
||||
}
|
||||
}
|
||||
}
|
||||
if categories, ok := data["homeCategoryData"].([]playletser.HomeCategoryData); ok {
|
||||
for _, category := range categories {
|
||||
for _, item := range category.List {
|
||||
appendPlaylet(item.Slug, item.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
if banners, ok := data["homeBannerData"].([]playletser.CommonData); ok {
|
||||
for _, item := range banners {
|
||||
appendPlaylet(item.Slug, item.ID)
|
||||
}
|
||||
}
|
||||
if themes, ok := data["list"].([]playletser.PlayletThemeItem); ok {
|
||||
for _, item := range themes {
|
||||
path := playletser.RecommendCanonicalPath(item.ID, item.Slug)
|
||||
urls = append(urls, AbsURL(siteURL, normalizePageURL(path)))
|
||||
}
|
||||
}
|
||||
// 福利应用页是一个可见的应用集合;使用页面实际渲染的链接生成通用
|
||||
// Schema.org ItemList。应用链接为外站落地页,因此不宣称具备 Google
|
||||
// 站内轮播资格,只帮助搜索引擎理解页面集合语义。
|
||||
if categories, ok := data["categories"].([]applistser.CategoryVM); ok {
|
||||
for _, category := range categories {
|
||||
for _, app := range category.Apps {
|
||||
appURL := structuredURL(siteURL, app.URL)
|
||||
if appURL != "" {
|
||||
urls = append(urls, appURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{}, len(urls))
|
||||
unique := make([]string, 0, len(urls))
|
||||
for _, itemURL := range urls {
|
||||
itemURL = strings.TrimSpace(itemURL)
|
||||
if itemURL == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[itemURL]; exists {
|
||||
continue
|
||||
}
|
||||
seen[itemURL] = struct{}{}
|
||||
unique = append(unique, itemURL)
|
||||
}
|
||||
if len(unique) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
start := 1
|
||||
offset := intFrom(data["itemListOffset"])
|
||||
if offset == 0 {
|
||||
offset = intFrom(data["rankOffset"])
|
||||
}
|
||||
if offset > 0 {
|
||||
start += offset
|
||||
}
|
||||
items := make([]any, 0, len(unique))
|
||||
for i, itemURL := range unique {
|
||||
items = append(items, ldThing{
|
||||
"@type": "ListItem",
|
||||
"position": start + i,
|
||||
"url": itemURL,
|
||||
})
|
||||
}
|
||||
return ldThing{
|
||||
"@type": "ItemList",
|
||||
"@id": pageURL + "#itemlist",
|
||||
"numberOfItems": len(items),
|
||||
"itemListElement": items,
|
||||
}
|
||||
}
|
||||
|
||||
func faqPageNode(siteURL, pageURL string, data map[string]any) ldThing {
|
||||
faqItems, ok := data["faqItems"].([]FAQItem)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
questions := make([]any, 0, len(faqItems))
|
||||
for _, item := range faqItems {
|
||||
question := strings.TrimSpace(item.Question)
|
||||
answer := strings.TrimSpace(item.Answer)
|
||||
if question == "" || answer == "" {
|
||||
continue
|
||||
}
|
||||
questions = append(questions, ldThing{
|
||||
"@type": "Question",
|
||||
"name": question,
|
||||
"acceptedAnswer": ldThing{
|
||||
"@type": "Answer",
|
||||
"text": answer,
|
||||
},
|
||||
})
|
||||
}
|
||||
if len(questions) == 0 {
|
||||
return nil
|
||||
}
|
||||
return ldThing{
|
||||
"@type": "FAQPage",
|
||||
"@id": pageURL + "#faq",
|
||||
"url": pageURL,
|
||||
"inLanguage": "zh-CN",
|
||||
"isPartOf": ldThing{"@id": pageURL + "#webpage"},
|
||||
"mainEntity": questions,
|
||||
"publisher": ldThing{"@id": siteURL + "/#organization"},
|
||||
}
|
||||
}
|
||||
|
||||
func breadcrumb(siteURL string, items [][2]string) ldThing {
|
||||
list := make([]any, 0, len(items))
|
||||
for i, item := range items {
|
||||
name, loc := strings.TrimSpace(item[0]), strings.TrimSpace(item[1])
|
||||
if name == "" || loc == "" {
|
||||
continue
|
||||
}
|
||||
list = append(list, ldThing{
|
||||
"@type": "ListItem",
|
||||
"position": i + 1,
|
||||
"name": name,
|
||||
"item": loc,
|
||||
})
|
||||
}
|
||||
pageURL := ""
|
||||
if len(items) > 0 {
|
||||
pageURL = strings.TrimSpace(items[len(items)-1][1])
|
||||
}
|
||||
return ldThing{
|
||||
"@type": "BreadcrumbList",
|
||||
"@id": pageURL + "#breadcrumb",
|
||||
"itemListElement": list,
|
||||
}
|
||||
}
|
||||
|
||||
func genericBreadcrumb(siteURL, pageURL string, data map[string]any) ldThing {
|
||||
if pageURL == "" || pageURL == AbsURL(siteURL, "/") {
|
||||
return nil
|
||||
}
|
||||
path := pagePath(pageURL)
|
||||
if !hasVisibleBreadcrumb(path) {
|
||||
return nil
|
||||
}
|
||||
name := firstNonEmpty(strFrom(data["pageTitle"]), strFrom(data["pageName"]), strFrom(data["title"]), "内容页")
|
||||
if strFrom(data["pageKey"]) == "topic" && strings.HasPrefix(path, "/topic-detail/") {
|
||||
return breadcrumb(siteURL, [][2]string{
|
||||
{"首页", AbsURL(siteURL, "/")},
|
||||
{"专题", AbsURL(siteURL, "/topic")},
|
||||
{name, pageURL},
|
||||
})
|
||||
}
|
||||
return breadcrumb(siteURL, [][2]string{
|
||||
{"首页", AbsURL(siteURL, "/")},
|
||||
{name, pageURL},
|
||||
})
|
||||
}
|
||||
|
||||
func hasVisibleBreadcrumb(path string) bool {
|
||||
for _, prefix := range []string{"/list", "/series", "/rank", "/popular-recommend", "/recently-added", "/topic", "/topic-detail", "/tags", "/category", "/richtext", "/application"} {
|
||||
if path == prefix || strings.HasPrefix(path, prefix+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
switch path {
|
||||
case "/home-road", "/about", "/terms", "/privacy", "/help", "/2257", "/dmca":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func structuredURL(siteURL, raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || raw == "#" {
|
||||
return ""
|
||||
}
|
||||
if strings.HasPrefix(raw, "/") {
|
||||
return AbsURL(siteURL, raw)
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") {
|
||||
return ""
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func pagePath(pageURL string) string {
|
||||
u, err := url.Parse(strings.TrimSpace(pageURL))
|
||||
if err == nil && u.Path != "" {
|
||||
return strings.TrimSuffix(u.Path, "/")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func pageDescription(data map[string]any, cfg configser.AppConfigVM, path string) string {
|
||||
name := siteName(cfg)
|
||||
if page, ok := asRichText(data["richtext"]); ok && page.Found {
|
||||
return firstNonEmpty(page.Subtitle, page.Title+" - "+name+"官方内容。")
|
||||
}
|
||||
switch seo := data["seoInfo"].(type) {
|
||||
case playletser.SeoInfo:
|
||||
if desc := strings.TrimSpace(seo.Description); desc != "" {
|
||||
return desc
|
||||
}
|
||||
case *playletser.SeoInfo:
|
||||
if seo != nil {
|
||||
if desc := strings.TrimSpace(seo.Description); desc != "" {
|
||||
return desc
|
||||
}
|
||||
}
|
||||
}
|
||||
if desc := firstNonEmpty(
|
||||
strFrom(data["topicDescription"]),
|
||||
strFrom(data["tagDescription"]),
|
||||
strFrom(data["categoryDescription"]),
|
||||
); desc != "" {
|
||||
return desc
|
||||
}
|
||||
if path == "/" {
|
||||
return firstNonEmpty(strings.TrimSpace(cfg.SeoInfo.Description), name+"汇集热门短剧、今日更新与精选推荐,免费在线观看高清短剧。")
|
||||
}
|
||||
if path == "/nav" {
|
||||
return firstNonEmpty(strings.TrimSpace(cfg.SeoNavInfo.DhDescription), name+"官方导航页,提供最新访问线路、永久地址、APP 下载与官方联系方式。")
|
||||
}
|
||||
for prefix, desc := range map[string]string{
|
||||
"/list": "浏览" + name + "全部短剧列表,按分类、标签筛选海量高清短剧,发现更多精彩剧集。",
|
||||
"/series": name + "精选AI成人剧集,持续更新热门漫剧、独家原创剧集,支持免费在线观看。",
|
||||
"/rank": name + "排行榜实时更新热播短剧,查看热门榜、飙升榜。",
|
||||
"/popular-recommend": name + "热播榜根据播放、收藏与解锁综合排序,查看热播、新剧与追剧榜。",
|
||||
"/recently-added": name + "今日更新,第一时间追更最新上线短剧与连载剧集。",
|
||||
"/topic": name + "精选短剧专题与专题作品,方便按主题发现内容。",
|
||||
"/topic-detail": name + "精选短剧专题与专题作品,方便按主题发现内容。",
|
||||
"/tags": name + "标签内容页,按题材标签浏览相关短剧。",
|
||||
"/category": name + "分类内容页,按内容分类浏览最新上架短剧。",
|
||||
"/richtext": name + "官方说明与帮助内容。",
|
||||
} {
|
||||
if path == prefix || strings.HasPrefix(path, prefix+"/") {
|
||||
return desc
|
||||
}
|
||||
}
|
||||
switch path {
|
||||
case "/application":
|
||||
return name + "福利应用中心,精选实用应用与福利入口。"
|
||||
case "/download-app":
|
||||
return "下载" + name + "APP,随时随地追看热门短剧。"
|
||||
case "/home-road":
|
||||
return name + "官方最新地址发布页,实时同步国内可用线路、永久入口与防失联联系方式。"
|
||||
case "/about-us", "/aboutUs":
|
||||
return "了解" + name + "的品牌、产品与服务。"
|
||||
case "/user-agreement", "/userAgreement":
|
||||
return name + "用户协议,说明使用平台服务的权利与义务。"
|
||||
case "/privacy-policy", "/privacyPolicy":
|
||||
return name + "隐私政策,说明个人信息的收集、使用与保护方式。"
|
||||
case "/help-center", "/helpCenter":
|
||||
return name + "帮助中心,提供账号、观影、充值与常见问题解答。"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func intFrom(v any) int {
|
||||
switch n := v.(type) {
|
||||
case int:
|
||||
return n
|
||||
case int32:
|
||||
return int(n)
|
||||
case int64:
|
||||
return int(n)
|
||||
case float64:
|
||||
return int(n)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func playletPath(route, slug string, id int64) string {
|
||||
key := strings.TrimSpace(slug)
|
||||
if key == "" && id > 0 {
|
||||
key = strconv.FormatInt(id, 10)
|
||||
}
|
||||
if key == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimRight(route, "/") + "/" + url.PathEscape(key)
|
||||
}
|
||||
|
||||
func chapterSEODesc(ch *playletser.PlayletChapterInfoResp) string {
|
||||
if ch == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(ch.SeoInfo.Description)
|
||||
}
|
||||
|
||||
func asPlaylet(v any) (*playletser.PlayletInfoResp, bool) {
|
||||
switch t := v.(type) {
|
||||
case *playletser.PlayletInfoResp:
|
||||
if t != nil {
|
||||
return t, true
|
||||
}
|
||||
case playletser.PlayletInfoResp:
|
||||
return &t, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func asChapter(v any) (*playletser.PlayletChapterInfoResp, bool) {
|
||||
switch t := v.(type) {
|
||||
case *playletser.PlayletChapterInfoResp:
|
||||
if t != nil {
|
||||
return t, true
|
||||
}
|
||||
case playletser.PlayletChapterInfoResp:
|
||||
return &t, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func asRichText(v any) (configser.RichTextPageVM, bool) {
|
||||
switch t := v.(type) {
|
||||
case configser.RichTextPageVM:
|
||||
return t, true
|
||||
case *configser.RichTextPageVM:
|
||||
if t != nil {
|
||||
return *t, true
|
||||
}
|
||||
}
|
||||
return configser.RichTextPageVM{}, false
|
||||
}
|
||||
|
||||
func absMedia(siteURL, raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
return AbsURL(siteURL, raw)
|
||||
}
|
||||
|
||||
// videoContentURL 仅接受可定位真实媒体字节的 HTTP(S) 或站内绝对路径。
|
||||
// 诸如哈希、播放器状态值等上游字段不能误标为 Google VideoObject.contentUrl。
|
||||
func videoContentURL(siteURL, raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
if strings.HasPrefix(raw, "/") {
|
||||
return AbsURL(siteURL, raw)
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
|
||||
return ""
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func isoDate(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || strings.HasPrefix(raw, "0001") || strings.HasPrefix(raw, "1970-01-01") {
|
||||
return ""
|
||||
}
|
||||
layouts := []string{
|
||||
time.RFC3339,
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02",
|
||||
"2006/01/02 15:04:05",
|
||||
"2006/01/02",
|
||||
}
|
||||
for _, layout := range layouts {
|
||||
if t, err := time.ParseInLocation(layout, raw, time.Local); err == nil {
|
||||
return t.Format("2006-01-02")
|
||||
}
|
||||
}
|
||||
if len(raw) >= 10 {
|
||||
return raw[:10]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// isoDateTime 为 Google VideoObject.uploadDate 生成带时区的 ISO 8601 DateTime。
|
||||
// 上游无时区的中文站点时间按 UTC+8 解释,避免让 Google 自行猜测时区。
|
||||
func isoDateTime(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || strings.HasPrefix(raw, "0001") || strings.HasPrefix(raw, "1970-01-01") {
|
||||
return ""
|
||||
}
|
||||
if parsed, err := time.Parse(time.RFC3339, raw); err == nil {
|
||||
return parsed.Format(time.RFC3339)
|
||||
}
|
||||
chinaTime := time.FixedZone("UTC+8", 8*60*60)
|
||||
for _, layout := range []string{
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02T15:04:05",
|
||||
"2006/01/02 15:04:05",
|
||||
"2006-01-02",
|
||||
"2006/01/02",
|
||||
} {
|
||||
if parsed, err := time.ParseInLocation(layout, raw, chinaTime); err == nil {
|
||||
return parsed.Format(time.RFC3339)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func uniqueStrings(in []string) []string {
|
||||
seen := map[string]struct{}{}
|
||||
out := make([]string, 0, len(in))
|
||||
for _, s := range in {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[s]; ok {
|
||||
continue
|
||||
}
|
||||
seen[s] = struct{}{}
|
||||
out = append(out, s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func firstNonEmpty(vals ...string) string {
|
||||
for _, v := range vals {
|
||||
if s := strings.TrimSpace(v); s != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func strFrom(v any) string {
|
||||
s, _ := v.(string)
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"videoweb/app/service/configser"
|
||||
)
|
||||
|
||||
const fallbackLLMSSiteName = "黄果短剧"
|
||||
|
||||
// BuildLLMSTXT 生成面向 AI 抓取工具的简洁站点说明与主要规范链接。
|
||||
func BuildLLMSTXT(siteURL string, cfg configser.AppConfigVM) string {
|
||||
base := strings.TrimRight(strings.TrimSpace(siteURL), "/")
|
||||
name := oneLine(cfg.SeoInfo.WebSiteName)
|
||||
if name == "" {
|
||||
name = fallbackLLMSSiteName
|
||||
}
|
||||
description := oneLine(cfg.SeoInfo.Description)
|
||||
if description == "" {
|
||||
description = "汇集热门短剧、今日更新与精选推荐,提供高清短剧在线观看。"
|
||||
}
|
||||
|
||||
links := []struct {
|
||||
label string
|
||||
path string
|
||||
}{
|
||||
{label: "首页", path: "/"},
|
||||
{label: "短剧列表", path: "/list"},
|
||||
{label: "剧集", path: "/series"},
|
||||
{label: "热门推荐", path: "/popular-recommend"},
|
||||
{label: "今日更新", path: "/recently-added"},
|
||||
{label: "专题", path: "/topic"},
|
||||
}
|
||||
|
||||
var body strings.Builder
|
||||
body.WriteString("# ")
|
||||
body.WriteString(name)
|
||||
body.WriteString("\n\n> ")
|
||||
body.WriteString(description)
|
||||
body.WriteString("\n\n## 核心页面\n\n")
|
||||
for _, link := range links {
|
||||
body.WriteString("- [")
|
||||
body.WriteString(link.label)
|
||||
body.WriteString("](")
|
||||
body.WriteString(AbsURL(base, link.path))
|
||||
body.WriteString(")\n")
|
||||
}
|
||||
body.WriteString("\n## 站点发现\n\n")
|
||||
body.WriteString("- [Sitemap](")
|
||||
body.WriteString(AbsURL(base, "/sitemap.xml"))
|
||||
body.WriteString(")\n- [Robots](")
|
||||
body.WriteString(AbsURL(base, "/robots.txt"))
|
||||
body.WriteString(")\n")
|
||||
return body.String()
|
||||
}
|
||||
|
||||
func oneLine(value string) string {
|
||||
return strings.Join(strings.Fields(value), " ")
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
templateassets "videoweb/app/template"
|
||||
)
|
||||
|
||||
// BuildRobotsTXT 动态 robots.txt:优先读取当前项目模板文件,并把 Sitemap 写成绝对地址。
|
||||
func BuildRobotsTXT(siteURL string) string {
|
||||
sitemap := AbsURL(siteURL, "/sitemap.xml")
|
||||
body := defaultRobotsTXT
|
||||
loadedFromDisk := false
|
||||
for _, path := range []string{
|
||||
"template/pages/robots.txt",
|
||||
"templates/pages/robots.txt",
|
||||
"app/template/pages/robots.txt",
|
||||
"app/templates/pages/robots.txt",
|
||||
} {
|
||||
if raw, err := os.ReadFile(path); err == nil {
|
||||
if t := strings.TrimSpace(string(raw)); t != "" {
|
||||
body = t
|
||||
loadedFromDisk = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !loadedFromDisk {
|
||||
if raw, err := templateassets.FS.ReadFile("pages/robots.txt"); err == nil {
|
||||
if t := strings.TrimSpace(string(raw)); t != "" {
|
||||
body = t
|
||||
}
|
||||
}
|
||||
}
|
||||
body = strings.ReplaceAll(body, "\r\n", "\n")
|
||||
body = sitemapLineRe.ReplaceAllString(body, "Sitemap: "+sitemap)
|
||||
if !strings.Contains(body, "Sitemap:") {
|
||||
body = strings.TrimRight(body, "\n") + "\n\nSitemap: " + sitemap + "\n"
|
||||
} else if !strings.HasSuffix(body, "\n") {
|
||||
body += "\n"
|
||||
}
|
||||
if host := hostFromSiteURL(siteURL); host != "" && !strings.Contains(body, "Host:") {
|
||||
body = strings.TrimRight(body, "\n") + "\nHost: " + host + "\n"
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func hostFromSiteURL(siteURL string) string {
|
||||
u, err := url.Parse(strings.TrimSpace(siteURL))
|
||||
if err != nil || u.Host == "" {
|
||||
return ""
|
||||
}
|
||||
return u.Host
|
||||
}
|
||||
|
||||
var sitemapLineRe = regexp.MustCompile(`(?m)^Sitemap:\s*\S+`)
|
||||
|
||||
const defaultRobotsTXT = `User-agent: *
|
||||
Allow: /
|
||||
|
||||
# 非 HTML 接口与工具路由不抓取
|
||||
Disallow: /api/
|
||||
Disallow: /ui/
|
||||
Disallow: /healthz
|
||||
Disallow: /cdn-cgi/
|
||||
Disallow: /h5/
|
||||
Disallow: /enkey
|
||||
Disallow: /newenkey
|
||||
|
||||
# 登录、搜索和账户 HTML 页面允许抓取,以便爬虫读取页面内 noindex。
|
||||
|
||||
Sitemap: /sitemap.xml
|
||||
`
|
||||
@@ -0,0 +1,226 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"videoweb/app/service/configser"
|
||||
"videoweb/app/service/videoser"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ResolveSiteURL 解析对外站点根地址(无尾斜杠)。
|
||||
// 优先有效正式域名;跳过占位域名后回退到当前请求 Host。
|
||||
func ResolveSiteURL(c *gin.Context, cfg configser.AppConfigVM) string {
|
||||
if siteURL := PrimarySiteURL(cfg); siteURL != "" {
|
||||
return siteURL
|
||||
}
|
||||
if c != nil && c.Request != nil {
|
||||
return requestOrigin(c.Request)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// PrimarySiteURL 返回配置接口动态下发的永久地址,供 canonical、OG 等站点元数据使用。
|
||||
// 页面是否允许索引由页面类型和内容状态决定,与请求 Host 是否匹配此地址无关。
|
||||
func PrimarySiteURL(cfg configser.AppConfigVM) string {
|
||||
return usableSiteURL(cfg.ForeverURLAddr)
|
||||
}
|
||||
|
||||
func usableSiteURL(raw string) string {
|
||||
u := sanitizeBase(raw)
|
||||
if u == "" {
|
||||
return ""
|
||||
}
|
||||
host := strings.ToLower(originHost(u))
|
||||
if isPlaceholderHost(host) {
|
||||
return ""
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func originHost(raw string) string {
|
||||
u, err := url.Parse(strings.TrimSpace(raw))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return u.Hostname()
|
||||
}
|
||||
|
||||
func isPlaceholderHost(host string) bool {
|
||||
host = strings.TrimSpace(strings.ToLower(host))
|
||||
switch host {
|
||||
case "", "localhost", "127.0.0.1", "0.0.0.0", "www.xx.xxx", "xx.xxx", "example.com", "www.example.com":
|
||||
return true
|
||||
}
|
||||
return strings.HasSuffix(host, ".example.com") || strings.HasSuffix(host, ".local")
|
||||
}
|
||||
|
||||
// AbsURL 将 path 或完整 URL 转为绝对地址。
|
||||
func AbsURL(siteURL, raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
if siteURL == "" {
|
||||
return "/"
|
||||
}
|
||||
return siteURL + "/"
|
||||
}
|
||||
if strings.HasPrefix(raw, "http://") || strings.HasPrefix(raw, "https://") {
|
||||
return raw
|
||||
}
|
||||
if siteURL == "" {
|
||||
if strings.HasPrefix(raw, "/") {
|
||||
return raw
|
||||
}
|
||||
return "/" + raw
|
||||
}
|
||||
if strings.HasPrefix(raw, "/") {
|
||||
return siteURL + raw
|
||||
}
|
||||
return siteURL + "/" + raw
|
||||
}
|
||||
|
||||
// PageCanonicalURL 首页用 siteURL 根路径;其他页使用当前 router 的无尾斜杠规范路径。
|
||||
// siteURL 为空时回退 foreverURLAddr。
|
||||
func PageCanonicalURL(siteURL, foreverURLAddr string, c *gin.Context) string {
|
||||
base := strings.TrimRight(strings.TrimSpace(siteURL), "/")
|
||||
if base == "" {
|
||||
base = strings.TrimRight(strings.TrimSpace(usableSiteURL(foreverURLAddr)), "/")
|
||||
}
|
||||
if c == nil || c.Request == nil || c.Request.URL == nil {
|
||||
return AbsURL(base, "/")
|
||||
}
|
||||
path := c.Request.URL.Path
|
||||
if path == "" || path == "/" {
|
||||
return AbsURL(base, "/")
|
||||
}
|
||||
return normalizePageURL(AbsURL(base, relativeRequestURI(c.Request.URL)))
|
||||
}
|
||||
|
||||
// CanonicalWithoutQuery 移除 canonical 中的查询参数与片段,避免参数变体形成重复 URL。
|
||||
func CanonicalWithoutQuery(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return raw
|
||||
}
|
||||
u.RawQuery = ""
|
||||
u.ForceQuery = false
|
||||
u.Fragment = ""
|
||||
return u.String()
|
||||
}
|
||||
|
||||
// ContentCanonicalURL 由实际内容生成 slug 路由。
|
||||
// 短剧规范入口是 /play/{slug},与 sitemap-playletdetail 一致;各集页 noindex,不能标成 canonical。
|
||||
func ContentCanonicalURL(siteURL, fallback string, data map[string]any) string {
|
||||
if detail, ok := asPlaylet(data["detail"]); ok {
|
||||
if path := playletPath("/play", detail.Slug, detail.ID); path != "" {
|
||||
return AbsURL(siteURL, path)
|
||||
}
|
||||
}
|
||||
if playlet, ok := asPlaylet(data["playlet"]); ok {
|
||||
if path := playletPath("/play", playlet.Slug, playlet.ID); path != "" {
|
||||
return AbsURL(siteURL, path)
|
||||
}
|
||||
}
|
||||
if video, ok := asVideo(data["video"]); ok {
|
||||
if path := videoser.VideoPlayURL(video.Slug, video.ID); path != "/series" {
|
||||
return AbsURL(siteURL, path)
|
||||
}
|
||||
}
|
||||
if chapter, ok := asChapter(data["chapter"]); ok {
|
||||
if path := playletPath("/play", "", chapter.PlayletID); path != "" {
|
||||
return AbsURL(siteURL, path)
|
||||
}
|
||||
}
|
||||
return normalizePageURL(fallback)
|
||||
}
|
||||
|
||||
func asVideo(v any) (*videoser.VideoInfoResp, bool) {
|
||||
switch value := v.(type) {
|
||||
case *videoser.VideoInfoResp:
|
||||
if value != nil {
|
||||
return value, true
|
||||
}
|
||||
case videoser.VideoInfoResp:
|
||||
return &value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// normalizePageURL 去掉非首页 URL 的尾斜杠,并保留查询参数。
|
||||
func normalizePageURL(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return raw
|
||||
}
|
||||
if u.Path == "" {
|
||||
u.Path = "/"
|
||||
}
|
||||
if u.Path != "/" {
|
||||
u.Path = strings.TrimRight(u.Path, "/")
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func relativeRequestURI(u *url.URL) string {
|
||||
if u == nil {
|
||||
return "/"
|
||||
}
|
||||
path := u.EscapedPath()
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
if u.RawQuery != "" {
|
||||
return path + "?" + u.RawQuery
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func sanitizeBase(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
||||
if !strings.Contains(raw, "://") && strings.Contains(raw, ".") {
|
||||
return sanitizeBase("https://" + strings.TrimPrefix(raw, "/"))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
if u.Scheme != "http" && u.Scheme != "https" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimRight(u.Scheme+"://"+u.Host, "/")
|
||||
}
|
||||
|
||||
func requestOrigin(r *http.Request) string {
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
proto := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto"))
|
||||
if proto == "" {
|
||||
if r.TLS != nil {
|
||||
proto = "https"
|
||||
} else {
|
||||
proto = "http"
|
||||
}
|
||||
}
|
||||
if i := strings.IndexByte(proto, ','); i >= 0 {
|
||||
proto = strings.TrimSpace(proto[:i])
|
||||
}
|
||||
host := strings.TrimSpace(r.Header.Get("X-Forwarded-Host"))
|
||||
if host == "" {
|
||||
host = strings.TrimSpace(r.Host)
|
||||
}
|
||||
if i := strings.IndexByte(host, ','); i >= 0 {
|
||||
host = strings.TrimSpace(host[:i])
|
||||
}
|
||||
if host == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimRight(proto+"://"+host, "/")
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
"videoweb/app/appg"
|
||||
)
|
||||
|
||||
// DefaultIndexNowKey 与 hgdj_server / 其它站共用的 IndexNow 密钥。
|
||||
// 对应站点根路径 /{key}.txt,内容必须等于 key 本身。
|
||||
const DefaultIndexNowKey = "6d41643810d540ce924edb982c3d3171"
|
||||
|
||||
// VerifyCodes 站长平台所有权验证(meta + 根路径文件)。
|
||||
type VerifyCodes struct {
|
||||
Baidu string
|
||||
BaiduHTML string
|
||||
IndexNowKey string
|
||||
}
|
||||
|
||||
// VerifyFromConfig 读取 yaml 中的验证码与验证文件名。
|
||||
func VerifyFromConfig() VerifyCodes {
|
||||
seo := appg.Conf.SEO
|
||||
key := sanitizeIndexNowKey(seo.IndexNowKey)
|
||||
if key == "" {
|
||||
key = DefaultIndexNowKey
|
||||
}
|
||||
return VerifyCodes{
|
||||
Baidu: strings.TrimSpace(seo.BaiduSiteVerification),
|
||||
BaiduHTML: sanitizeVerifyFile(seo.BaiduHTMLFile),
|
||||
IndexNowKey: key,
|
||||
}
|
||||
}
|
||||
|
||||
func sanitizeVerifyFile(raw string) string {
|
||||
raw = strings.TrimSpace(strings.ReplaceAll(raw, "\\", "/"))
|
||||
raw = path.Base(raw)
|
||||
if raw == "" || raw == "." || raw == "/" || strings.Contains(raw, "..") {
|
||||
return ""
|
||||
}
|
||||
lower := strings.ToLower(raw)
|
||||
if strings.HasSuffix(lower, ".html") || strings.HasSuffix(lower, ".htm") || strings.HasSuffix(lower, ".xml") {
|
||||
return raw
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func sanitizeIndexNowKey(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
for _, r := range raw {
|
||||
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '-' || r == '_' {
|
||||
continue
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return raw
|
||||
}
|
||||
Reference in New Issue
Block a user