metascraper-audio 5.25.8 → 5.28.1

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 (3) hide show
  1. package/README.md +20 -0
  2. package/index.js +48 -38
  3. package/package.json +6 -4
package/README.md CHANGED
@@ -11,6 +11,26 @@
11
11
  $ npm install metascraper-audio --save
12
12
  ```
13
13
 
14
+ ## API
15
+
16
+ ### metascraper-audio([options])
17
+
18
+ #### options
19
+
20
+ ##### getIframe
21
+
22
+ ##### gotOpts
23
+
24
+ Type: `object`
25
+
26
+ Any option provided here will passed to [got#options](https://github.com/sindresorhus/got#options).
27
+
28
+ ##### keyvOpts
29
+
30
+ Type: `object`
31
+
32
+ Any option provided here will passed to [@keyvhq/memoize#options](https://github.com/microlinkhq/keyv/tree/master/packages/memoize#keyvoptions).
33
+
14
34
  ## License
15
35
 
16
36
  **metascraper-audio** © [Microlink](https://microlink.io), Released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
package/index.js CHANGED
@@ -11,6 +11,8 @@ const {
11
11
  toRule
12
12
  } = require('@metascraper/helpers')
13
13
 
14
+ const reachableUrl = require('reachable-url')
15
+ const memoize = require('@keyvhq/memoize')
14
16
  const pReflect = require('p-reflect')
15
17
  const cheerio = require('cheerio')
16
18
  const got = require('got')
@@ -45,42 +47,50 @@ const _getIframe = async (url, { src }) => {
45
47
  return iframe.document.documentElement.outerHTML
46
48
  }
47
49
 
48
- module.exports = ({ getIframe = _getIframe, gotOpts } = {}) => ({
49
- audio: [
50
- ...audioRules,
51
- async ({ htmlDom: $, url }) => {
52
- const iframe = $('iframe')
53
- if (iframe.length === 0) return
54
-
55
- const src = $filter($, iframe, el => normalizeUrl(url, el.attr('src')))
56
- if (!src) return
57
-
58
- const html = await getIframe(url, { src })
59
- const htmlDom = cheerio.load(html)
60
-
61
- return findRule(audioRules, { htmlDom, url })
62
- },
63
- async ({ htmlDom: $, url }) => {
64
- const playerUrl =
65
- $('meta[name="twitter:player"]').attr('content') ||
66
- $('meta[property="twitter:player"]').attr('content')
67
-
68
- if (!playerUrl) return
69
-
70
- const { isRejected, value } = await pReflect(got.head(playerUrl, gotOpts))
71
- if (isRejected) return
72
-
73
- const contentType = value.headers['content-type']
74
- if (!contentType || !contentType.startsWith('text')) return
75
-
76
- const { value: html } = await pReflect(
77
- got(playerUrl, { resolveBodyOnly: true, ...gotOpts })
78
- )
79
- if (!html) return
80
-
81
- const htmlDom = cheerio.load(html)
50
+ const createGetPlayer = ({ gotOpts, keyvOpts }) => {
51
+ const getPlayer = async playerUrl => {
52
+ const response = await reachableUrl(playerUrl, gotOpts)
53
+ if (!reachableUrl.isReachable(response)) return
54
+ const contentType = response.headers['content-type']
55
+ if (!contentType || !contentType.startsWith('text')) return
56
+ const { value: html } = await pReflect(
57
+ got(playerUrl, { resolveBodyOnly: true, ...gotOpts })
58
+ )
59
+ return html
60
+ }
61
+
62
+ return memoize(getPlayer, keyvOpts)
63
+ }
82
64
 
83
- return findRule(audioRules, { htmlDom, url })
84
- }
85
- ]
86
- })
65
+ module.exports = ({ getIframe = _getIframe, gotOpts, keyvOpts } = {}) => {
66
+ const getPlayer = createGetPlayer({ gotOpts, keyvOpts })
67
+
68
+ return {
69
+ audio: [
70
+ ...audioRules,
71
+ async ({ htmlDom: $, url }) => {
72
+ const iframe = $('iframe')
73
+ if (iframe.length === 0) return
74
+
75
+ const src = $filter($, iframe, el => normalizeUrl(url, el.attr('src')))
76
+ if (!src) return
77
+
78
+ const html = await getIframe(url, { src })
79
+ const htmlDom = cheerio.load(html)
80
+
81
+ return findRule(audioRules, { htmlDom, url })
82
+ },
83
+ async ({ htmlDom: $, url }) => {
84
+ const playerUrl =
85
+ $('meta[name="twitter:player"]').attr('content') ||
86
+ $('meta[property="twitter:player"]').attr('content')
87
+
88
+ if (!playerUrl) return
89
+ const html = await getPlayer(playerUrl)
90
+ if (!html) return
91
+ const htmlDom = cheerio.load(html)
92
+ return findRule(audioRules, { htmlDom, url })
93
+ }
94
+ ]
95
+ }
96
+ }
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.25.8",
5
+ "version": "5.28.1",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -22,10 +22,12 @@
22
22
  "metascraper"
23
23
  ],
24
24
  "dependencies": {
25
- "@metascraper/helpers": "^5.25.8",
25
+ "@keyvhq/memoize": "~1.6.6",
26
+ "@metascraper/helpers": "^5.26.0",
26
27
  "cheerio": "~1.0.0-rc.10",
27
28
  "got": "~11.8.3",
28
- "p-reflect": "~2.1.0"
29
+ "p-reflect": "~2.1.0",
30
+ "reachable-url": "~1.6.7"
29
31
  },
30
32
  "devDependencies": {
31
33
  "mocha": "latest",
@@ -41,5 +43,5 @@
41
43
  "test": "NODE_PATH=.. TZ=UTC NODE_ENV=test mocha test"
42
44
  },
43
45
  "license": "MIT",
44
- "gitHead": "9513b1ebc0262208119f1e7e17c1ac3758d26811"
46
+ "gitHead": "8b3ec1e62e637ffe3a7ee3e723f51942e42d4276"
45
47
  }