Skip to content

Commit

Permalink
Implements twintproject#1320
Browse files Browse the repository at this point in the history
  • Loading branch information
stoep committed Jan 2, 2022
1 parent 6a65930 commit bfef7a5
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions twint/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,49 @@ def refresh(self):

res = self._request()
match = re.search(r'\("gt=(\d+);', res.text)

if match:
logme.debug('Found guest token in HTML')
self.config.Guest_token = str(match.group(1))
else:
# Try again but now through TOR
res = self._request_through_tor()
match = re.search(r'\("gt=(\d+);', res.text)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
'authority': 'api.twitter.com',
'content-length': '0',
'authorization': self.config.Bearer_token,
'x-twitter-client-language': 'en',
'x-csrf-token': res.cookies.get("ct0"),
'x-twitter-active-user': 'yes',
'content-type': 'application/x-www-form-urlencoded',
'accept': '*/*',
'sec-gpc': '1',
'origin': 'https://twitter.com',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://twitter.com/',
'accept-language': 'en-US',
}

self._session.headers.update(headers)
req = self._session.prepare_request(requests.Request('POST', 'https://api.twitter.com/1.1/guest/activate.json'))
res = self._session.send(req, allow_redirects=True, timeout=self._timeout)
match = re.search(r'{"guest_token":"(\d )"}', res.text)

if match:
logme.debug('Found guest token in HTML')
logme.debug('Found guest token in JSON')
self.config.Guest_token = str(match.group(1))
else:
self.config.Guest_token = None
raise RefreshTokenException('Could not find the Guest token in HTML, slept for 5m')
# Try original again but now through TOR
res = self._request_through_tor()
match = re.search(r'\("gt=(\d+);', res.text)

if match:
logme.debug('Found guest token in HTML')
self.config.Guest_token = str(match.group(1))
else:
self.config.Guest_token = None
raise RefreshTokenException('Could not find the Guest token in HTML or JSON, sleep')

def _request_through_tor(self):
with TorClient() as tor:
Expand Down

0 comments on commit bfef7a5

Please sign in to comment.