metascraper-video 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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/index.js +39 -47
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-video",
3
3
  "description": "Get video property from HTML markup",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-video",
5
- "version": "5.49.24",
5
+ "version": "5.50.0",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -23,7 +23,7 @@
23
23
  "video"
24
24
  ],
25
25
  "dependencies": {
26
- "@metascraper/helpers": "5.49.24",
26
+ "@metascraper/helpers": "5.50.0",
27
27
  "lodash": "~4.17.23",
28
28
  "p-reflect": "~2.1.0"
29
29
  },
@@ -32,7 +32,7 @@
32
32
  "ava": "5"
33
33
  },
34
34
  "engines": {
35
- "node": ">= 16"
35
+ "node": ">= 22"
36
36
  },
37
37
  "files": [
38
38
  "src"
@@ -47,5 +47,5 @@
47
47
  "!test/helpers.js"
48
48
  ]
49
49
  },
50
- "gitHead": "4498fc7634a83a772379ab474f66abf4de1d1fa7"
50
+ "gitHead": "bf71c86e241b0cafa64dc697e3d6d2c065422a0b"
51
51
  }
package/src/index.js CHANGED
@@ -2,44 +2,38 @@
2
2
 
3
3
  const {
4
4
  $jsonld,
5
+ createGetIframeCached,
6
+ defaultGetIframe,
5
7
  findRule,
6
8
  has,
7
- loadIframe,
8
9
  normalizeUrl,
9
10
  toRule,
10
11
  url: urlFn,
11
12
  video
12
13
  } = require('@metascraper/helpers')
13
14
 
14
- const pReflect = require('p-reflect')
15
-
16
- const { chain, find, isEqual } = require('lodash')
17
-
18
15
  const toUrl = toRule(urlFn)
19
16
 
20
17
  const toVideo = toRule(video)
21
18
 
19
+ const getMediaType = domNode => {
20
+ const type = domNode?.attribs.type
21
+ if (!type) return
22
+ return type.split(';')[0]?.split('/')[1]
23
+ }
24
+
22
25
  const toVideoFromDom = toRule((domNodes, opts) => {
23
- const values = chain(domNodes)
24
- .map(domNode => ({
25
- src: domNode?.attribs.src,
26
- type: chain(domNode)
27
- .get('attribs.type')
28
- .split(';')
29
- .get(0)
30
- .split('/')
31
- .get(1)
32
- .value()
33
- }))
34
- .uniqWith(isEqual)
35
- .value()
26
+ const seen = new Set()
27
+ for (const domNode of domNodes) {
28
+ const src = domNode?.attribs.src
29
+ const type = getMediaType(domNode)
30
+ const key = `${src}::${type}`
31
+ if (seen.has(key)) continue
32
+ seen.add(key)
36
33
 
37
- let result
38
- find(
39
- values,
40
- ({ src, type }) => (result = video(src, Object.assign({ type }, opts)))
41
- )
42
- return result
34
+ const result = video(src, { type, ...opts })
35
+ if (result !== undefined) return result
36
+ }
43
37
  })
44
38
 
45
39
  const videoRules = [
@@ -74,45 +68,43 @@ const videoRules = [
74
68
 
75
69
  const imageRules = [toUrl($ => $('video').attr('poster'))]
76
70
 
77
- const _getIframe = (url, $, { src }) =>
78
- loadIframe(url, $.load(`<iframe src="${src}"></iframe>`))
79
-
80
71
  const withIframe = (rules, getIframe) =>
81
72
  rules.concat(
82
73
  async ({ htmlDom: $, url }) => {
83
- const srcs = [
84
- ...new $('iframe[src^="http"], iframe[src^="/"]')
85
- .map((_, element) => $(element).attr('src'))
86
- .get()
87
- .map(src => normalizeUrl(url, src))
88
- ]
74
+ const srcs = $('iframe[src^="http"], iframe[src^="/"]')
75
+ .map((_, element) => $(element).attr('src'))
76
+ .get()
89
77
  if (srcs.length === 0) return
90
- return pReflect(
91
- Promise.any(
92
- srcs.map(async src => {
93
- const htmlDom = await getIframe(url, $, { src })
94
- const result = await findRule(rules, { htmlDom, url })
95
- if (!has(result)) throw TypeError('no result')
96
- return result
97
- })
98
- )
99
- ).then(({ value }) => value)
78
+ const seenSrcs = new Set()
79
+ for (const src of srcs) {
80
+ try {
81
+ const normalizedSrc = normalizeUrl(url, src)
82
+ if (!normalizedSrc) continue
83
+ if (seenSrcs.has(normalizedSrc)) continue
84
+ seenSrcs.add(normalizedSrc)
85
+
86
+ const htmlDom = await getIframe(url, $, normalizedSrc)
87
+ const result = await findRule(rules, { htmlDom, url })
88
+ if (has(result)) return result
89
+ } catch (_) {}
90
+ }
100
91
  },
101
92
  async ({ htmlDom: $, url }) => {
102
93
  const src = $('meta[name="twitter:player"]').attr('content')
103
94
  return src
104
95
  ? findRule(rules, {
105
- htmlDom: await getIframe(url, $, { src }),
96
+ htmlDom: await getIframe(url, $, src),
106
97
  url
107
98
  })
108
99
  : undefined
109
100
  }
110
101
  )
111
102
 
112
- module.exports = ({ getIframe = _getIframe } = {}) => {
103
+ module.exports = ({ getIframe = defaultGetIframe } = {}) => {
104
+ const getIframeCached = createGetIframeCached(getIframe)
113
105
  const rules = {
114
- image: withIframe(imageRules, getIframe),
115
- video: withIframe(videoRules, getIframe)
106
+ image: withIframe(imageRules, getIframeCached),
107
+ video: withIframe(videoRules, getIframeCached)
116
108
  }
117
109
 
118
110
  rules.pkgName = 'metascraper-video'