trevorhobenshield / twitter-api-client

Implementation of X/Twitter v1, v2, and GraphQL APIs
https://pypi.org/project/twitter-api-client
MIT License
1.56k stars 207 forks source link

Fixes error getting user ID when using cookies identification #232

Open jleroy opened 1 month ago

jleroy commented 1 month ago

Currently, Account.id returns an error if we’re using cookies identification method:

account = Account(cookies={
    'ct0': '…’,
    'auth_token': '…’,
    'twid': 'u%3D…’
})

print(account.id)
Traceback (most recent call last):
  File "xxxx.py", line 38, in <module>
    print(account.id())
          ^^^^^^^^^^
  File "XXXX/twitter/account.py", line 832, in id
    return int(re.findall('"u=(\d+)"', self.session.cookies.get('twid'))[0])
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

This PR fixes this issue by changing the regex pattern if session._init_with_cookies is set.