metascraper-audio 5.27.3 → 5.28.9

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 +45 -39
  3. package/package.json +5 -5
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,7 +11,7 @@ const {
11
11
  toRule
12
12
  } = require('@metascraper/helpers')
13
13
 
14
- const reachableUrl = require('reachable-url')
14
+ const memoize = require('@keyvhq/memoize')
15
15
  const pReflect = require('p-reflect')
16
16
  const cheerio = require('cheerio')
17
17
  const got = require('got')
@@ -46,42 +46,48 @@ const _getIframe = async (url, { src }) => {
46
46
  return iframe.document.documentElement.outerHTML
47
47
  }
48
48
 
49
- module.exports = ({ getIframe = _getIframe, gotOpts } = {}) => ({
50
- audio: [
51
- ...audioRules,
52
- async ({ htmlDom: $, url }) => {
53
- const iframe = $('iframe')
54
- if (iframe.length === 0) return
55
-
56
- const src = $filter($, iframe, el => normalizeUrl(url, el.attr('src')))
57
- if (!src) return
58
-
59
- const html = await getIframe(url, { src })
60
- const htmlDom = cheerio.load(html)
61
-
62
- return findRule(audioRules, { htmlDom, url })
63
- },
64
- async ({ htmlDom: $, url }) => {
65
- const playerUrl =
66
- $('meta[name="twitter:player"]').attr('content') ||
67
- $('meta[property="twitter:player"]').attr('content')
68
-
69
- if (!playerUrl) return
70
-
71
- const response = await reachableUrl(playerUrl, gotOpts)
72
- if (!reachableUrl.isReachable(response)) return
73
-
74
- const contentType = response.headers['content-type']
75
- if (!contentType || !contentType.startsWith('text')) return
76
-
77
- const { value: html } = await pReflect(
78
- got(playerUrl, { resolveBodyOnly: true, ...gotOpts })
79
- )
80
- if (!html) return
81
-
82
- const htmlDom = cheerio.load(html)
49
+ const createGetPlayer = ({ gotOpts, keyvOpts }) => {
50
+ const getPlayer = async playerUrl => {
51
+ const { value: response } = await pReflect(got(playerUrl, gotOpts))
52
+ if (!response) return
53
+ const contentType = response.headers['content-type']
54
+ if (!contentType || !contentType.startsWith('text')) return
55
+ return response.body
56
+ }
57
+ return memoize(getPlayer, keyvOpts, {
58
+ value: value => (value === undefined ? null : value)
59
+ })
60
+ }
83
61
 
84
- return findRule(audioRules, { htmlDom, url })
85
- }
86
- ]
87
- })
62
+ module.exports = ({ getIframe = _getIframe, gotOpts, keyvOpts } = {}) => {
63
+ const getPlayer = createGetPlayer({ gotOpts, keyvOpts })
64
+
65
+ return {
66
+ audio: [
67
+ ...audioRules,
68
+ async ({ htmlDom: $, url }) => {
69
+ const iframe = $('iframe')
70
+ if (iframe.length === 0) return
71
+
72
+ const src = $filter($, iframe, el => normalizeUrl(url, el.attr('src')))
73
+ if (!src) return
74
+
75
+ const html = await getIframe(url, { src })
76
+ const htmlDom = cheerio.load(html)
77
+
78
+ return findRule(audioRules, { htmlDom, url })
79
+ },
80
+ async ({ htmlDom: $, url }) => {
81
+ const playerUrl =
82
+ $('meta[name="twitter:player"]').attr('content') ||
83
+ $('meta[property="twitter:player"]').attr('content')
84
+
85
+ if (!playerUrl) return
86
+ const html = await getPlayer(normalizeUrl(url, playerUrl))
87
+ if (!html) return
88
+ const htmlDom = cheerio.load(html)
89
+ return findRule(audioRules, { htmlDom, url })
90
+ }
91
+ ]
92
+ }
93
+ }
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.27.3",
5
+ "version": "5.28.9",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -22,11 +22,11 @@
22
22
  "metascraper"
23
23
  ],
24
24
  "dependencies": {
25
- "@metascraper/helpers": "^5.26.0",
25
+ "@keyvhq/memoize": "~1.6.6",
26
+ "@metascraper/helpers": "^5.28.9",
26
27
  "cheerio": "~1.0.0-rc.10",
27
28
  "got": "~11.8.3",
28
- "p-reflect": "~2.1.0",
29
- "reachable-url": "~1.6.7"
29
+ "p-reflect": "~2.1.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "mocha": "latest",
@@ -42,5 +42,5 @@
42
42
  "test": "NODE_PATH=.. TZ=UTC NODE_ENV=test mocha test"
43
43
  },
44
44
  "license": "MIT",
45
- "gitHead": "7f6a2b9edc7fe1342ca08368ff9d5aa3f3dedc2c"
45
+ "gitHead": "2062907198cd0fa209e844107a0b1207e7516880"
46
46
  }