metascraper-audio 5.35.0 → 5.35.2

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/index.js +59 -61
  2. package/package.json +5 -6
package/index.js CHANGED
@@ -6,20 +6,41 @@ const {
6
6
  audio,
7
7
  findRule,
8
8
  has,
9
- isMime,
9
+ $twitter,
10
10
  loadIframe,
11
11
  normalizeUrl,
12
12
  toRule
13
13
  } = require('@metascraper/helpers')
14
14
 
15
- const memoize = require('@keyvhq/memoize')
15
+ const { find, chain, isEqual } = require('lodash')
16
16
  const pReflect = require('p-reflect')
17
- const got = require('got')
18
17
 
19
18
  const toAudio = toRule(audio)
20
19
 
21
- const withContentType = (url, contentType) =>
22
- isMime(contentType, 'audio') ? url : false
20
+ const toAudioFromDom = toRule((domNodes, opts) => {
21
+ const values = chain(domNodes)
22
+ .map(domNode => ({
23
+ src: domNode?.attribs.src,
24
+ type: chain(domNode)
25
+ .get('attribs.type')
26
+ .split(';')
27
+ .get(0)
28
+ .split('/')
29
+ .get(1)
30
+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
31
+ .replace('mpeg', 'mp3')
32
+ .value()
33
+ }))
34
+ .uniqWith(isEqual)
35
+ .value()
36
+
37
+ let result
38
+ find(
39
+ values,
40
+ ({ src, type }) => (result = audio(src, Object.assign({ type }, opts)))
41
+ )
42
+ return result
43
+ })
23
44
 
24
45
  const audioRules = [
25
46
  ({ url, htmlDom: $ }) => {
@@ -35,45 +56,27 @@ const audioRules = [
35
56
  })
36
57
  : undefined
37
58
  },
38
- toAudio($ => {
39
- const contentType =
40
- $('meta[name="twitter:player:stream:content_type"]').attr('content') ||
41
- $('meta[property="twitter:player:stream:content_type"]').attr('content')
42
-
43
- const streamUrl =
44
- $('meta[name="twitter:player:stream"]').attr('content') ||
45
- $('meta[property="twitter:player:stream"]').attr('content')
46
-
47
- return contentType ? withContentType(streamUrl, contentType) : streamUrl
48
- }),
59
+ ({ url, htmlDom: $ }) => {
60
+ const src = $twitter($, 'twitter:player:stream')
61
+ return src
62
+ ? audio(src, {
63
+ url,
64
+ type: $twitter($, 'twitter:player:stream:content_type')
65
+ })
66
+ : undefined
67
+ },
49
68
  toAudio($jsonld('contentUrl')),
50
- toAudio($ => $('audio').attr('src')),
51
- toAudio($ => $('audio > source').attr('src')),
69
+ toAudioFromDom($ => $('audio').get()),
70
+ toAudioFromDom($ => $('audio > source').get()),
52
71
  ({ htmlDom: $ }) => $filter($, $('a[href]'), el => audio(el.attr('href')))
53
72
  ]
54
73
 
55
74
  const _getIframe = (url, $, { src }) =>
56
75
  loadIframe(url, $.load(`<iframe src="${src}"></iframe>`))
57
76
 
58
- const createGetPlayer = ({ gotOpts, keyvOpts }) => {
59
- const getPlayer = async playerUrl => {
60
- const { value: response } = await pReflect(got(playerUrl, gotOpts))
61
- if (!response) return
62
- const contentType = response.headers['content-type']
63
- if (!contentType || !contentType.startsWith('text')) return
64
- return response.body
65
- }
66
- return memoize(getPlayer, keyvOpts, {
67
- value: value => (value === undefined ? null : value)
68
- })
69
- }
70
-
71
- module.exports = ({ getIframe = _getIframe, gotOpts, keyvOpts } = {}) => {
72
- const getPlayer = createGetPlayer({ gotOpts, keyvOpts })
73
-
77
+ module.exports = ({ getIframe = _getIframe } = {}) => {
74
78
  return {
75
- audio: [
76
- ...audioRules,
79
+ audio: audioRules.concat(
77
80
  async ({ htmlDom: $, url }) => {
78
81
  const iframe = $('iframe')
79
82
  if (iframe.length === 0) return
@@ -92,33 +95,28 @@ module.exports = ({ getIframe = _getIframe, gotOpts, keyvOpts } = {}) => {
92
95
  }
93
96
  })
94
97
 
95
- if (srcs.length === 0) return
96
-
97
- const { value } = await pReflect(
98
- Promise.any(
99
- srcs.map(async src => {
100
- const htmlDom = await getIframe(url, $, { src })
101
- const result = await findRule(audioRules, { htmlDom, url })
102
- if (!has(result)) throw TypeError('no result')
103
- return result
104
- })
105
- )
106
- )
107
-
108
- return value
98
+ return srcs.length > 0
99
+ ? pReflect(
100
+ Promise.any(
101
+ srcs.map(async src => {
102
+ const htmlDom = await getIframe(url, $, { src })
103
+ const result = await findRule(audioRules, { htmlDom, url })
104
+ if (!has(result)) throw TypeError('no result')
105
+ return result
106
+ })
107
+ )
108
+ ).then(({ value }) => value)
109
+ : undefined
109
110
  },
110
111
  async ({ htmlDom: $, url }) => {
111
- const playerUrl =
112
- $('meta[name="twitter:player"]').attr('content') ||
113
- $('meta[property="twitter:player"]').attr('content')
114
- if (!playerUrl) return
115
-
116
- const html = await getPlayer(normalizeUrl(url, playerUrl))
117
- if (!html) return
118
-
119
- const htmlDom = $.load(html)
120
- return findRule(audioRules, { htmlDom, url })
112
+ const src = $twitter($, 'twitter:player')
113
+ return src
114
+ ? findRule(audioRules, {
115
+ htmlDom: await getIframe(url, $, { src }),
116
+ url
117
+ })
118
+ : undefined
121
119
  }
122
- ]
120
+ )
123
121
  }
124
122
  }
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://nicedoc.io/microlinkhq/metascraper/packages/metascraper-audio",
5
- "version": "5.35.0",
5
+ "version": "5.35.2",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -22,16 +22,15 @@
22
22
  "metascraper"
23
23
  ],
24
24
  "dependencies": {
25
- "@keyvhq/memoize": "~2.0.3",
26
- "@metascraper/helpers": "^5.35.0",
27
- "got": "~11.8.6",
25
+ "@metascraper/helpers": "^5.35.2",
26
+ "lodash": "~4.17.21",
28
27
  "p-reflect": "~2.1.0"
29
28
  },
30
29
  "devDependencies": {
31
30
  "ava": "latest"
32
31
  },
33
32
  "engines": {
34
- "node": ">= 12"
33
+ "node": ">= 16"
35
34
  },
36
35
  "files": [
37
36
  "index.js"
@@ -40,5 +39,5 @@
40
39
  "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
41
40
  },
42
41
  "license": "MIT",
43
- "gitHead": "4050798d28b1118e3bb8058bf17b182bb84b7c5b"
42
+ "gitHead": "ba30cbe6b6d649f845131278f65504e9c63db622"
44
43
  }