metascraper-media-provider 5.49.24 → 5.50.0
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/package.json +4 -4
- package/src/get-media.js +57 -39
- package/src/index.js +48 -18
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://github.com/microlinkhq/metascraper/packages/metascraper-media-provider",
|
|
5
|
-
"version": "5.
|
|
5
|
+
"version": "5.50.0",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"author": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"youtube-dl"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@metascraper/helpers": "5.
|
|
27
|
+
"@metascraper/helpers": "5.50.0",
|
|
28
28
|
"async-memoize-one": "~1.1.9",
|
|
29
29
|
"debug-logfmt": "~1.4.7",
|
|
30
30
|
"got": "~11.8.6",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"unique-random-array": "2"
|
|
44
44
|
},
|
|
45
45
|
"engines": {
|
|
46
|
-
"node": ">=
|
|
46
|
+
"node": ">= 22"
|
|
47
47
|
},
|
|
48
48
|
"files": [
|
|
49
49
|
"src"
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"!test/fixtures/generate.js"
|
|
60
60
|
]
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "bf71c86e241b0cafa64dc697e3d6d2c065422a0b"
|
|
63
63
|
}
|
package/src/get-media.js
CHANGED
|
@@ -7,7 +7,6 @@ const { serializeError } = require('serialize-error')
|
|
|
7
7
|
const { parseUrl } = require('@metascraper/helpers')
|
|
8
8
|
const asyncMemoizeOne = require('async-memoize-one')
|
|
9
9
|
const youtubedl = require('youtube-dl-exec')
|
|
10
|
-
const pTimeout = require('p-timeout')
|
|
11
10
|
|
|
12
11
|
const RE_UNSUPPORTED_URL = /Unsupported URL/
|
|
13
12
|
|
|
@@ -18,51 +17,70 @@ const DEFAULT_FLAGS = {
|
|
|
18
17
|
preferFreeFormats: true
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
const getMedia = async ({
|
|
21
|
+
targetUrl,
|
|
22
|
+
getArgs,
|
|
23
|
+
retry,
|
|
24
|
+
timeout,
|
|
25
|
+
props,
|
|
26
|
+
run = youtubedl
|
|
27
|
+
}) => {
|
|
28
|
+
let retryCount = 0
|
|
29
|
+
let isSupportedURL = true
|
|
30
|
+
let data
|
|
31
|
+
const startTime = Date.now()
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
const isTimeout = () =>
|
|
34
|
+
timeout !== Infinity && Date.now() - startTime >= timeout
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
url: targetUrl,
|
|
41
|
-
retryCount,
|
|
42
|
-
flags: DEFAULT_FLAGS
|
|
43
|
-
})
|
|
36
|
+
const getRemainingTimeout = () =>
|
|
37
|
+
timeout === Infinity
|
|
38
|
+
? Infinity
|
|
39
|
+
: Math.max(1, timeout - (Date.now() - startTime))
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (condition()) {
|
|
48
|
-
debug('getInfo:error', { retryCount }, serializeError(error))
|
|
49
|
-
}
|
|
41
|
+
const shouldRetry = () =>
|
|
42
|
+
isSupportedURL && !isTimeout() && data === undefined && retryCount <= retry
|
|
50
43
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
44
|
+
do {
|
|
45
|
+
try {
|
|
46
|
+
const { url, flags } = await getArgs({
|
|
47
|
+
url: targetUrl,
|
|
48
|
+
retryCount,
|
|
49
|
+
flags: DEFAULT_FLAGS
|
|
50
|
+
})
|
|
56
51
|
|
|
57
|
-
|
|
58
|
-
}
|
|
52
|
+
data = await run(url, flags, { timeout: getRemainingTimeout(), ...props })
|
|
53
|
+
} catch (error) {
|
|
54
|
+
const isYoutube = parseUrl(targetUrl).domain === 'youtube.com'
|
|
55
|
+
const isUnsupported = RE_UNSUPPORTED_URL.test(error.stderr)
|
|
56
|
+
isSupportedURL = isYoutube ? isUnsupported : !isUnsupported
|
|
59
57
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
58
|
+
if (shouldRetry()) {
|
|
59
|
+
debug('getInfo:error', { retryCount }, serializeError(error))
|
|
60
|
+
}
|
|
63
61
|
}
|
|
64
62
|
|
|
65
|
-
|
|
66
|
-
})
|
|
63
|
+
retryCount += 1
|
|
64
|
+
} while (shouldRetry())
|
|
65
|
+
|
|
66
|
+
return data ?? {}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = ({
|
|
70
|
+
timeout = 30000,
|
|
71
|
+
retry = 2,
|
|
72
|
+
args: getArgs = ({ url, flags }) => ({ url, flags }),
|
|
73
|
+
...props
|
|
74
|
+
}) =>
|
|
75
|
+
asyncMemoizeOne(targetUrl =>
|
|
76
|
+
getMedia({
|
|
77
|
+
targetUrl,
|
|
78
|
+
getArgs,
|
|
79
|
+
retry,
|
|
80
|
+
timeout,
|
|
81
|
+
props
|
|
82
|
+
})
|
|
83
|
+
)
|
|
67
84
|
|
|
68
85
|
module.exports.DEFAULT_FLAGS = DEFAULT_FLAGS
|
|
86
|
+
module.exports.getMedia = getMedia
|
package/src/index.js
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
chain,
|
|
5
|
-
eq,
|
|
6
|
-
find,
|
|
7
|
-
get,
|
|
8
|
-
isEmpty,
|
|
9
|
-
isNil,
|
|
10
|
-
negate,
|
|
11
|
-
overEvery
|
|
12
|
-
} = require('lodash')
|
|
3
|
+
const { eq, find, get, isEmpty, isNil, negate } = require('lodash')
|
|
13
4
|
|
|
14
5
|
const {
|
|
15
6
|
extension: extensionFn,
|
|
@@ -67,6 +58,26 @@ const hasVideo = format =>
|
|
|
67
58
|
const isDownloadable = ({ url }) =>
|
|
68
59
|
new URL(url).searchParams.get('download') === '1'
|
|
69
60
|
|
|
61
|
+
const getOrderByRank = value => {
|
|
62
|
+
if (Number.isNaN(value)) return 4
|
|
63
|
+
if (value === undefined) return 3
|
|
64
|
+
if (value === null) return 2
|
|
65
|
+
return 1
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const compareOrderByAsc = (left, right) => {
|
|
69
|
+
const leftRank = getOrderByRank(left)
|
|
70
|
+
const rightRank = getOrderByRank(right)
|
|
71
|
+
|
|
72
|
+
if (leftRank !== rightRank) return leftRank - rightRank
|
|
73
|
+
|
|
74
|
+
if (leftRank !== 1) return 0
|
|
75
|
+
|
|
76
|
+
if (left > right) return 1
|
|
77
|
+
if (left < right) return -1
|
|
78
|
+
return 0
|
|
79
|
+
}
|
|
80
|
+
|
|
70
81
|
const getFormatUrls =
|
|
71
82
|
(basicFilters, { orderBy }) =>
|
|
72
83
|
(input, extraFilters, { isStream }) => {
|
|
@@ -75,14 +86,33 @@ const getFormatUrls =
|
|
|
75
86
|
const formats = get(input, 'formats') ||
|
|
76
87
|
get(input, 'entries[0].formats') || [input]
|
|
77
88
|
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
89
|
+
const urlProp = isStream ? 'manifest_url' : 'url'
|
|
90
|
+
let lastUrl
|
|
91
|
+
let lastOrderByValue
|
|
92
|
+
let hasLast = false
|
|
93
|
+
|
|
94
|
+
for (const format of formats) {
|
|
95
|
+
let isMatch = true
|
|
96
|
+
for (const filter of filters) {
|
|
97
|
+
if (!filter(format)) {
|
|
98
|
+
isMatch = false
|
|
99
|
+
break
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!isMatch) continue
|
|
103
|
+
|
|
104
|
+
const currentOrderByValue = format?.[orderBy]
|
|
105
|
+
if (
|
|
106
|
+
!hasLast ||
|
|
107
|
+
compareOrderByAsc(currentOrderByValue, lastOrderByValue) >= 0
|
|
108
|
+
) {
|
|
109
|
+
lastUrl = format?.[urlProp]
|
|
110
|
+
lastOrderByValue = currentOrderByValue
|
|
111
|
+
hasLast = true
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return !isEmpty(lastUrl) ? lastUrl : undefined
|
|
86
116
|
}
|
|
87
117
|
|
|
88
118
|
const VIDEO_FILTERS = ({ isStream }) =>
|