metascraper-video 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 +18 -0
  2. package/index.js +34 -24
  3. package/package.json +6 -4
package/README.md CHANGED
@@ -11,6 +11,24 @@
11
11
  $ npm install metascraper-video --save
12
12
  ```
13
13
 
14
+ ## API
15
+
16
+ ### metascraper-video([options])
17
+
18
+ #### options
19
+
20
+ ##### gotOpts
21
+
22
+ Type: `object`
23
+
24
+ Any option provided here will passed to [got#options](https://github.com/sindresorhus/got#options).
25
+
26
+ ##### keyvOpts
27
+
28
+ Type: `object`
29
+
30
+ Any option provided here will passed to [@keyvhq/memoize#options](https://github.com/microlinkhq/keyv/tree/master/packages/memoize#keyvoptions).
31
+
14
32
  ## License
15
33
 
16
34
  **metascraper-video** © [Microlink](https://microlink.io), Released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
package/index.js CHANGED
@@ -9,6 +9,8 @@ const {
9
9
  video
10
10
  } = require('@metascraper/helpers')
11
11
 
12
+ const reachableUrl = require('reachable-url')
13
+ const memoize = require('@keyvhq/memoize')
12
14
  const pReflect = require('p-reflect')
13
15
  const { chain } = require('lodash')
14
16
  const cheerio = require('cheerio')
@@ -39,31 +41,39 @@ const videoRules = [
39
41
  toVideoFromDom($ => $('video > source').get())
40
42
  ]
41
43
 
42
- module.exports = ({ gotOpts } = {}) => ({
43
- image: [toUrl($ => $('video').attr('poster'))],
44
- video: [
45
- ...videoRules,
46
- async ({ htmlDom: $, url }) => {
47
- const playerUrl =
48
- $('meta[name="twitter:player"]').attr('content') ||
49
- $('meta[property="twitter:player"]').attr('content')
44
+ const createGetPlayer = ({ gotOpts, keyvOpts }) => {
45
+ const getPlayer = async playerUrl => {
46
+ const response = await reachableUrl(playerUrl, gotOpts)
47
+ if (!reachableUrl.isReachable(response)) return
48
+ const contentType = response.headers['content-type']
49
+ if (!contentType || !contentType.startsWith('text')) return
50
+ const { value: html } = await pReflect(
51
+ got(playerUrl, { resolveBodyOnly: true, ...gotOpts })
52
+ )
53
+ return html
54
+ }
50
55
 
51
- if (!playerUrl) return
56
+ return memoize(getPlayer, keyvOpts)
57
+ }
52
58
 
53
- const { isRejected, value } = await pReflect(got.head(playerUrl, gotOpts))
54
- if (isRejected) return
59
+ module.exports = ({ gotOpts, keyvOpts } = {}) => {
60
+ const getPlayer = createGetPlayer({ gotOpts, keyvOpts })
55
61
 
56
- const contentType = value.headers['content-type']
57
- if (!contentType || !contentType.startsWith('text')) return
62
+ return {
63
+ image: [toUrl($ => $('video').attr('poster'))],
64
+ video: [
65
+ ...videoRules,
66
+ async ({ htmlDom: $, url }) => {
67
+ const playerUrl =
68
+ $('meta[name="twitter:player"]').attr('content') ||
69
+ $('meta[property="twitter:player"]').attr('content')
58
70
 
59
- const { value: html } = await pReflect(
60
- got(playerUrl, { resolveBodyOnly: true, ...gotOpts })
61
- )
62
- if (!html) return
63
-
64
- const htmlDom = cheerio.load(html)
65
-
66
- return findRule(videoRules, { htmlDom, url })
67
- }
68
- ]
69
- })
71
+ if (!playerUrl) return
72
+ const html = await getPlayer(playerUrl)
73
+ if (!html) return
74
+ const htmlDom = cheerio.load(html)
75
+ return findRule(videoRules, { htmlDom, url })
76
+ }
77
+ ]
78
+ }
79
+ }
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://nicedoc.io/microlinkhq/metascraper/packages/metascraper-video",
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,11 +22,13 @@
22
22
  "video"
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
29
  "lodash": "~4.17.21",
29
- "p-reflect": "~2.1.0"
30
+ "p-reflect": "~2.1.0",
31
+ "reachable-url": "~1.6.7"
30
32
  },
31
33
  "devDependencies": {
32
34
  "mocha": "latest",
@@ -43,5 +45,5 @@
43
45
  "test": "NODE_PATH=.. TZ=UTC NODE_ENV=test mocha test"
44
46
  },
45
47
  "license": "MIT",
46
- "gitHead": "9513b1ebc0262208119f1e7e17c1ac3758d26811"
48
+ "gitHead": "8b3ec1e62e637ffe3a7ee3e723f51942e42d4276"
47
49
  }