Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes error getting user ID when using cookies identification #232

Open
wants to merge 1 commit into
base: main
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
5 changes: 4 additions & 1 deletion twitter/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,10 @@ def fleetline(self, params: dict = None) -> dict:
@property
def id(self) -> int:
""" Get User ID """
return int(re.findall('"u=(\d+)"', self.session.cookies.get('twid'))[0])
twid = self.session.cookies.get('twid')
if not twid:
raise Exception(f'[{RED}error{RESET}] No "twid" cookie found')
return int(re.findall(r'u%3D(\d+)' if self.session._init_with_cookies else r'"u=(\d+)"', twid)[0])

def save_cookies(self, fname: str = None):
""" Save cookies to file """
Expand Down
12 changes: 10 additions & 2 deletions twitter/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,13 @@ def _validate_session(self, *args, **kwargs):

# validate credentials
if all((email, username, password)):
return login(email, username, password, **kwargs)
session = login(email, username, password, **kwargs)
session._init_with_cookies = False
return session

# invalid credentials, try validating session
if session and all(session.cookies.get(c) for c in {'ct0', 'auth_token'}):
session._init_with_cookies = True
return session

# invalid credentials and session
Expand All @@ -876,12 +879,14 @@ def _validate_session(self, *args, **kwargs):
# try validating cookies dict
if isinstance(cookies, dict) and all(cookies.get(c) for c in {'ct0', 'auth_token'}):
_session = Client(cookies=cookies, follow_redirects=True)
_session._init_with_cookies = True
_session.headers.update(get_headers(_session))
return _session

# try validating cookies from file
if isinstance(cookies, str):
_session = Client(cookies=orjson.loads(Path(cookies).read_bytes()), follow_redirects=True)
_session._init_with_cookies = True
_session.headers.update(get_headers(_session))
return _session

Expand All @@ -894,7 +899,10 @@ def _validate_session(self, *args, **kwargs):
@property
def id(self) -> int:
""" Get User ID """
return int(re.findall('"u=(\d+)"', self.session.cookies.get('twid'))[0])
twid = self.session.cookies.get('twid')
if not twid:
raise Exception(f'[{RED}error{RESET}] No "twid" cookie found')
return int(re.findall(r'u%3D(\d+)' if self.session._init_with_cookies else r'"u=(\d+)"', twid)[0])

def save_cookies(self, fname: str = None):
""" Save cookies to file """
Expand Down