metascraper-media-provider 5.45.10 → 5.45.18

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 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.45.10",
5
+ "version": "5.45.18",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -59,5 +59,5 @@
59
59
  "!test/fixtures/generate.js"
60
60
  ]
61
61
  },
62
- "gitHead": "dba98fa823be9b495ed8935c094d41de6184f578"
62
+ "gitHead": "1f19f59c48c715cd3bc0f04ceab8c8890ed8f893"
63
63
  }
@@ -4,6 +4,7 @@ const debug = require('debug-logfmt')(
4
4
  'metascraper-media-provider:provider:generic'
5
5
  )
6
6
  const { serializeError } = require('serialize-error')
7
+ const asyncMemoizeOne = require('async-memoize-one')
7
8
  const youtubedl = require('youtube-dl-exec')
8
9
  const { get, constant } = require('lodash')
9
10
  const pTimeout = require('p-timeout')
@@ -39,8 +40,8 @@ module.exports = ({
39
40
  retry = 2,
40
41
  gotOpts,
41
42
  ...props
42
- }) => {
43
- return async url => {
43
+ }) =>
44
+ asyncMemoizeOne(async url => {
44
45
  let retryCount = 0
45
46
  let isTimeout = false
46
47
  let isSupportedURL = true
@@ -73,8 +74,7 @@ module.exports = ({
73
74
  return data
74
75
  }
75
76
 
76
- return pTimeout(task(), timeout, fallback)
77
- }
78
- }
77
+ return pTimeout(task(), timeout, fallback).then(data => data ?? {})
78
+ })
79
79
 
80
80
  module.exports.getFlags = getFlags
