Skip to content

Commit

Permalink
[ie/twitter] Fix syndication token generation (#12107)
Browse files Browse the repository at this point in the history
Authored by: pjrobertson, Grub4K

Co-authored-by: Simon Sawicki <[email protected]>
  • Loading branch information
pjrobertson and Grub4K authored Feb 10, 2025
1 parent 4ca8c44 commit 14cd7f3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions yt_dlp/extractor/twitter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import functools
import json
import random
import math
import re
import urllib.parse

from .common import InfoExtractor
from .periscope import PeriscopeBaseIE, PeriscopeIE
from ..jsinterp import js_number_to_string
from ..networking.exceptions import HTTPError
from ..utils import (
ExtractorError,
Expand Down Expand Up @@ -1330,15 +1331,19 @@ def _build_graphql_query(self, media_id):
},
}

def _generate_syndication_token(self, twid):
# ((Number(twid) / 1e15) * Math.PI).toString(36).replace(/(0+|\.)/g, '')
translation = str.maketrans(dict.fromkeys('0.'))
return js_number_to_string((int(twid) / 1e15) * math.PI, 36).translate(translation)

def _call_syndication_api(self, twid):
self.report_warning(
'Not all metadata or media is available via syndication endpoint', twid, only_once=True)
status = self._download_json(
'https://cdn.syndication.twimg.com/tweet-result', twid, 'Downloading syndication JSON',
headers={'User-Agent': 'Googlebot'}, query={
'id': twid,
# TODO: token = ((Number(twid) / 1e15) * Math.PI).toString(36).replace(/(0+|\.)/g, '')
'token': ''.join(random.choices('123456789abcdefghijklmnopqrstuvwxyz', k=10)),
'token': self._generate_syndication_token(twid),
})
if not status:
raise ExtractorError('Syndication endpoint returned empty JSON response')
Expand Down

0 comments on commit 14cd7f3

Please sign in to comment.