Skip to content

Commit 77e47ff

Browse files
committed
chore(media-provider): improve flags interface
1 parent 8de0a05 commit 77e47ff

File tree

3 files changed

+25
-50
lines changed

3 files changed

+25
-50
lines changed

packages/metascraper-media-provider/README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,6 @@ Type: `string`
2626

2727
It specifies cache based on file system to be used by [youtube-dl](youtube-dl).
2828

29-
##### getProxy
30-
31-
Type: `function`
32-
33-
It will be called to determinate if a proxy should be used for resolving the next request URL.
34-
35-
```js
36-
const getProxy = ({ url, retryCount }) => {
37-
if (retryCount === 0) return false
38-
return 'http://user:pwd@proxy:8001'
39-
}
40-
```
41-
42-
##### gotOpts
43-
44-
Type: `object`
45-
46-
Any option provided here will passed to [got#options](https://github.com/sindresorhus/got#options).
4729

4830
##### retry
4931

@@ -59,6 +41,20 @@ Default: `30000`
5941

6042
The maximum time allowed to wait until considering the request as timed out.
6143

44+
##### flags
45+
46+
Type: `function`<br>
47+
Default: `object`
48+
49+
It defines a function that will determine the flags to be passed to [youtube-dl](youtube-dl):
50+
51+
```js
52+
const getFlags = ({ flags, url, retryCount }) => {
53+
flags.addHeader = [`referer:${url}`]
54+
return flags
55+
}
56+
```
57+
6258
## License
6359

6460
**metascraper-media-provider** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>

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

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,36 @@ const debug = require('debug-logfmt')(
66
const { serializeError } = require('serialize-error')
77
const asyncMemoizeOne = require('async-memoize-one')
88
const youtubedl = require('youtube-dl-exec')
9-
const { get, constant } = require('lodash')
109
const pTimeout = require('p-timeout')
1110

1211
const RE_UNSUPORTED_URL = /Unsupported URL/
1312

14-
const getFlags = ({ proxy, url, userAgent, cacheDir }) => {
15-
const flags = {
16-
dumpSingleJson: true,
17-
noCheckCertificates: true,
18-
noWarnings: true,
19-
preferFreeFormats: true
20-
}
21-
22-
flags.addHeader = [
23-
`referer:${url}`,
24-
userAgent && `user-agent:${userAgent}`
25-
].filter(Boolean)
26-
27-
if (cacheDir) flags.cacheDir = cacheDir
28-
else flags.noCacheDir = true
29-
30-
if (proxy) flags.proxy = proxy.toString()
31-
32-
return flags
13+
const DEFAULT_FLAGS = {
14+
dumpSingleJson: true,
15+
noCheckCertificates: true,
16+
noWarnings: true,
17+
preferFreeFormats: true
3318
}
3419

3520
module.exports = ({
36-
cacheDir,
37-
getProxy = constant(false),
3821
timeout = 30000,
3922
retry = 2,
40-
gotOpts,
23+
flags: getFlags = ({ flags }) => flags,
4124
...props
4225
}) =>
4326
asyncMemoizeOne(async url => {
44-
let retryCount = 0
27+
const retryCount = 0
4528
let isTimeout = false
4629
let isSupportedURL = true
4730
let data
4831

4932
const condition = () =>
5033
isSupportedURL && !isTimeout && data === undefined && retryCount <= retry
5134

52-
const userAgent = get(gotOpts, 'headers.user-agent')
53-
5435
const task = async () => {
5536
do {
5637
try {
57-
const proxy = getProxy({ url, retryCount: retryCount++ })
58-
const flags = getFlags({ url, proxy, userAgent, cacheDir })
38+
const flags = getFlags({ url, retryCount, flags: DEFAULT_FLAGS })
5939
data = await youtubedl(url, flags, { timeout, ...props })
6040
} catch (error) {
6141
if (condition()) {
@@ -76,4 +56,4 @@ module.exports = ({
7656
return pTimeout(task(), timeout, fallback).then(data => data ?? {})
7757
})
7858

79-
module.exports.getFlags = getFlags
59+
module.exports.DEFAULT_FLAGS = DEFAULT_FLAGS

packages/metascraper-media-provider/test/fixtures/generate.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ const youtubedl = require('youtube-dl-exec')
44
const path = require('path')
55
const fs = require('fs/promises')
66

7-
const { getFlags } = require('../../src/get-media')
7+
const { DEFAULT_FLAGS } = require('../../src/get-media')
88

99
let [, , url, filename] = process.argv
1010

1111
if (!url) throw new TypeError('process.argv[2] should be an url')
1212
if (!filename) throw new TypeError('process.argv[3] should be a filename')
1313
if (!filename.endsWith('.json')) filename += '.json'
1414
;(async () => {
15-
const flags = getFlags({ url })
16-
const payload = await youtubedl(url, flags)
15+
const payload = await youtubedl(url, DEFAULT_FLAGS)
1716
const filepath = path.resolve(__dirname, 'provider', filename)
1817
await fs.writeFile(filepath, JSON.stringify(payload, null, 2))
1918
console.log('Done at', filepath, '✨')

0 commit comments

Comments
 (0)