-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetadata-json.go
486 lines (410 loc) · 12.9 KB
/
metadata-json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package trafilatura
import (
"encoding/json"
"sort"
"strings"
"unicode/utf8"
"github.com/go-shiori/dom"
"golang.org/x/net/html"
)
type SchemaData struct {
Types []string
Data map[string]any
Importance float64
Parent *SchemaData
}
// extractJsonLd search metadata from JSON+LD data following the Schema.org guidelines
// (https://schema.org). Here we don't really care about error here, so if parse failed
// we just return the original metadata.
func extractJsonLd(opts Options, doc *html.Node, originalMetadata Metadata) Metadata {
var metadata Metadata
// Decode all script nodes that contain JSON+Ld schema
persons, organizations, articles := decodeJsonLd(doc, opts)
// Extract metadata from each article
for _, article := range articles {
// Grab "author" property from schema with @type "Person"
if metadata.Author == "" {
var authorNames string
for _, author := range getSchemaNames(article.Data["author"], "person") {
author = validateMetadataName(author)
authorNames = normalizeAuthors(authorNames, author)
}
if authorNames != "" {
metadata.Author = authorNames
}
}
// Grab sitename
if metadata.Sitename == "" {
if sitenames := getSchemaNames(article.Data["publisher"]); len(sitenames) > 0 {
metadata.Sitename = sitenames[0]
}
}
// Grab category
categories := getStringValues(article.Data, "articleSection")
if len(categories) != 0 {
metadata.Categories = append(metadata.Categories, categories...)
}
// Grab tags
tags := getSchemaNames(article.Data["keywords"])
if len(tags) > 0 {
metadata.Tags = append(metadata.Tags, tags...)
}
// Grab title
if metadata.Title == "" {
metadata.Title = getSingleStringValue(article.Data, "name")
}
// If title is empty or only consist of one word, try to look in headline
if metadata.Title == "" || strWordCount(metadata.Title) == 1 {
for attr := range article.Data {
if !strings.Contains(strings.ToLower(attr), "headline") {
continue
}
title := getSingleStringValue(article.Data, attr)
if title != "" && !strings.Contains(title, "...") {
metadata.Title = title
break
}
}
}
// If title found, use article type as page type
if metadata.PageType == "" && metadata.Title != "" && len(article.Types) > 0 {
metadata.PageType = article.Types[0]
}
}
// If author not found, look in persons
if metadata.Author == "" {
var authorNames string
for _, person := range persons {
for _, name := range getSchemaNames(person.Data) {
name = validateMetadataName(name)
authorNames = normalizeAuthors(authorNames, name)
}
}
if authorNames != "" {
metadata.Author = authorNames
}
}
// If sitename not found, look in organizations
if metadata.Sitename == "" {
names := []string{}
for _, org := range organizations {
for _, name := range getSchemaNames(org.Data) {
name = validateMetadataName(name)
if name != "" {
names = append(names, name)
}
}
}
if len(names) > 0 {
metadata.Sitename = strings.Join(names, "; ")
}
}
// If type not found, use the first article type
if metadata.PageType == "" && len(articles) > 0 && len(articles[0].Types) > 0 {
metadata.PageType = articles[0].Types[0]
}
// Uniquify tags and categories
metadata.Tags = uniquifyLists(metadata.Tags...)
metadata.Categories = uniquifyLists(metadata.Categories...)
// If available, override type, title, author, categories and tags in original metadata
originalMetadata.Title = strOr(originalMetadata.Title, metadata.Title)
originalMetadata.PageType = strOr(originalMetadata.PageType, metadata.PageType)
originalMetadata.Author = strOr(metadata.Author, originalMetadata.Author)
if len(metadata.Categories) > 0 {
originalMetadata.Categories = metadata.Categories
}
if len(metadata.Tags) > 0 {
originalMetadata.Tags = metadata.Tags
}
// If the new sitename exist and longer, override the original
if utf8.RuneCountInString(metadata.Sitename) > utf8.RuneCountInString(originalMetadata.Sitename) {
originalMetadata.Sitename = metadata.Sitename
}
return originalMetadata
}
func decodeJsonLd(doc *html.Node, opts Options) (persons, organizations, articles []SchemaData) {
// Prepare function to find articles and persons inside JSON+LD recursively
var findImportantObjects func(obj map[string]any, parent *SchemaData)
findImportantObjects = func(obj map[string]any, parent *SchemaData) {
// Schema type could be either string or slices, so extract it properly
schemaTypes := getSchemaTypes(obj, false)
// Check if the schemas is usable for our purpose
var isPerson bool
var isWebsite, isOrganization bool
var isArticle, isPosting, isReport, isBlog, isPage, isListing bool
for _, st := range schemaTypes {
st = strings.ToLower(st)
isPerson = isPerson || st == "person"
isWebsite = isWebsite || st == "website"
isOrganization = isOrganization || strings.Contains(st, "organization")
isArticle = isArticle || strings.Contains(st, "article")
isPosting = isPosting || strings.Contains(st, "posting")
isReport = isReport || st == "report"
isBlog = isBlog || st == "blog"
isPage = isPage || strings.Contains(st, "page")
isListing = isListing || strings.Contains(st, "listing")
}
// Create initial schema data
schemaData := SchemaData{
Types: schemaTypes,
Data: obj,
Parent: parent,
}
// Depending on its type, save the schema to respective slice
if isPerson {
persons = append(persons, schemaData)
}
if isWebsite || isOrganization {
// Organization is more important than website.
switch {
case isOrganization:
schemaData.Importance = 2
default:
schemaData.Importance = 1
}
organizations = append(organizations, schemaData)
}
if isArticle || isPosting || isReport || isBlog || isPage || isListing {
// Adjust its importance level
switch {
case isArticle, isPosting, isReport:
schemaData.Importance = 3
case isBlog:
schemaData.Importance = 2
case isPage, isListing:
schemaData.Importance = 1
}
articles = append(articles, schemaData)
}
// Continue to look in its sub values
for _, value := range obj {
switch v := value.(type) {
case map[string]any:
findImportantObjects(v, &schemaData)
case []any:
for _, item := range v {
if subObj, isObj := item.(map[string]any); isObj {
findImportantObjects(subObj, &schemaData)
}
}
}
}
}
// Find all script nodes that contain JSON+Ld schema
scriptNodes1 := dom.QuerySelectorAll(doc, `script[type="application/ld+json"]`)
scriptNodes2 := dom.QuerySelectorAll(doc, `script[type="application/settings+json"]`)
scriptNodes := append(scriptNodes1, scriptNodes2...)
for _, script := range scriptNodes {
// Get the json text inside the script
jsonLdText := dom.TextContent(script)
jsonLdText = strings.TrimSpace(jsonLdText)
jsonLdText = html.UnescapeString(jsonLdText)
if jsonLdText == "" {
continue
}
// Decode JSON text assuming it is an array
var dataList []map[string]any
jsonLdByte := []byte(jsonLdText)
err := json.Unmarshal(jsonLdByte, &dataList)
if err != nil {
// If not succeed, try it as an object
var data map[string]any
err = json.Unmarshal(jsonLdByte, &data)
if err == nil {
dataList = []map[string]any{data}
} else {
logWarn(opts, "error in JSON metadata extraction: %v", err)
continue
}
}
// Extract each data
for _, data := range dataList {
findImportantObjects(data, nil)
}
}
// Sort schemas based on importance
sort.SliceStable(organizations, func(i, j int) bool {
return organizations[i].Importance > organizations[j].Importance
})
sort.SliceStable(articles, func(i, j int) bool {
return articles[i].Importance > articles[j].Importance
})
// When possible, only use persons from articles
var articlePersons []SchemaData
for _, person := range persons {
if schemaInArticle(person, "person") {
articlePersons = append(articlePersons, person)
}
}
if len(articlePersons) > 0 {
persons = articlePersons
}
// Do the same for organizations
var articleOrganizations []SchemaData
for _, org := range organizations {
if schemaInArticle(org, "organization") {
articleOrganizations = append(articleOrganizations, org)
}
}
if len(articleOrganizations) > 0 {
organizations = articleOrganizations
}
return
}
func getSchemaNames(v any, expectedTypes ...string) []string {
// First, check if its string
if value, isString := v.(string); isString {
// There are some case where the name string contains an unescaped JSON,
// so try to handle it here.
parts := rxNameJson.FindStringSubmatch(value)
if rxJsonSymbol.MatchString(value) && len(parts) > 0 {
value = parts[1]
}
// Return cleaned up string
if value = trim(value); value != "" {
return []string{value}
} else {
return nil
}
}
// Second, check if its schema
if value, isObject := v.(map[string]any); isObject {
// If there are expected types specified, make sure this schema is one of those types.
// If not, we just return empty handed.
schemaTypes := getSchemaTypes(value, true)
if len(expectedTypes) > 0 {
if len(schemaTypes) == 0 {
return nil
}
var schemaAllowed bool
for _, schemaType := range schemaTypes {
if strIn(schemaType, expectedTypes...) {
schemaAllowed = true
break
}
}
if !schemaAllowed {
return nil
}
}
// If this schema has "name" string property, try it
names := getStringValues(value, "name")
// If name is empty and its type is Person, try name combination
if len(names) == 0 && strIn("person", schemaTypes...) {
givenName := getSingleStringValue(value, "givenName")
additionalName := getSingleStringValue(value, "additionalName")
familyName := getSingleStringValue(value, "familyName")
fullName := trim(givenName + " " + additionalName + " " + familyName)
names = []string{fullName}
}
// If name still empty, try its legal name
if len(names) == 0 {
names = getStringValues(value, "legalName")
}
// If name still empty, next try its alternate name
if len(names) == 0 {
names = getStringValues(value, "alternateName")
}
// If name is found, we can return it
if len(names) != 0 {
return names
}
// At this point name is still not found, so there is a possibility that the
// JSON+LD use name with uncommon format. Here we try to treat it as schema or array.
switch childValue := value["name"].(type) {
case map[string]any, []any:
return getSchemaNames(childValue, expectedTypes...)
}
// If nothing else, return nil
return nil
}
// Finally, check if its array
if values, isArray := v.([]any); isArray {
var names []string
for _, value := range values {
if subNames := getSchemaNames(value, expectedTypes...); len(subNames) > 0 {
names = append(names, subNames...)
}
}
if len(names) > 0 {
return names
} else {
return nil
}
}
// If nothing found, just return empty
return nil
}
func getSchemaTypes(schema map[string]any, toLower bool) []string {
schemaTypes := getStringValues(schema, "@type")
if toLower {
for i, tp := range schemaTypes {
schemaTypes[i] = strings.ToLower(tp)
}
}
return schemaTypes
}
func getStringValues(obj map[string]any, key string) []string {
var result []string
switch value := obj[key].(type) {
case string:
if cleanStr := trim(value); cleanStr != "" {
result = []string{cleanStr}
}
case []any:
for _, item := range value {
str, ok := item.(string)
if !ok {
continue
}
if cleanStr := trim(str); cleanStr != "" {
result = append(result, cleanStr)
}
}
}
return result
}
func getSingleStringValue(obj map[string]any, key string) string {
values := getStringValues(obj, key)
if len(values) > 0 {
return values[0]
}
return ""
}
func schemaInArticle(data SchemaData, wantedType string) bool {
// If it doesn't have any parent, it's important
if data.Parent == nil {
return true
}
// Check if parent is person or organization
var parentIsPerson bool
var parentIsOrganization bool
for _, st := range data.Parent.Types {
st = strings.ToLower(st)
parentIsPerson = parentIsPerson || st == "person"
parentIsOrganization = parentIsOrganization || st == "website" || strings.Contains(st, "organization")
}
// If necessary, check grandparent types
parentTypesToCheck := data.Parent.Types
if (wantedType == "person" && parentIsPerson) || (wantedType == "organization" && parentIsOrganization) {
if data.Parent.Parent == nil {
return true
} else {
parentTypesToCheck = data.Parent.Parent.Types
}
}
// Now, check if this schema inside article
for _, st := range parentTypesToCheck {
st = strings.ToLower(st)
isArticle := strings.Contains(st, "article")
isPosting := strings.Contains(st, "posting")
isReport := st == "report"
isBlog := st == "blog"
isPage := strings.Contains(st, "page")
isListing := strings.Contains(st, "listing")
if isArticle || isPosting || isReport || isBlog || isPage || isListing {
return true
}
}
return false
}