metascraper-media-provider 5.26.2 → 5.28.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.
package/README.md CHANGED
@@ -23,20 +23,6 @@ Type: `string`
23
23
 
24
24
  It specifies cache based on file system to be used by [youtube-dl](youtube-dl).
25
25
 
26
- ##### onError
27
-
28
- Type: `function`
29
-
30
- A function to be called when something wrong happens.
31
-
32
- It will receive `error` and `url`.
33
-
34
- ```js
35
- const onError = (url, error) => {
36
- console.error(`${url} failed: ${error.message}`)
37
- }
38
- ```
39
-
40
26
  ##### getProxy
41
27
 
42
28
  Type: `function`
@@ -50,6 +36,18 @@ const getProxy = ({ url, retryCount }) => {
50
36
  }
51
37
  ```
52
38
 
39
+ ##### getAgent
40
+
41
+ Type: `function`
42
+
43
+ It receives as input the output from `.getProxy`. The output will be passed to `.gotOpts`.
44
+
45
+ ##### gotOpts
46
+
47
+ Type: `object`
48
+
49
+ Any option provided here will passed to [got#options](https://github.com/sindresorhus/got#options).
50
+
53
51
  ##### retry
54
52
 
55
53
  Type: `number`<br>
@@ -64,12 +62,6 @@ Default: `30000`
64
62
 
65
63
  The maximum time allowed to wait until considering the request as timed out.
66
64
 
67
- ##### userAgent
68
-
69
- Type: `string`
70
-
71
- It specifies a custom user agent.
72
-
73
65
  ## License
74
66
 
75
67
  **metascraper-media-provider** © [Microlink](https://microlink.io), Released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-media-provider",
3
3
  "description": "Get video property from HTML markup",
4
4
  "homepage": "https://nicedoc.io/microlinkhq/metascraper/packages/metascraper-media-provider",
5
- "version": "5.26.2",
5
+ "version": "5.28.2",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -23,17 +23,16 @@
23
23
  "youtube-dl"
24
24
  ],
25
25
  "dependencies": {
26
- "@metascraper/helpers": "^5.26.0",
26
+ "@metascraper/helpers": "^5.28.2",
27
27
  "async-memoize-one": "~1.1.0",
28
28
  "debug-logfmt": "~1.0.4",
29
29
  "got": "~11.8.3",
30
- "keepalive-proxy-agent": "~1.2.2",
31
30
  "lodash": "~4.17.21",
32
31
  "p-do-whilst": "~1.1.0",
33
32
  "p-reflect": "~2.1.0",
34
33
  "p-retry": "~4.6.1",
35
34
  "p-timeout": "~4.1.0",
36
- "tldts": "~5.7.69",
35
+ "tldts": "~5.7.70",
37
36
  "youtube-dl-exec": "~2.0.8"
38
37
  },
39
38
  "devDependencies": {
@@ -43,9 +42,8 @@
43
42
  "pretty-ms": "latest",
44
43
  "should": "latest",
45
44
  "snap-shot": "latest",
46
- "time-span": "latest",
47
- "ua-string": "latest",
48
- "unique-random-array": "latest"
45
+ "time-span": "4",
46
+ "unique-random-array": "2"
49
47
  },
50
48
  "engines": {
51
49
  "node": ">= 12"
@@ -65,5 +63,5 @@
65
63
  "slow": 300,
66
64
  "timeout": "120000"
67
65
  },
68
- "gitHead": "e3289bbeeab1150e08759908230ce247e75050ce"
66
+ "gitHead": "373d7f2b46ca3d1f163c412c1735daaf9434568d"
69
67
  }
@@ -9,6 +9,7 @@ const { isTweetUrl } = require('./util')
9
9
  module.exports = props => {
10
10
  const fromGeneric = createGenericProvider(props)
11
11
  const fromTwitter = createTwitterProvider({ ...props, fromGeneric })
12
+
12
13
  return asyncMemoizeOne(async url => {
13
14
  const result = await (isTweetUrl(url) ? fromTwitter(url) : fromGeneric(url))
14
15
  return result || {}
@@ -3,8 +3,8 @@
3
3
  const debug = require('debug-logfmt')(
4
4
  'metascraper-media-provider:provider:generic'
5
5
  )
6
- const { noop, constant } = require('lodash')
7
6
  const youtubedl = require('youtube-dl-exec')
7
+ const { get, constant } = require('lodash')
8
8
  const pDoWhilst = require('p-do-whilst')
9
9
  const pTimeout = require('p-timeout')
10
10
 
@@ -29,10 +29,10 @@ const getFlags = ({ proxy, url, userAgent, cacheDir }) => {
29
29
  module.exports = ({
30
30
  cacheDir,
31
31
  getProxy = constant(false),
32
- onError = noop,
32
+ getAgent, // destructure to don't pass it
33
33
  timeout = 30000,
34
34
  retry = 2,
35
- userAgent,
35
+ gotOpts,
36
36
  ...props
37
37
  }) => {
38
38
  return async url => {
@@ -44,6 +44,8 @@ module.exports = ({
44
44
  const condition = () =>
45
45
  isSupportedURL && !isTimeout && data === undefined && retryCount <= retry
46
46
 
47
+ const userAgent = get(gotOpts, 'headers.user-agent')
48
+
47
49
  const task = async () => {
48
50
  await pDoWhilst(async () => {
49
51
  try {
@@ -51,10 +53,7 @@ module.exports = ({
51
53
  const flags = getFlags({ url, proxy, userAgent, cacheDir })
52
54
  data = await youtubedl(url, flags, { timeout, ...props })
53
55
  } catch (error) {
54
- if (condition()) {
55
- debug('getInfo:error', { retryCount }, error)
56
- onError(url, error)
57
- }
56
+ if (condition()) debug('getInfo:error', { retryCount }, error)
58
57
  isSupportedURL = !RE_UNSUPORTED_URL.test(error.stderr)
59
58
  }
60
59
  }, condition)
@@ -9,7 +9,7 @@ const pReflect = require('p-reflect')
9
9
  const pRetry = require('p-retry')
10
10
  const got = require('got')
11
11
 
12
- const { expirableCounter, getAgent, getTweetId } = require('../util')
12
+ const { expirableCounter, getTweetId } = require('../util')
13
13
 
14
14
  // twitter guest web token
15
15
  // https://github.com/soimort/you-get/blob/da8c982608c9308765e0960e08fc28cccb74b215/src/you_get/extractors/twitter.py#L72
@@ -23,8 +23,9 @@ const canRetry = proxy => !!proxy
23
23
 
24
24
  const createGuestToken = ({
25
25
  timeout,
26
+ getAgent = constant(undefined),
26
27
  getProxy = constant(false),
27
- userAgent
28
+ gotOpts = {}
28
29
  }) => {
29
30
  const retry = expirableCounter()
30
31
 
@@ -38,16 +39,15 @@ const createGuestToken = ({
38
39
 
39
40
  try {
40
41
  const { body } = await got.post(TWITTER_API_URL, {
42
+ ...gotOpts,
41
43
  timeout,
42
- https: {
43
- rejectUnauthorized: false
44
- },
44
+ https: { rejectUnauthorized: false },
45
45
  responseType: 'json',
46
46
  agent,
47
47
  headers: {
48
+ ...gotOpts.headers,
48
49
  authorization: TWITTER_BEARER_TOKEN,
49
- origin: 'https://twitter.com',
50
- 'user-agent': userAgent
50
+ origin: 'https://twitter.com'
51
51
  }
52
52
  })
53
53
  token = get(body, 'guest_token')
@@ -62,17 +62,18 @@ const createGuestToken = ({
62
62
  }
63
63
  }
64
64
 
65
- const createGetTwitterVideo = ({ userAgent, getGuestToken }) => {
65
+ const createGetTwitterVideo = ({ gotOpts = {}, getGuestToken }) => {
66
66
  const getData = async (apiUrl, url, token) => {
67
67
  const { isFulfilled, value, reason } = await pReflect(
68
68
  got(apiUrl, {
69
+ ...gotOpts,
69
70
  responseType: 'json',
70
71
  headers: {
72
+ ...gotOpts.headers,
71
73
  referer: url,
72
74
  'x-guest-token': token,
73
75
  origin: 'https://twitter.com',
74
- authorization: TWITTER_BEARER_TOKEN,
75
- 'user-agent': userAgent
76
+ authorization: TWITTER_BEARER_TOKEN
76
77
  }
77
78
  })
78
79
  )
@@ -89,9 +90,7 @@ const createGetTwitterVideo = ({ userAgent, getGuestToken }) => {
89
90
 
90
91
  const getContent = async () => {
91
92
  const token = await getGuestToken()
92
- debug(
93
- `getTwitterInfo apiUrl=${apiUrl} guestToken=${token} userAgent=${userAgent}`
94
- )
93
+ debug(`getTwitterInfo apiUrl=${apiUrl} guestToken=${token}`)
95
94
 
96
95
  const payload = await getData(apiUrl, url, token)
97
96
 
@@ -116,8 +115,8 @@ const createGetTwitterVideo = ({ userAgent, getGuestToken }) => {
116
115
 
117
116
  await pRetry(getContent, {
118
117
  retries: 3,
119
- onFailedAttempt: () => {
120
- debug('getTwitterInfo rotating token')
118
+ onFailedAttempt: error => {
119
+ debug('getTwitterInfo rotating token', error)
121
120
  }
122
121
  })
123
122
 
@@ -125,9 +124,9 @@ const createGetTwitterVideo = ({ userAgent, getGuestToken }) => {
125
124
  }
126
125
  }
127
126
 
128
- module.exports = ({ fromGeneric, userAgent, getProxy }) => {
129
- const getGuestToken = createGuestToken({ getProxy, userAgent })
130
- const getTwitterVideo = createGetTwitterVideo({ getGuestToken, userAgent })
127
+ module.exports = ({ fromGeneric, getProxy, gotOpts }) => {
128
+ const getGuestToken = createGuestToken({ getProxy, gotOpts })
129
+ const getTwitterVideo = createGetTwitterVideo({ getGuestToken, gotOpts })
131
130
 
132
131
  return async url => {
133
132
  const [videoInfo, twitterVideos] = await Promise.all([
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const { getDomainWithoutSuffix } = require('tldts')
4
- const Agent = require('keepalive-proxy-agent')
5
4
 
6
5
  const TEN_MIN_MS = 10 * 60 * 1000
7
6
 
@@ -12,17 +11,6 @@ const isTweetUrl = url =>
12
11
 
13
12
  const getTweetId = url => url.split('/').reverse()[0]
14
13
 
15
- const getAgent = proxy => {
16
- if (!proxy) return undefined
17
- return {
18
- [proxy.protocol]: new Agent({
19
- keepAlive: false,
20
- rejectUnauthorized: false,
21
- proxy
22
- })
23
- }
24
- }
25
-
26
14
  const expirableCounter = (value = 0, ttl = TEN_MIN_MS) => {
27
15
  let timestamp = Date.now()
28
16
 
@@ -45,7 +33,6 @@ const expirableCounter = (value = 0, ttl = TEN_MIN_MS) => {
45
33
 
46
34
  module.exports = {
47
35
  expirableCounter,
48
- getAgent,
49
36
  getTweetId,
50
37
  isTweet,
51
38
  isTweetUrl