-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
twitter.js
139 lines (117 loc) · 3.91 KB
/
twitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict'
const debug = require('debug')('metascraper-media-provider:twitter')
const { reduce, set, get, chain } = require('lodash')
const { protocol } = require('@metascraper/helpers')
const pRetry = require('p-retry')
const got = require('got')
const { expirableCounter, getAgent, getTweetId, proxyUri } = require('../util')
// twitter guest web token
// https://github.com/soimort/you-get/blob/da8c982608c9308765e0960e08fc28cccb74b215/src/you_get/extractors/twitter.py#L72
// https://github.com/rg3/youtube-dl/blob/master/youtube_dl/extractor/twitter.py#L235
const TWITTER_BEARER_TOKEN =
'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw'
const createGuestToken = ({ userAgent, tunnel }) => {
const retry = expirableCounter()
const hasTunnelAvailable = () => tunnel && retry.val() < tunnel.size()
return async () => {
let token
do {
const agent = retry.val() ? getAgent(tunnel) : undefined
debug(
`guestToken retry=${retry.val()} agent=${
agent ? proxyUri(agent) : false
}`
)
try {
const { body } = await got.post(
'https://api.twitter.com/1.1/guest/activate.json',
{
json: true,
retry: 0,
agent,
headers: {
authorization: TWITTER_BEARER_TOKEN,
origin: 'https://twitter.com',
'user-agent': userAgent
}
}
)
token = get(body, 'guest_token')
} catch (err) {
debug('guestToken:err', err.message)
retry.incr()
}
} while (!token && hasTunnelAvailable())
return token
}
}
const createGetTwitterVideo = ({ userAgent, getGuestToken }) => {
return async url => {
const tweetId = getTweetId(url)
const apiUrl = `https://api.twitter.com/2/timeline/conversation/${tweetId}.json?tweet_mode=extended`
let data
const getContent = async () => {
const token = await getGuestToken()
debug(
`getTwitterInfo apiUrl=${apiUrl} guestToken=${token} userAgent=${userAgent}`
)
const { body } = await got(apiUrl, {
retry: 0,
json: true,
headers: {
referer: url,
'x-guest-token': token,
origin: 'https://twitter.com',
authorization: TWITTER_BEARER_TOKEN,
'user-agent': userAgent
}
})
const id = get(
body,
`globalObjects.tweets.${tweetId}.retweeted_status_id_str`,
tweetId
)
const tweetObj = get(body, `globalObjects.tweets.${id}`)
data = {
extractor_key: 'Twitter',
language: get(tweetObj, 'lang'),
formats: chain(tweetObj)
.get('extended_entities.media.0.video_info.variants')
.filter('bitrate')
.orderBy('bitrate', 'asc')
.value()
}
}
await pRetry(getContent, {
retries: 3,
onFailedAttempt: () => {
debug('getTwitterInfo rotating token')
}
})
return data
}
}
module.exports = ({ fromGeneric, userAgent, tunnel }) => {
const getGuestToken = createGuestToken({ tunnel, userAgent })
const getTwitterVideo = createGetTwitterVideo({ getGuestToken, userAgent })
return async url => {
const [videoInfo, twitterVideos] = await Promise.all([
fromGeneric(url),
getTwitterVideo(url)
])
const { formats: twitterVideoFormats, ...twitterVideosData } = twitterVideos
const formats = reduce(
twitterVideoFormats,
(acc, twitterVideo, index) => {
const { url } = twitterVideo
const format = get(acc, index, {})
set(acc, index, { ...format, url, protocol: protocol(url) })
return acc
},
get(videoInfo, 'formats')
)
return { ...videoInfo, ...twitterVideosData, formats }
}
}
module.exports.createGuestToken = createGuestToken
module.exports.createGetTwitterVideo = createGetTwitterVideo