Skip to content

Commit 23f641d

Browse files
committed
Oh twitter
1 parent 9c5463b commit 23f641d

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

packages/metascraper-video-provider/src/get-video-info/twitter-video-info.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,43 @@ const got = require('got')
66

77
// twitter guest web token
88
// https://github.com/soimort/you-get/blob/da8c982608c9308765e0960e08fc28cccb74b215/src/you_get/extractors/twitter.py#L72
9-
const TWITTER_BEARER_TOKEN = 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA'
9+
// https://github.com/rg3/youtube-dl/blob/master/youtube_dl/extractor/twitter.py#L235
10+
const TWITTER_BEARER_TOKEN =
11+
'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw'
1012

1113
const TWITTER_HOSTNAMES = ['twitter.com', 'mobile.twitter.com']
1214

1315
const isTwitterUrl = url => TWITTER_HOSTNAMES.includes(new URL(url).hostname)
1416

1517
const getTweetId = url => url.split('/').reverse()[0]
1618

17-
const getGuestToken = async () => {
18-
const { body } = await got.post('https://api.twitter.com/1.1/guest/activate.json', {
19-
headers: { authorization: TWITTER_BEARER_TOKEN },
20-
json: true
21-
})
19+
const getGuestToken = async url => {
20+
const { body } = await got.post(
21+
'https://api.twitter.com/1.1/guest/activate.json',
22+
{
23+
headers: { Authorization: TWITTER_BEARER_TOKEN, Referer: url },
24+
json: true
25+
}
26+
)
2227
return body.guest_token
2328
}
2429

2530
const getTwitterVideoInfo = async url => {
2631
const tweetId = getTweetId(url)
32+
console.log('tweetId', tweetId)
2733
const apiUrl = `https://api.twitter.com/2/timeline/conversation/${tweetId}.json?tweet_mode=extended`
2834
const { body } = await got(apiUrl, {
2935
json: true,
3036
headers: {
3137
authorization: TWITTER_BEARER_TOKEN,
32-
'x-guest-token': await getGuestToken()
38+
'x-guest-token': await getGuestToken(url)
3339
}
3440
})
3541

3642
return chain(body)
37-
.get(`globalObjects.tweets.${tweetId}.extended_entities.media.0.video_info.variants`)
43+
.get(
44+
`globalObjects.tweets.${tweetId}.extended_entities.media.0.video_info.variants`
45+
)
3846
.orderBy('bitrate', 'asc')
3947
.value()
4048
}

0 commit comments

Comments
 (0)