metascraper-audio 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 +40 -48
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-audio",
3
3
  "description": "Get audio property from HTML markup",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-audio",
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
  "metascraper"
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
@@ -4,43 +4,37 @@ const {
4
4
  $filter,
5
5
  $jsonld,
6
6
  audio,
7
+ createGetIframeCached,
8
+ defaultGetIframe,
7
9
  findRule,
8
10
  has,
9
- loadIframe,
10
11
  normalizeUrl,
11
12
  toRule
12
13
  } = require('@metascraper/helpers')
13
14
 
14
- const { find, chain, isEqual } = require('lodash')
15
- const pReflect = require('p-reflect')
16
-
17
15
  const toAudio = toRule(audio)
18
16
 
17
+ const getMediaType = domNode => {
18
+ const type = domNode?.attribs.type
19
+ if (!type) return
20
+ const mediaType = type.split(';')[0]?.split('/')[1]
21
+ if (!mediaType) return
22
+ if (mediaType === 'mpeg' || mediaType === 'mp4') return 'mp3'
23
+ return mediaType
24
+ }
25
+
19
26
  const toAudioFromDom = toRule((domNodes, opts) => {
20
- const values = chain(domNodes)
21
- .map(domNode => ({
22
- src: domNode?.attribs.src,
23
- type: chain(domNode)
24
- .get('attribs.type')
25
- .split(';')
26
- .get(0)
27
- .split('/')
28
- .get(1)
29
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
30
- .replace('mpeg', 'mp3')
31
- /* mp4 is commonly used for video */
32
- .replace('mp4', 'mp3')
33
- .value()
34
- }))
35
- .uniqWith(isEqual)
36
- .value()
27
+ const seen = new Set()
28
+ for (const domNode of domNodes) {
29
+ const src = domNode?.attribs.src
30
+ const type = getMediaType(domNode)
31
+ const key = `${src}::${type}`
32
+ if (seen.has(key)) continue
33
+ seen.add(key)
37
34
 
38
- let result
39
- find(
40
- values,
41
- ({ src, type }) => (result = audio(src, Object.assign({ type }, opts)))
42
- )
43
- return result
35
+ const result = audio(src, { type, ...opts })
36
+ if (result !== undefined) return result
37
+ }
44
38
  })
45
39
 
46
40
  const audioRules = [
@@ -74,36 +68,34 @@ const audioRules = [
74
68
  ({ htmlDom: $ }) => $filter($, $('a[href]'), el => audio(el.attr('href')))
75
69
  ]
76
70
 
77
- const _getIframe = (url, $, { src }) =>
78
- loadIframe(url, $.load(`<iframe src="${src}"></iframe>`))
79
-
80
- module.exports = ({ getIframe = _getIframe } = {}) => {
71
+ module.exports = ({ getIframe = defaultGetIframe } = {}) => {
72
+ const getIframeCached = createGetIframeCached(getIframe)
81
73
  const rules = {
82
74
  audio: audioRules.concat(
83
75
  async ({ htmlDom: $, url }) => {
84
- const srcs = [
85
- ...new $('iframe[src^="http"], iframe[src^="/"]')
86
- .map((_, element) => $(element).attr('src'))
87
- .get()
88
- .map(src => normalizeUrl(url, src))
89
- ]
76
+ const srcs = $('iframe[src^="http"], iframe[src^="/"]')
77
+ .map((_, element) => $(element).attr('src'))
78
+ .get()
90
79
  if (srcs.length === 0) return
91
- return pReflect(
92
- Promise.any(
93
- srcs.map(async src => {
94
- const htmlDom = await getIframe(url, $, { src })
95
- const result = await findRule(audioRules, { htmlDom, url })
96
- if (!has(result)) throw TypeError('no result')
97
- return result
98
- })
99
- )
100
- ).then(({ value }) => value)
80
+ const seenSrcs = new Set()
81
+ for (const src of srcs) {
82
+ try {
83
+ const normalizedSrc = normalizeUrl(url, src)
84
+ if (!normalizedSrc) continue
85
+ if (seenSrcs.has(normalizedSrc)) continue
86
+ seenSrcs.add(normalizedSrc)
87
+
88
+ const htmlDom = await getIframeCached(url, $, normalizedSrc)
89
+ const result = await findRule(audioRules, { htmlDom, url })
90
+ if (has(result)) return result
91
+ } catch (_) {}
92
+ }
101
93
  },
102
94
  async ({ htmlDom: $, url }) => {
103
95
  const src = $('meta[name="twitter:player"]').attr('content')
104
96
  return src
105
97
  ? findRule(audioRules, {
106
- htmlDom: await getIframe(url, $, { src }),
98
+ htmlDom: await getIframeCached(url, $, src),
107
99
  url
108
100
  })
109
101
  : undefined