Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

FIX - KeyError: 'profile_banner_url' #1329

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions twint/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,30 @@ def refresh(self):
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')
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:
self.config.Guest_token = str(match.group(1))
else:
self.config.Guest_token = None
raise RefreshTokenException('Could not find the Guest token in HTML')
7 changes: 6 additions & 1 deletion twint/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def User(ur):
_usr.username = ur['data']['user']['legacy']['screen_name']
_usr.bio = ur['data']['user']['legacy']['description']
_usr.location = ur['data']['user']['legacy']['location']
_usr.url = ""
if 'url' in ur['data']['user']['legacy']:
_usr.url = ur['data']['user']['legacy']['url']
_usr.url = ur['data']['user']['legacy']['url']
# parsing date to user-friendly format
_dt = ur['data']['user']['legacy']['created_at']
Expand All @@ -46,7 +49,9 @@ def User(ur):
_usr.is_private = ur['data']['user']['legacy']['protected']
_usr.is_verified = ur['data']['user']['legacy']['verified']
_usr.avatar = ur['data']['user']['legacy']['profile_image_url_https']
_usr.background_image = ur['data']['user']['legacy']['profile_banner_url']
_usr.background_image = ""
if 'profile_banner_url' in ur['data']['user']['legacy']:
_usr.background_image = ur['data']['user']['legacy']['profile_banner_url']
# TODO : future implementation
# legacy_extended_profile is also available in some cases which can be used to get DOB of user
return _usr