Skip to content

Commit 2f1f9e2

Browse files
committed
feat(media-provider): flags → args
1 parent c8dc21f commit 2f1f9e2

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

packages/metascraper-media-provider/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ Default: `30000`
4141

4242
The maximum time allowed to wait until considering the request as timed out.
4343

44-
##### flags
44+
##### args
4545

4646
Type: `function`<br>
47-
Default: `object`
47+
Default: `({ url, flags }) => ({ url, flags })`
4848

49-
It defines a function that will determine the flags to be passed to [youtube-dl](youtube-dl):
49+
It defines a function that will determine the arguments to be passed to [youtube-dl](youtube-dl):
5050

5151
```js
52-
const getFlags = ({ flags, url, retryCount }) => {
52+
const args = ({ flags, url, retryCount }) => {
5353
flags.addHeader = [`referer:${url}`]
54-
return flags
54+
return { url, flags }
5555
}
5656
```
5757

packages/metascraper-media-provider/src/get-media.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ const debug = require('debug-logfmt')(
44
'metascraper-media-provider:provider:generic'
55
)
66
const { serializeError } = require('serialize-error')
7+
const { parseUrl } = require('@metascraper/helpers')
78
const asyncMemoizeOne = require('async-memoize-one')
89
const youtubedl = require('youtube-dl-exec')
910
const pTimeout = require('p-timeout')
1011

11-
const RE_UNSUPORTED_URL = /Unsupported URL/
12+
const RE_UNSUPPORTED_URL = /Unsupported URL/
1213

1314
const DEFAULT_FLAGS = {
1415
dumpSingleJson: true,
@@ -20,10 +21,10 @@ const DEFAULT_FLAGS = {
2021
module.exports = ({
2122
timeout = 30000,
2223
retry = 2,
23-
flags: getFlags = ({ flags }) => flags,
24+
args: getArgs = ({ url, flags }) => ({ url, flags }),
2425
...props
2526
}) =>
26-
asyncMemoizeOne(async url => {
27+
asyncMemoizeOne(async targetUrl => {
2728
const retryCount = 0
2829
let isTimeout = false
2930
let isSupportedURL = true
@@ -35,17 +36,21 @@ module.exports = ({
3536
const task = async () => {
3637
do {
3738
try {
38-
const flags = await getFlags({
39-
url,
39+
const { url, flags } = await getArgs({
40+
url: targetUrl,
4041
retryCount,
4142
flags: DEFAULT_FLAGS
4243
})
44+
4345
data = await youtubedl(url, flags, { timeout, ...props })
4446
} catch (error) {
4547
if (condition()) {
4648
debug('getInfo:error', { retryCount }, serializeError(error))
4749
}
48-
isSupportedURL = !RE_UNSUPORTED_URL.test(error.stderr)
50+
51+
const isYoutube = parseUrl(targetUrl).domain === 'youtube.com'
52+
const isUnsupported = RE_UNSUPPORTED_URL.test(error.stderr)
53+
isSupportedURL = isYoutube ? isUnsupported : !isUnsupported
4954
}
5055
} while (condition())
5156

0 commit comments

Comments
 (0)