metascraper 5.49.24 → 5.50.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/get-data.js +24 -6
- package/src/rules.js +13 -12
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "metascraper",
|
|
3
3
|
"description": "A library to easily scrape metadata from an article on the web using Open Graph, JSON+LD, regular HTML metadata, and series of fallbacks.",
|
|
4
4
|
"homepage": "https://metascraper.js.org",
|
|
5
|
-
"version": "5.
|
|
5
|
+
"version": "5.50.0",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"repository": {
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"website"
|
|
59
59
|
],
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@metascraper/helpers": "5.
|
|
61
|
+
"@metascraper/helpers": "5.50.0",
|
|
62
62
|
"cheerio": "~1.1.2",
|
|
63
63
|
"debug-logfmt": "~1.4.7",
|
|
64
64
|
"whoops": "~5.0.7"
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"tsd": "latest"
|
|
69
69
|
},
|
|
70
70
|
"engines": {
|
|
71
|
-
"node": ">=
|
|
71
|
+
"node": ">= 22"
|
|
72
72
|
},
|
|
73
73
|
"files": [
|
|
74
74
|
"src"
|
|
@@ -86,5 +86,5 @@
|
|
|
86
86
|
"tsd": {
|
|
87
87
|
"directory": "test/types"
|
|
88
88
|
},
|
|
89
|
-
"gitHead": "
|
|
89
|
+
"gitHead": "bf71c86e241b0cafa64dc697e3d6d2c065422a0b"
|
|
90
90
|
}
|
package/src/get-data.js
CHANGED
|
@@ -4,17 +4,35 @@ const debug = require('debug-logfmt')('metascraper:get-data')
|
|
|
4
4
|
const { findRule, has } = require('@metascraper/helpers')
|
|
5
5
|
|
|
6
6
|
const getData = async ({ rules, ...props }) => {
|
|
7
|
-
const data =
|
|
7
|
+
const data = {}
|
|
8
|
+
|
|
9
|
+
await Promise.all(
|
|
8
10
|
rules.map(async ([propName, innerRules]) => {
|
|
9
11
|
const duration = debug.duration()
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
let normalizedValue = null
|
|
13
|
+
let status = 'ok'
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
const value = await findRule(innerRules, props, propName)
|
|
17
|
+
normalizedValue = has(value) ? value : null
|
|
18
|
+
} catch (error) {
|
|
19
|
+
status = 'error'
|
|
20
|
+
debug('rule:error', {
|
|
21
|
+
propName,
|
|
22
|
+
rules: innerRules.length,
|
|
23
|
+
errorName: error?.name,
|
|
24
|
+
errorMessage: error?.message
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
duration(
|
|
29
|
+
`${propName}=${normalizedValue} rules=${innerRules.length} status=${status}`
|
|
30
|
+
)
|
|
31
|
+
data[propName] = normalizedValue
|
|
14
32
|
})
|
|
15
33
|
)
|
|
16
34
|
|
|
17
|
-
return
|
|
35
|
+
return data
|
|
18
36
|
}
|
|
19
37
|
|
|
20
38
|
module.exports = getData
|
package/src/rules.js
CHANGED
|
@@ -31,36 +31,38 @@ const mergeRules = (
|
|
|
31
31
|
omitPropNames = new Set(),
|
|
32
32
|
pickPropNames
|
|
33
33
|
) => {
|
|
34
|
+
const hasPickProps = Boolean(pickPropNames && pickPropNames.size > 0)
|
|
35
|
+
const hasOmittedProps = Boolean(omitPropNames && omitPropNames.size > 0)
|
|
36
|
+
const hasInlineRules = Array.isArray(rules) && rules.length > 0
|
|
37
|
+
|
|
38
|
+
if (!hasPickProps && !hasOmittedProps && !hasInlineRules) {
|
|
39
|
+
return baseRules
|
|
40
|
+
}
|
|
41
|
+
|
|
34
42
|
const result = {}
|
|
35
43
|
|
|
36
|
-
// Helper function to determine if a property should be included
|
|
37
44
|
const shouldIncludeProp = propName => {
|
|
38
|
-
if (
|
|
45
|
+
if (hasPickProps) {
|
|
39
46
|
return pickPropNames.has(propName)
|
|
40
47
|
}
|
|
41
48
|
return !omitPropNames.has(propName)
|
|
42
49
|
}
|
|
43
50
|
|
|
44
|
-
// Process base rules first (shallow clone arrays only)
|
|
45
51
|
for (const [propName, ruleArray] of baseRules) {
|
|
46
52
|
if (shouldIncludeProp(propName)) {
|
|
47
|
-
result[propName] =
|
|
53
|
+
result[propName] = ruleArray
|
|
48
54
|
}
|
|
49
55
|
}
|
|
50
56
|
|
|
51
|
-
|
|
52
|
-
if (!rules || !Array.isArray(rules)) {
|
|
57
|
+
if (!hasInlineRules) {
|
|
53
58
|
return Object.entries(result)
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
// Process inline rules
|
|
57
61
|
for (const { test, ...ruleSet } of rules) {
|
|
58
62
|
for (const [propName, innerRules] of Object.entries(ruleSet)) {
|
|
59
63
|
if (!shouldIncludeProp(propName)) continue
|
|
60
64
|
|
|
61
|
-
const processedRules =
|
|
62
|
-
? [...innerRules]
|
|
63
|
-
: [innerRules]
|
|
65
|
+
const processedRules = castArray(innerRules)
|
|
64
66
|
if (test) {
|
|
65
67
|
for (const rule of processedRules) {
|
|
66
68
|
rule.test = test
|
|
@@ -68,8 +70,7 @@ const mergeRules = (
|
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
if (result[propName]) {
|
|
71
|
-
|
|
72
|
-
result[propName] = [...processedRules, ...result[propName]]
|
|
73
|
+
result[propName] = processedRules.concat(result[propName])
|
|
73
74
|
} else {
|
|
74
75
|
result[propName] = processedRules
|
|
75
76
|
}
|