fix(scraper): verify complete article evidence (#40)
This commit is contained in:
169
backend/internal/scraper/articlecontent/evidence.go
Normal file
169
backend/internal/scraper/articlecontent/evidence.go
Normal file
@ -0,0 +1,169 @@
|
||||
package articlecontent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"golang.org/x/net/html"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type articleEvidence struct {
|
||||
associated, mismatch, blocked, dateConflict bool
|
||||
date *time.Time
|
||||
fullBody *string
|
||||
}
|
||||
|
||||
func inspectEvidence(doc *html.Node, source, expected string) articleEvidence {
|
||||
e := articleEvidence{}
|
||||
wanted, valid := safeURL(source, expected)
|
||||
key := func(value string) string {
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
base, err := url.Parse(expected)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
ref, err := url.Parse(value)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
value, ok := safeURL(source, base.ResolveReference(ref).String())
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return value
|
||||
}
|
||||
primary, articleIDs, matching := 0, 0, false
|
||||
recordDate := func(value string) {
|
||||
if parsed, err := time.Parse(time.RFC3339Nano, value); err == nil {
|
||||
if e.date != nil && !e.date.Equal(parsed) {
|
||||
e.dateConflict = true
|
||||
} else {
|
||||
e.date = &parsed
|
||||
}
|
||||
}
|
||||
}
|
||||
identity := func(v any) string {
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
return x
|
||||
case map[string]any:
|
||||
s, _ := x["@id"].(string)
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
visited := 0
|
||||
var inspect func(any, int)
|
||||
inspect = func(v any, depth int) {
|
||||
visited++
|
||||
if depth > 32 || visited > 2000 {
|
||||
return
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case []any:
|
||||
for _, v := range x {
|
||||
inspect(v, depth+1)
|
||||
}
|
||||
case map[string]any:
|
||||
types := []string{}
|
||||
switch typ := x["@type"].(type) {
|
||||
case string:
|
||||
types = append(types, typ)
|
||||
case []any:
|
||||
for _, v := range typ {
|
||||
if t, ok := v.(string); ok {
|
||||
types = append(types, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
article := false
|
||||
for _, typ := range types {
|
||||
if typ == "Article" || typ == "NewsArticle" || typ == "ReportageNewsArticle" {
|
||||
article = true
|
||||
}
|
||||
}
|
||||
if article {
|
||||
value := identity(x["url"])
|
||||
if value == "" {
|
||||
value = identity(x["mainEntityOfPage"])
|
||||
}
|
||||
if value == "" {
|
||||
value = identity(x["@id"])
|
||||
}
|
||||
if value != "" {
|
||||
articleIDs++
|
||||
}
|
||||
if valid && key(value) == wanted {
|
||||
e.associated = true
|
||||
matching = true
|
||||
if x["isAccessibleForFree"] == false || strings.EqualFold(identity(x["isAccessibleForFree"]), "false") {
|
||||
e.blocked = true
|
||||
}
|
||||
if d, ok := x["datePublished"].(string); ok {
|
||||
recordDate(d)
|
||||
}
|
||||
if body, ok := x["articleBody"].(string); ok {
|
||||
e.fullBody = &body
|
||||
}
|
||||
}
|
||||
}
|
||||
// Traverse metadata graphs and nested Article objects, never HTML text.
|
||||
for _, v := range x {
|
||||
if _, ok := v.(map[string]any); ok {
|
||||
inspect(v, depth+1)
|
||||
} else if _, ok := v.([]any); ok {
|
||||
inspect(v, depth+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var walk func(*html.Node)
|
||||
walk = func(n *html.Node) {
|
||||
if n.Type == html.ElementNode {
|
||||
value := ""
|
||||
if n.Data == "link" && strings.EqualFold(attr(n, "rel"), "canonical") {
|
||||
value = attr(n, "href")
|
||||
}
|
||||
if n.Data == "meta" && attr(n, "property") == "og:url" {
|
||||
value = attr(n, "content")
|
||||
}
|
||||
if value != "" {
|
||||
primary++
|
||||
if valid && key(value) == wanted {
|
||||
e.associated = true
|
||||
} else {
|
||||
e.mismatch = true
|
||||
}
|
||||
}
|
||||
if n.Data == "meta" && (attr(n, "property") == "article:published_time" || attr(n, "itemprop") == "datePublished") {
|
||||
recordDate(attr(n, "content"))
|
||||
}
|
||||
if n.Data == "time" && attr(n, "itemprop") == "datePublished" {
|
||||
recordDate(attr(n, "datetime"))
|
||||
}
|
||||
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 {
|
||||
inspect(value, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
walk(c)
|
||||
}
|
||||
}
|
||||
walk(doc)
|
||||
if primary == 0 && articleIDs > 0 && !matching {
|
||||
e.mismatch = true
|
||||
}
|
||||
return e
|
||||
}
|
||||
Reference in New Issue
Block a user