@@ -0,0 +1,37 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsIndexablePath 是否为需要收录的公开页(与 robots 可抓取、页面 index,follow 对齐)。
|
||||
func IsIndexablePath(raw string) bool {
|
||||
path := strings.TrimSpace(raw)
|
||||
if path == "" {
|
||||
return true
|
||||
}
|
||||
if u, err := url.Parse(path); err == nil && u.Path != "" {
|
||||
path = u.Path
|
||||
}
|
||||
path = strings.TrimSuffix(path, "/")
|
||||
if path == "" {
|
||||
return true
|
||||
}
|
||||
switch path {
|
||||
case "/list", "/series", "/rank", "/today-update", "/topic", "/tag", "/category",
|
||||
"/download-app", "/application", "/home-road", "/detail", "/play", "/nav",
|
||||
"/about-us", "/aboutUs", "/help-center", "/helpCenter":
|
||||
return true
|
||||
default:
|
||||
return strings.HasPrefix(path, "/list/") ||
|
||||
strings.HasPrefix(path, "/category/") ||
|
||||
strings.HasPrefix(path, "/series/") ||
|
||||
strings.HasPrefix(path, "/topic/") ||
|
||||
strings.HasPrefix(path, "/tag/") ||
|
||||
strings.HasPrefix(path, "/rank/") ||
|
||||
strings.HasPrefix(path, "/today-update/") ||
|
||||
strings.HasPrefix(path, "/detail/") ||
|
||||
strings.HasPrefix(path, "/play/")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,862 @@
|
||||
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"
|
||||
)
|
||||
|
||||
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 = ensureTrailingSlash(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 {
|
||||
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 {
|
||||
logo := AbsURL(siteURL, "/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": 512,
|
||||
"height": 512,
|
||||
},
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func websiteNode(siteURL string, cfg configser.AppConfigVM) ldThing {
|
||||
desc := strings.TrimSpace(cfg.SeoInfo.Description)
|
||||
if desc == "" {
|
||||
desc = "黄果短剧汇集热门短剧、今日更新与精选推荐,免费在线观看高清短剧。"
|
||||
}
|
||||
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("/detail", 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("/detail", "", 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 seriesID != "" {
|
||||
video["partOfSeries"] = ldThing{"@id": seriesID}
|
||||
} else if playletID > 0 {
|
||||
video["partOfSeries"] = ldThing{"@type": "TVSeries", "name": playletTitle, "url": detailURL}
|
||||
}
|
||||
if episode > 0 {
|
||||
video["episodeNumber"] = episode
|
||||
}
|
||||
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 {
|
||||
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 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 = brandName
|
||||
}
|
||||
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", "/today-update", "/topic", "/tag", "/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.DetailPath(slug, id)
|
||||
if path != "#" {
|
||||
urls = append(urls, AbsURL(siteURL, path))
|
||||
}
|
||||
}
|
||||
|
||||
if list, ok := data["list"].([]playletser.CommonData); ok {
|
||||
for _, item := range list {
|
||||
appendPlaylet(item.Slug, item.ID)
|
||||
}
|
||||
}
|
||||
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.HomeBannerData); ok {
|
||||
for _, item := range banners {
|
||||
if itemURL := structuredURL(siteURL, item.URL); itemURL != "" {
|
||||
urls = append(urls, itemURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
if themes, ok := data["list"].([]playletser.PlayletThemeItem); ok {
|
||||
for _, item := range themes {
|
||||
path := playletser.RecommendCanonicalPath(item.ID, item.Slug)
|
||||
urls = append(urls, AbsURL(siteURL, 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/") {
|
||||
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", "/today-update", "/topic", "/tag", "/category", "/richtext", "/application"} {
|
||||
if path == prefix || strings.HasPrefix(path, prefix+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
switch path {
|
||||
case "/home-road", "/about-us", "/aboutUs", "/user-agreement", "/userAgreement",
|
||||
"/privacy-policy", "/privacyPolicy", "/help-center", "/helpCenter":
|
||||
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 {
|
||||
if page, ok := asRichText(data["richtext"]); ok && page.Found {
|
||||
return firstNonEmpty(page.Subtitle, page.Title+" - 黄果短剧官方内容。")
|
||||
}
|
||||
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), "黄果短剧汇集热门短剧、今日更新与精选推荐,免费在线观看高清短剧。")
|
||||
}
|
||||
if path == "/nav" {
|
||||
return firstNonEmpty(strings.TrimSpace(cfg.SeoNavInfo.DhDescription), "黄果短剧官方导航页,提供最新访问线路、永久地址、APP 下载与官方联系方式。")
|
||||
}
|
||||
for prefix, desc := range map[string]string{
|
||||
"/list": "浏览黄果短剧全部短剧列表,按分类、标签筛选海量高清短剧,发现更多精彩剧集。",
|
||||
"/series": "黄果短剧精选AI成人剧集,持续更新热门漫剧、独家原创剧集,支持免费在线观看。",
|
||||
"/rank": "黄果短剧排行榜实时更新热播短剧,查看热门榜、飙升榜。",
|
||||
"/today-update": "黄果短剧今日更新,第一时间追更最新上线短剧与连载剧集。",
|
||||
"/topic": "黄果短剧精选短剧专题与专题作品,方便按主题发现内容。",
|
||||
"/tag": "黄果短剧标签内容页,按题材标签浏览相关短剧。",
|
||||
"/category": "黄果短剧分类内容页,按内容分类浏览最新上架短剧。",
|
||||
"/richtext": "黄果短剧官方说明与帮助内容。",
|
||||
} {
|
||||
if path == prefix || strings.HasPrefix(path, prefix+"/") {
|
||||
return desc
|
||||
}
|
||||
}
|
||||
switch path {
|
||||
case "/application":
|
||||
return "黄果短剧福利应用中心,精选实用应用与福利入口。"
|
||||
case "/download-app":
|
||||
return "下载黄果短剧APP,随时随地追看热门短剧。"
|
||||
case "/home-road":
|
||||
return "黄果短剧官方最新地址发布页,实时同步国内可用线路、永久入口与防失联联系方式。"
|
||||
case "/about-us", "/aboutUs":
|
||||
return "了解黄果短剧的品牌、产品与服务。"
|
||||
case "/user-agreement", "/userAgreement":
|
||||
return "黄果短剧用户协议,说明使用平台服务的权利与义务。"
|
||||
case "/privacy-policy", "/privacyPolicy":
|
||||
return "黄果短剧隐私政策,说明个人信息的收集、使用与保护方式。"
|
||||
case "/help-center", "/helpCenter":
|
||||
return "黄果短剧帮助中心,提供账号、观影、充值与常见问题解答。"
|
||||
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,215 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"testing"
|
||||
"videoweb/app/service/configser"
|
||||
"videoweb/app/service/playletser"
|
||||
)
|
||||
|
||||
func TestPageJSONLD_DetailIsValidJSON(t *testing.T) {
|
||||
detail := &playletser.PlayletInfoResp{
|
||||
ID: 12,
|
||||
Slug: "test-show",
|
||||
Title: "测试短剧",
|
||||
Desc: "剧情简介",
|
||||
Cover: "https://cdn.example.com/cover.jpg",
|
||||
OnlineAt: "2026-01-02 15:04:05",
|
||||
TotalEpisode: 30,
|
||||
TagNames: []string{"都市"},
|
||||
}
|
||||
raw := PageJSONLD("https://www.example.com", "https://www.example.com/detail/test-show", "/detail/test-show", map[string]any{"detail": detail}, configser.AppConfigVM{})
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &parsed); err != nil {
|
||||
t.Fatalf("json-ld invalid: %v\n%s", err, raw)
|
||||
}
|
||||
graph, _ := parsed["@graph"].([]any)
|
||||
if len(graph) < 3 {
|
||||
t.Fatalf("expected organization + website + detail nodes, got %d", len(graph))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageJSONLD_PlayHasVideoObject(t *testing.T) {
|
||||
playlet := &playletser.PlayletInfoResp{
|
||||
ID: 1,
|
||||
Slug: "example-show",
|
||||
Title: "剧",
|
||||
Cover: "https://cdn.example.com/a.jpg",
|
||||
MediaCreatedAt: "2026-03-01 08:30:00",
|
||||
MediaUpdatedAt: "2026-03-02 09:45:00",
|
||||
TotalEpisode: 12,
|
||||
TagNames: []string{"都市", "甜宠"},
|
||||
}
|
||||
chapter := &playletser.PlayletChapterInfoResp{
|
||||
ID: 9,
|
||||
PlayletID: 1,
|
||||
CurrentEpisode: 2,
|
||||
MediaTime: 90,
|
||||
MediaSize: 2048,
|
||||
Width: 1080,
|
||||
Height: 1920,
|
||||
VideoURL: "https://cdn.example.com/episode-2.m3u8",
|
||||
CountBrowse: 321,
|
||||
}
|
||||
raw := PageJSONLD("https://www.example.com", "https://www.example.com/play/example-show/9", "/play/example-show/9", map[string]any{
|
||||
"playlet": playlet,
|
||||
"chapter": chapter,
|
||||
}, configser.AppConfigVM{})
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &parsed); err != nil {
|
||||
t.Fatalf("json-ld invalid: %v", err)
|
||||
}
|
||||
found := false
|
||||
foundSeries := false
|
||||
foundEpisode := false
|
||||
for _, node := range parsed["@graph"].([]any) {
|
||||
m, _ := node.(map[string]any)
|
||||
if m["@type"] == "TVSeries" {
|
||||
foundSeries = true
|
||||
if m["numberOfEpisodes"] != float64(12) || m["datePublished"] == nil {
|
||||
t.Fatalf("TVSeries missing episode/date fields: %v", m)
|
||||
}
|
||||
}
|
||||
if m["@type"] == "TVEpisode" {
|
||||
foundEpisode = true
|
||||
if m["episodeNumber"] != float64(2) || m["associatedMedia"] == nil {
|
||||
t.Fatalf("TVEpisode missing episode/media relation: %v", m)
|
||||
}
|
||||
}
|
||||
if m["@type"] == "VideoObject" {
|
||||
found = true
|
||||
if m["uploadDate"] == nil || m["thumbnailUrl"] == nil {
|
||||
t.Fatalf("VideoObject missing required fields: %v", m)
|
||||
}
|
||||
if m["uploadDate"] != "2026-03-01T08:30:00+08:00" {
|
||||
t.Fatalf("VideoObject uploadDate = %v", m["uploadDate"])
|
||||
}
|
||||
if m["dateModified"] != "2026-03-02T09:45:00+08:00" {
|
||||
t.Fatalf("VideoObject dateModified = %v", m["dateModified"])
|
||||
}
|
||||
if m["contentUrl"] != "https://cdn.example.com/episode-2.m3u8" {
|
||||
t.Fatalf("VideoObject contentUrl = %v", m["contentUrl"])
|
||||
}
|
||||
if m["duration"] != "PT90S" || m["episodeNumber"] != float64(2) {
|
||||
t.Fatalf("VideoObject missing duration/episode: %v", m)
|
||||
}
|
||||
if m["contentRating"] != "adult" || m["isFamilyFriendly"] != false {
|
||||
t.Fatalf("VideoObject missing adult flags: %v", m)
|
||||
}
|
||||
if m["partOfSeries"] == nil || m["genre"] == nil {
|
||||
t.Fatalf("VideoObject missing series/genre relation: %v", m)
|
||||
}
|
||||
if _, exists := m["embedUrl"]; exists {
|
||||
t.Fatalf("landing page must not be emitted as embedUrl: %v", m["embedUrl"])
|
||||
}
|
||||
if m["interactionStatistic"] == nil {
|
||||
t.Fatalf("VideoObject missing interactionStatistic: %v", m)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("missing VideoObject")
|
||||
}
|
||||
if !foundSeries || !foundEpisode {
|
||||
t.Fatalf("missing TVSeries/TVEpisode: series=%v episode=%v", foundSeries, foundEpisode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageJSONLD_PaginatedCollectionHasItemList(t *testing.T) {
|
||||
raw := PageJSONLD("https://www.example.com", "https://www.example.com/list/ai-short-play/page/2", "/list/ai-short-play/page/2", map[string]any{
|
||||
"title": "短剧第2页",
|
||||
"rankOffset": 20,
|
||||
"list": []playletser.CommonData{
|
||||
{ID: 1, Slug: "first"},
|
||||
{ID: 2, Slug: "second"},
|
||||
},
|
||||
}, configser.AppConfigVM{})
|
||||
graph := parseGraph(t, raw)
|
||||
page := findNodeByType(t, graph, "CollectionPage")
|
||||
if page["mainEntity"] == nil {
|
||||
t.Fatalf("CollectionPage missing ItemList reference: %v", page)
|
||||
}
|
||||
items := findNodeByType(t, graph, "ItemList")
|
||||
entries, _ := items["itemListElement"].([]any)
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("ItemList entries = %d", len(entries))
|
||||
}
|
||||
first, _ := entries[0].(map[string]any)
|
||||
if first["position"] != float64(21) || first["url"] != "https://www.example.com/detail/first" {
|
||||
t.Fatalf("first ItemList entry = %v", first)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageJSONLD_FAQMatchesVisibleItems(t *testing.T) {
|
||||
raw := PageJSONLD("https://www.example.com", "https://www.example.com/home-road", "/home-road", map[string]any{
|
||||
"title": "回家的路",
|
||||
"faqItems": []FAQItem{
|
||||
{Question: "入口打不开怎么办?", Answer: "请切换备用入口。"},
|
||||
{Question: "如何确认安全?", Answer: "仅使用官方发布的地址。"},
|
||||
},
|
||||
}, configser.AppConfigVM{})
|
||||
graph := parseGraph(t, raw)
|
||||
faq := findNodeByType(t, graph, "FAQPage")
|
||||
questions, _ := faq["mainEntity"].([]any)
|
||||
if len(questions) != 2 {
|
||||
t.Fatalf("FAQ questions = %d", len(questions))
|
||||
}
|
||||
question, _ := questions[0].(map[string]any)
|
||||
if question["name"] != "入口打不开怎么办?" {
|
||||
t.Fatalf("FAQ question = %v", question)
|
||||
}
|
||||
findNodeByType(t, graph, "BreadcrumbList")
|
||||
}
|
||||
|
||||
func TestPageJSONLD_DetailBreadcrumbMatchesVisibleHierarchy(t *testing.T) {
|
||||
detail := &playletser.PlayletInfoResp{ID: 12, Slug: "test-show", Title: "测试短剧"}
|
||||
raw := PageJSONLD("https://www.example.com", "https://www.example.com/detail/test-show", "/detail/test-show", map[string]any{"detail": detail}, configser.AppConfigVM{})
|
||||
crumb := findNodeByType(t, parseGraph(t, raw), "BreadcrumbList")
|
||||
items, _ := crumb["itemListElement"].([]any)
|
||||
if len(items) != 3 {
|
||||
t.Fatalf("detail breadcrumb items = %d", len(items))
|
||||
}
|
||||
middle, _ := items[1].(map[string]any)
|
||||
if middle["name"] != "短剧" || middle["item"] != "https://www.example.com/list/ai-short-play" {
|
||||
t.Fatalf("detail breadcrumb middle item = %v", middle)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageJSONLD_SkipNoindex(t *testing.T) {
|
||||
raw := PageJSONLD("https://www.example.com", "", "/setting", map[string]any{"title": "账号设置"}, configser.AppConfigVM{})
|
||||
if string(raw) != "" {
|
||||
t.Fatalf("noindex page should not emit json-ld, got %s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIndexablePath(t *testing.T) {
|
||||
if !IsIndexablePath("/") || !IsIndexablePath("/detail/example-show") || !IsIndexablePath("/topic/editor-picks") || !IsIndexablePath("/help-center") {
|
||||
t.Fatal("public pages should be indexable")
|
||||
}
|
||||
if IsIndexablePath("/setting") || IsIndexablePath("/user-center") || IsIndexablePath("/search") || IsIndexablePath("/richtext/custom") || IsIndexablePath("/privacy-policy") || IsIndexablePath("/user-agreement") {
|
||||
t.Fatal("private pages should not be indexable")
|
||||
}
|
||||
}
|
||||
|
||||
func parseGraph(t *testing.T, raw template.JS) []any {
|
||||
t.Helper()
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &parsed); err != nil {
|
||||
t.Fatalf("json-ld invalid: %v\n%s", err, raw)
|
||||
}
|
||||
graph, _ := parsed["@graph"].([]any)
|
||||
return graph
|
||||
}
|
||||
|
||||
func findNodeByType(t *testing.T, graph []any, typ string) map[string]any {
|
||||
t.Helper()
|
||||
for _, node := range graph {
|
||||
m, _ := node.(map[string]any)
|
||||
if m["@type"] == typ {
|
||||
return m
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing %s node in %v", typ, graph)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// BuildRobotsTXT 动态 robots.txt:优先读取 templates/page/robots.txt,并把 Sitemap 写成绝对地址。
|
||||
func BuildRobotsTXT(siteURL string) string {
|
||||
sitemap := AbsURL(siteURL, "/sitemap.xml")
|
||||
body := defaultRobotsTXT
|
||||
if raw, err := os.ReadFile("templates/page/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: /h5/
|
||||
Disallow: /enkey
|
||||
Disallow: /newenkey
|
||||
|
||||
# 登录、搜索和账户 HTML 页面允许抓取,以便爬虫读取页面内 noindex。
|
||||
|
||||
Sitemap: /sitemap.xml
|
||||
`
|
||||
@@ -0,0 +1,29 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildRobotsTXTUsesSingleCrawlerGroup(t *testing.T) {
|
||||
body := BuildRobotsTXT("https://www.example.com")
|
||||
if got := strings.Count(body, "User-agent:"); got != 1 {
|
||||
t.Fatalf("User-agent group count = %d, want 1\n%s", got, body)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"Disallow: /api/",
|
||||
"Disallow: /ui/",
|
||||
"Disallow: /healthz",
|
||||
"Sitemap: https://www.example.com/sitemap.xml",
|
||||
"Host: www.example.com",
|
||||
} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Fatalf("robots.txt missing %q\n%s", want, body)
|
||||
}
|
||||
}
|
||||
for _, unwanted := range []string{"Disallow: /login", "Disallow: /search", "Disallow: /user-center"} {
|
||||
if strings.Contains(body, unwanted) {
|
||||
t.Fatalf("robots.txt should allow HTML page for noindex discovery: %q", unwanted)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"videoweb/app/service/configser"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ResolveSiteURL 解析对外站点根地址(无尾斜杠)。
|
||||
// 优先有效正式域名;跳过占位域名后回退到当前请求 Host。
|
||||
func ResolveSiteURL(c *gin.Context, cfg configser.AppConfigVM) string {
|
||||
candidates := []string{
|
||||
cfg.SeoInfo.CanonicalURL,
|
||||
cfg.ForeverURLAddr,
|
||||
cfg.NewURLAddr,
|
||||
}
|
||||
for _, raw := range candidates {
|
||||
if u := usableSiteURL(raw); u != "" {
|
||||
return u
|
||||
}
|
||||
}
|
||||
if c != nil && c.Request != nil {
|
||||
return requestOrigin(c.Request)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
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 根路径;其他页用 siteURL + 当前 path + query。
|
||||
// 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 ensureTrailingSlash(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 路由。
|
||||
func ContentCanonicalURL(siteURL, fallback string, data map[string]any) string {
|
||||
if detail, ok := asPlaylet(data["detail"]); ok {
|
||||
if path := playletPath("/detail", detail.Slug, detail.ID); path != "" {
|
||||
return AbsURL(siteURL, path)
|
||||
}
|
||||
} else if chapter, ok := asChapter(data["chapter"]); ok {
|
||||
playlet, _ := asPlaylet(data["playlet"])
|
||||
playletID := chapter.PlayletID
|
||||
playletSlug := ""
|
||||
if playlet != nil {
|
||||
playletID = playlet.ID
|
||||
playletSlug = playlet.Slug
|
||||
}
|
||||
path := playletPath("/play", playletSlug, playletID)
|
||||
if path == "" {
|
||||
return fallback
|
||||
}
|
||||
if chapter.ID > 0 {
|
||||
path += strconv.FormatInt(chapter.ID, 10) + "/"
|
||||
}
|
||||
return AbsURL(siteURL, path)
|
||||
}
|
||||
return ensureTrailingSlash(fallback)
|
||||
}
|
||||
|
||||
// ensureTrailingSlash 将 HTML 页面 URL 统一为目录式尾斜杠形式,并保留查询参数。
|
||||
func ensureTrailingSlash(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 != "/" && !strings.HasSuffix(u.Path, "/") {
|
||||
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,68 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"videoweb/app/service/configser"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestResolveSiteURLUsesForeverURLAddr(t *testing.T) {
|
||||
cfg := configser.AppConfigVM{
|
||||
ForeverURLAddr: "https://forever.prod.com/path",
|
||||
}
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest("GET", "https://temp.example.com/", nil)
|
||||
|
||||
got := ResolveSiteURL(c, cfg)
|
||||
want := "https://forever.prod.com"
|
||||
if got != want {
|
||||
t.Fatalf("ResolveSiteURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSiteURLFallbackRequestHost(t *testing.T) {
|
||||
cfg := configser.AppConfigVM{}
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest("GET", "https://temp.example.com/", nil)
|
||||
|
||||
got := ResolveSiteURL(c, cfg)
|
||||
want := "https://temp.example.com"
|
||||
if got != want {
|
||||
t.Fatalf("ResolveSiteURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageCanonicalURLHomepageUsesSiteURL(t *testing.T) {
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest("GET", "https://mirror.example.com/", nil)
|
||||
|
||||
got := PageCanonicalURL("https://www.prod.com", "", c)
|
||||
want := "https://www.prod.com/"
|
||||
if got != want {
|
||||
t.Fatalf("PageCanonicalURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageCanonicalURLSubpageUsesSiteURLAndPath(t *testing.T) {
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest("GET", "https://mirror.example.com/list?theme=1&page=2", nil)
|
||||
|
||||
got := PageCanonicalURL("https://www.prod.com", "", c)
|
||||
want := "https://www.prod.com/list?theme=1&page=2"
|
||||
if got != want {
|
||||
t.Fatalf("PageCanonicalURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageCanonicalURLFallbackForeverURLAddr(t *testing.T) {
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest("GET", "/detail/example-show", nil)
|
||||
|
||||
got := PageCanonicalURL("", "https://forever.prod.com/app", c)
|
||||
want := "https://forever.prod.com/detail/example-show"
|
||||
if got != want {
|
||||
t.Fatalf("PageCanonicalURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package seoser
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
"videoweb/app/appg"
|
||||
)
|
||||
|
||||
// VerifyCodes 站长平台所有权验证(meta + 根路径文件)。
|
||||
type VerifyCodes struct {
|
||||
Baidu string
|
||||
BaiduHTML string
|
||||
IndexNowKey string
|
||||
}
|
||||
|
||||
// VerifyFromConfig 读取 yaml 中的验证码与验证文件名。
|
||||
func VerifyFromConfig() VerifyCodes {
|
||||
seo := appg.Conf.SEO
|
||||
return VerifyCodes{
|
||||
Baidu: strings.TrimSpace(seo.BaiduSiteVerification),
|
||||
BaiduHTML: sanitizeVerifyFile(seo.BaiduHTMLFile),
|
||||
IndexNowKey: sanitizeIndexNowKey(seo.IndexNowKey),
|
||||
}
|
||||
}
|
||||
|
||||
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