@@ -1,17 +0,0 @@
1
- 'use strict'
2
-
3
- const asyncMemoizeOne = require('async-memoize-one')
4
-
5
- const createTwitterProvider = require('./provider/twitter')
6
- const createGenericProvider = require('./provider/generic')
7
- const { isTweetUrl } = require('./util')
8
-
9
- module.exports = props => {
10
- const fromGeneric = createGenericProvider(props)
11
- const fromTwitter = createTwitterProvider({ ...props, fromGeneric })
12
-
13
- return asyncMemoizeOne(async url => {
14
- const result = await (isTweetUrl(url) ? fromTwitter(url) : fromGeneric(url))
15
- return result || {}
16
- })
17
- }
@@ -1,155 +0,0 @@
1
- 'use strict'
2
-
3
- const debug = require('debug-logfmt')(
4
- 'metascraper-media-provider:provider:twitter'
5
- )
6
- const { constant, reduce, set, get, chain } = require('lodash')
7
- const { protocol } = require('@metascraper/helpers')
8
- const pReflect = require('p-reflect')
9
- const pRetry = require('p-retry')
10
- const got = require('got')
11
-
12
- const { expirableCounter, getTweetId } = require('../util')
13
-
14
- // twitter guest web token
15
- // https://github.com/soimort/you-get/blob/da8c982608c9308765e0960e08fc28cccb74b215/src/you_get/extractors/twitter.py#L72
16
- // https://github.com/rg3/youtube-dl/blob/master/youtube_dl/extractor/twitter.py#L235
17
- const TWITTER_BEARER_TOKEN =
18
- 'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw'
19
-
20
- const TWITTER_API_URL = 'https://api.twitter.com/1.1/guest/activate.json'
21
-
22
- const canRetry = proxy => !!proxy
23
-
24
- const createGuestToken = ({
25
- timeout,
26
- getAgent = constant(undefined),
27
- getProxy = constant(false),
28
- gotOpts = {}
29
- }) => {
30
- const retry = expirableCounter()
31
-
32
- return async () => {
33
- let proxy = getProxy({ url: TWITTER_API_URL, retryCount: retry.val() })
34
- let token
35
-
36
- do {
37
- const agent = getAgent(proxy)
38
- debug(`guestToken retry=${retry.val()} hasAgent=${!!agent}`)
39
-
40
- try {
41
- const { body } = await got.post(TWITTER_API_URL, {
42
- ...gotOpts,
43
- timeout,
44
- https: { rejectUnauthorized: false },
45
- responseType: 'json',
46
- agent,
47
- headers: {
48
- ...gotOpts.headers,
49
- authorization: TWITTER_BEARER_TOKEN,
50
- origin: 'https://twitter.com'
51
- }
52
- })
53
- token = get(body, 'guest_token')
54
- } catch (err) {
55
- debug('guestToken:err', err.message)
56
- retry.incr()
57
- proxy = getProxy({ url: TWITTER_API_URL, retryCount: retry.val() })
58
- }
59
- } while (!token && canRetry(proxy))
60
-
61
- return token
62
- }
63
- }
64
-
65
- const createGetTwitterVideo = ({ gotOpts = {}, getGuestToken }) => {
66
- const getData = async (apiUrl, url, token) => {
67
- const { isFulfilled, value, reason } = await pReflect(
68
- got(apiUrl, {
69
- ...gotOpts,
70
- responseType: 'json',
71
- headers: {
72
- ...gotOpts.headers,
73
- referer: url,
74
- 'x-guest-token': token,
75
- origin: 'https://twitter.com/',
76
- authorization: TWITTER_BEARER_TOKEN
77
- }
78
- })
79
- )
80
-
81
- if (isFulfilled) return value.body
82
- if (reason.response.statusCode === 404) return {}
83
- throw reason
84
- }
85
-
86
- return async url => {
87
- const tweetId = getTweetId(url)
88
- const apiUrl = `https://api.twitter.com/2/timeline/conversation/${tweetId}.json?tweet_mode=extended`
89
- let data
90
-
91
- const getContent = async () => {
92
- const token = await getGuestToken()
93
- debug(`getTwitterInfo apiUrl=${apiUrl} guestToken=${token}`)
94
-
95
- const payload = await getData(apiUrl, url, token)
96
-
97
- const id = get(
98
- payload,
99
- `globalObjects.tweets.${tweetId}.retweeted_status_id_str`,
100
- tweetId
101
- )
102
-
103
- const tweetObj = get(payload, `globalObjects.tweets.${id}`)
104
-
105
- data = {
106
- extractor_key: 'Twitter',
107
- language: get(tweetObj, 'lang'),
108
- formats: chain(tweetObj)
109
- .get('extended_entities.media.0.video_info.variants')
110
- .filter('bitrate')
111
- .orderBy('bitrate', 'asc')
112
- .value()
113
- }
114
- }
115
-
116
- await pRetry(getContent, {
117
- retries: 3,
118
- onFailedAttempt: error => {
119
- debug('getTwitterInfo rotating token', error)
120
- }
121
- })
122
-
123
- return data
124
- }
125
- }
126
-
127
- module.exports = ({ fromGeneric, getProxy, gotOpts }) => {
128
- const getGuestToken = createGuestToken({ getProxy, gotOpts })
129
- const getTwitterVideo = createGetTwitterVideo({ getGuestToken, gotOpts })
130
-
131
- return async url => {
132
- const [videoInfo, twitterVideos] = await Promise.all([
133
- fromGeneric(url),
134
- getTwitterVideo(url)
135
- ])
136
-
137
- const { formats: twitterVideoFormats, ...twitterVideosData } = twitterVideos
138
-
139
- const formats = reduce(
140
- twitterVideoFormats,
141
- (acc, twitterVideo, index) => {
142
- const { url } = twitterVideo
143
- const format = get(acc, index, {})
144
- set(acc, index, { ...format, url, protocol: protocol(url) })
145
- return acc
146
- },
147
- get(videoInfo, 'formats')
148
- )
149
-
150
- return { ...videoInfo, ...twitterVideosData, formats }
151
- }
152
- }
153
-
154
- module.exports.createGuestToken = createGuestToken
155
- module.exports.createGetTwitterVideo = createGetTwitterVideo
@@ -1,41 +0,0 @@
1
- 'use strict'
2
-
3
- const { parseUrl } = require('@metascraper/helpers')
4
- const { chain } = require('lodash')
5
-
6
- const TEN_MIN_MS = 10 * 60 * 1000
7
-
8
- const isTweet = url => url.includes('/status/')
9
-
10
- const isTweetUrl = url =>
11
- isTweet(url) && parseUrl(url).domainWithoutSuffix === 'twitter'
12
-
13
- const getTweetId = url =>
14
- chain(url).split('/').reverse().first().split('?').first().value()
15
-
16
- const expirableCounter = (value = 0, ttl = TEN_MIN_MS) => {
17
- let timestamp = Date.now()
18
-
19
- const incr = (steps = 0) => {
20
- const now = Date.now()
21
- if (now - timestamp > ttl) {
22
- timestamp = Date.now()
23
- value = 0
24
- }
25
-
26
- value += steps
27
- return value
28
- }
29
-
30
- return {
31
- val: () => incr(),
32
- incr: (n = 1) => incr(n)
33
- }
34
- }
35
-
36
- module.exports = {
37
- expirableCounter,
38
- getTweetId,
39
- isTweet,
40
- isTweetUrl
41
- }