@metascraper/helpers 5.53.0 → 5.54.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 +12 -4
- package/src/index.js +63 -4
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metascraper/helpers",
|
|
3
|
-
"description": "
|
|
3
|
+
"description": "Shared helper utilities used across metascraper rules for parsing and normalizing metadata.",
|
|
4
4
|
"homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-helpers",
|
|
5
|
-
"version": "5.
|
|
5
|
+
"version": "5.54.0",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -19,7 +19,15 @@
|
|
|
19
19
|
},
|
|
20
20
|
"keywords": [
|
|
21
21
|
"helpers",
|
|
22
|
-
"
|
|
22
|
+
"html",
|
|
23
|
+
"json-ld",
|
|
24
|
+
"metadata",
|
|
25
|
+
"metascraper",
|
|
26
|
+
"normalize",
|
|
27
|
+
"open-graph",
|
|
28
|
+
"parse",
|
|
29
|
+
"scraper",
|
|
30
|
+
"utils"
|
|
23
31
|
],
|
|
24
32
|
"dependencies": {
|
|
25
33
|
"audio-extensions": "0.0.0",
|
|
@@ -65,5 +73,5 @@
|
|
|
65
73
|
"publishConfig": {
|
|
66
74
|
"access": "public"
|
|
67
75
|
},
|
|
68
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "23db3208c1dcc6367a705bdb9bf723fbaae259e0"
|
|
69
77
|
}
|
package/src/index.js
CHANGED
|
@@ -548,9 +548,23 @@ const validator = {
|
|
|
548
548
|
|
|
549
549
|
const truthyTest = () => true
|
|
550
550
|
|
|
551
|
-
const
|
|
551
|
+
const isValidValue = async (rule, value, args) => {
|
|
552
|
+
try {
|
|
553
|
+
return await rule.validate(value, args, debug)
|
|
554
|
+
} catch (error) {
|
|
555
|
+
debug('validate:error', {
|
|
556
|
+
pkgName: rule.pkgName,
|
|
557
|
+
errorName: error?.name,
|
|
558
|
+
errorMessage: error?.message
|
|
559
|
+
})
|
|
560
|
+
return false
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const findRule = async (rules, args = {}, propName) => {
|
|
552
565
|
let index = 0
|
|
553
566
|
let value
|
|
567
|
+
let hasValue = false
|
|
554
568
|
|
|
555
569
|
do {
|
|
556
570
|
const rule = rules[index++]
|
|
@@ -558,9 +572,20 @@ const findRule = async (rules, args, propName) => {
|
|
|
558
572
|
if (test(args)) {
|
|
559
573
|
const duration = debug.duration()
|
|
560
574
|
value = await rule(args)
|
|
561
|
-
|
|
575
|
+
hasValue = has(value)
|
|
576
|
+
const isRejected =
|
|
577
|
+
hasValue && rule.validate && !(await isValidValue(rule, value, args))
|
|
578
|
+
if (isRejected) {
|
|
579
|
+
value = undefined
|
|
580
|
+
hasValue = false
|
|
581
|
+
}
|
|
582
|
+
duration(
|
|
583
|
+
`${rule.pkgName}:${propName}:${index - 1}:${hasValue}${
|
|
584
|
+
isRejected ? ':rejected' : ''
|
|
585
|
+
}`
|
|
586
|
+
)
|
|
562
587
|
}
|
|
563
|
-
} while (!
|
|
588
|
+
} while (!hasValue && index < rules.length)
|
|
564
589
|
|
|
565
590
|
return value
|
|
566
591
|
}
|
|
@@ -624,6 +649,39 @@ const createGetIframeCached = getIframe => {
|
|
|
624
649
|
}
|
|
625
650
|
}
|
|
626
651
|
|
|
652
|
+
const withIframe = (rules, getIframe, propName) => {
|
|
653
|
+
const probe = async (src, args) => {
|
|
654
|
+
try {
|
|
655
|
+
return await findRule(
|
|
656
|
+
rules,
|
|
657
|
+
{ ...args, htmlDom: await getIframe(args.url, args.htmlDom, src) },
|
|
658
|
+
propName
|
|
659
|
+
)
|
|
660
|
+
} catch (_) {}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
return rules.concat(async args => {
|
|
664
|
+
const { htmlDom: $, url } = args
|
|
665
|
+
const srcs = $('iframe[src^="http"], iframe[src^="/"]')
|
|
666
|
+
.map((_, el) => $(el).attr('src'))
|
|
667
|
+
.get()
|
|
668
|
+
|
|
669
|
+
if (srcs.length > 0) {
|
|
670
|
+
const seen = new Set()
|
|
671
|
+
for (const src of srcs) {
|
|
672
|
+
const normalized = normalizeUrl(url, src)
|
|
673
|
+
if (!normalized || seen.has(normalized)) continue
|
|
674
|
+
seen.add(normalized)
|
|
675
|
+
const value = await probe(normalized, args)
|
|
676
|
+
if (has(value)) return value
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
const twitter = $('meta[name="twitter:player"]').attr('content')
|
|
681
|
+
if (twitter) return probe(twitter, args)
|
|
682
|
+
})
|
|
683
|
+
}
|
|
684
|
+
|
|
627
685
|
module.exports = {
|
|
628
686
|
$filter,
|
|
629
687
|
$jsonld,
|
|
@@ -676,5 +734,6 @@ module.exports = {
|
|
|
676
734
|
url,
|
|
677
735
|
validator,
|
|
678
736
|
video,
|
|
679
|
-
videoExtensions
|
|
737
|
+
videoExtensions,
|
|
738
|
+
withIframe
|
|
680
739
|
}
|