feat(scraper): bound article enrichment diagnostics (#40)

This commit is contained in:
2026-11-25 00:07:45 +01:00
parent a3c70670f6
commit 6883b3976b
53 changed files with 4049 additions and 68 deletions

View File

@ -0,0 +1,228 @@
package articlecontent
import (
"encoding/json"
"net/url"
"strconv"
"strings"
"github.com/tradarr/backend/internal/scraper"
"golang.org/x/net/html"
)
// observeGranular reduces existing transient DOM evidence into fixed flags.
// Each category/scope contributes at most once per fetch. Nothing here decides
// whether content is associated, complete, or usable by the product.
func observeGranular(doc *html.Node, roots []*html.Node, source, expected string, aliases []string, content string, extractionEvaluated bool, o *scraper.FetchObservation) {
o.GranularSupported = true
accepted := map[string]bool{}
for _, v := range append([]string{expected}, aliases...) {
if k, ok := articleIdentity(source, v); ok {
accepted[k] = true
}
}
base, _ := url.Parse(expected)
relation := func(value string) (bool, bool) {
if value == "" || base == nil {
return false, false
}
ref, err := url.Parse(value)
if err != nil {
return false, false
}
k, ok := articleIdentity(source, base.ResolveReference(ref).String())
return accepted[k], ok
}
scope := func(n *html.Node) bool {
for p := n; p != nil; p = p.Parent {
for _, root := range roots {
if p == root {
return true
}
}
}
// Article wrappers, including metadata beside the body, are editorial.
for _, root := range roots {
for p := root; p != nil; p = p.Parent {
if p.Type == html.ElementNode && p.Data == "article" {
for q := n; q != nil; q = q.Parent {
if q == p {
return true
}
}
break
}
}
}
return false
}
set := func(c *scraper.ScopedEvidenceCounts, inside bool) {
if inside {
c.ArticleRoot = 1
} else {
c.OutsideRoot = 1
}
}
identity := func(v any) string {
switch x := v.(type) {
case string:
return x
case map[string]any:
s, _ := x["@id"].(string)
return s
}
return ""
}
var metadata func(any, int, bool, *int)
metadata = func(value any, depth int, inside bool, visited *int) {
*visited++
if depth > 32 || *visited > 2000 {
return
}
switch x := value.(type) {
case []any:
for _, v := range x {
metadata(v, depth+1, inside, visited)
}
case map[string]any:
article := false
switch typ := x["@type"].(type) {
case string:
article = typ == "Article" || typ == "NewsArticle" || typ == "ReportageNewsArticle"
case []any:
for _, typ := range typ {
if typ == "Article" || typ == "NewsArticle" || typ == "ReportageNewsArticle" {
article = true
}
}
}
if article {
id := identity(x["url"])
if id == "" {
id = identity(x["mainEntityOfPage"])
}
if id == "" {
id = identity(x["@id"])
}
match, _ := relation(id)
if id != "" {
if match {
o.IdentityEvidence.JSONLD.Associated = 1
} else {
o.IdentityEvidence.JSONLD.Mismatch = 1
}
}
if match {
if x["isAccessibleForFree"] == false || strings.EqualFold(identity(x["isAccessibleForFree"]), "false") {
set(&o.BlockingEvidence.Accessibility, inside)
}
if body, ok := x["articleBody"].(string); ok && extractionEvaluated && len(roots) == 1 && strings.Join(strings.Fields(body), " ") != strings.Join(strings.Fields(content), " ") {
set(&o.BlockingEvidence.BodyConflict, inside)
}
}
}
for _, v := range x {
switch v.(type) {
case map[string]any, []any:
metadata(v, depth+1, inside, visited)
}
}
}
}
// Presence is tracked separately for each metadata family; absent means no
// nonempty value, rather than an invalid or a different identity.
canonicalPresent, ogPresent := false, false
recordIdentity := func(c *scraper.MetadataIdentityCounts, value string) {
ref, err := url.Parse(value)
if err != nil || base == nil || len(value) > 4096 {
c.Invalid = 1
return
}
u := base.ResolveReference(ref)
host := u.Hostname()
valid := (u.Scheme == "https" || u.Scheme == "http") && host != "" && u.User == nil && u.Opaque == "" && !strings.ContainsAny(host, " \t\r\n\\")
if port := u.Port(); port != "" {
n, err := strconv.Atoi(port)
valid = valid && err == nil && n > 0 && n <= 65535
}
if !valid {
c.Invalid = 1
return
}
match, _ := relation(value)
if match {
c.Associated = 1
return
}
if strings.EqualFold(host, base.Hostname()) {
c.SameSourceDifferentIdentity = 1
} else {
c.ValidOtherHost = 1
}
}
var walk func(*html.Node)
walk = func(n *html.Node) {
if n.Type == html.ElementNode {
inside := scope(n)
marker := strings.ToLower(attr(n, "class") + " " + attr(n, "id") + " " + attr(n, "data-component"))
if strings.Contains(marker, "paywall") || strings.Contains(marker, "giftarticle") || attr(n, "data-test") == "paywall" || attr(n, "data-testid") == "paywall" {
set(&o.BlockingEvidence.Paywall, inside)
}
if challengeMarker(n) {
o.BlockingEvidence.Challenge.GlobalPage = 1
}
if attr(n, "data-content-state") == "partial" || attr(n, "data-content-state") == "incomplete" {
set(&o.BlockingEvidence.Partial, inside)
}
if attr(n, "data-content-truncated") == "true" {
set(&o.BlockingEvidence.Truncated, inside)
}
if strings.Contains(marker, "placeholder") || strings.Contains(marker, "skeleton") {
set(&o.BlockingEvidence.Placeholder, inside)
}
kind, value := "", ""
if n.Data == "link" && strings.EqualFold(attr(n, "rel"), "canonical") {
kind = "canonical"
value = attr(n, "href")
}
if n.Data == "meta" && attr(n, "property") == "og:url" {
kind = "og"
value = attr(n, "content")
}
value = strings.TrimSpace(value)
if value != "" {
if kind == "canonical" {
canonicalPresent = true
recordIdentity(&o.IdentityEvidence.Canonical, value)
} else {
ogPresent = true
recordIdentity(&o.IdentityEvidence.OG, value)
}
}
if n.Data == "script" && attr(n, "type") == "application/ld+json" {
var text strings.Builder
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
text.WriteString(c.Data)
}
}
var value any
if json.Unmarshal([]byte(text.String()), &value) == nil {
visited := 0
metadata(value, 0, inside, &visited)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
walk(doc)
if !canonicalPresent {
o.IdentityEvidence.Canonical.Absent = 1
}
if !ogPresent {
o.IdentityEvidence.OG.Absent = 1
}
}