[go: up one dir, main page]

Skip to content

Commit

Permalink
Detect when CLI guest token from file has expired
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAnotherArchivist committed Feb 8, 2022
1 parent b0753c3 commit 01799a7
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion snscrape/modules/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
_logger = logging.getLogger(__name__)
_API_AUTHORIZATION_HEADER = 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs=1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA'
_globalGuestTokenManager = None
_GUEST_TOKEN_VALIDITY = 10800


@dataclasses.dataclass
Expand Down Expand Up @@ -248,6 +249,9 @@ def _read(self):
o = json.load(fp)
self._token = o['token']
self._setTime = o['setTime']
if self._setTime < time.time() - _GUEST_TOKEN_VALIDITY:
_logger.info('Guest token expired')
self.reset()

def _write(self):
with self._lock:
Expand Down Expand Up @@ -330,7 +334,7 @@ def _ensure_guest_token(self, url = None):
self._guestTokenManager.token = o['guest_token']
assert self._guestTokenManager.token
_logger.debug(f'Using guest token {self._guestTokenManager.token}')
self._session.cookies.set('gt', self._guestTokenManager.token, domain = '.twitter.com', path = '/', secure = True, expires = self._guestTokenManager.setTime + 10800)
self._session.cookies.set('gt', self._guestTokenManager.token, domain = '.twitter.com', path = '/', secure = True, expires = self._guestTokenManager.setTime + _GUEST_TOKEN_VALIDITY)
self._apiHeaders['x-guest-token'] = self._guestTokenManager.token

def _unset_guest_token(self):
Expand Down

0 comments on commit 01799a7

Please sign in to comment.