Skip to content

Commit

Permalink
Added get_latest_followers, get_latest_friends, get_followers_ids, ge…
Browse files Browse the repository at this point in the history
…t_friends_ids
  • Loading branch information
d60 committed May 17, 2024
1 parent 94d7b84 commit 4019384
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 1 deletion.
2 changes: 1 addition & 1 deletion twikit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
A Python library for interacting with the Twitter API.
"""

__version__ = '1.6.2'
__version__ = '1.6.3'

from .bookmark import BookmarkFolder
from .client import Client
Expand Down
159 changes: 159 additions & 0 deletions twikit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2822,6 +2822,38 @@ def _get_user_friendship(
next_cursor
)

def _get_user_friendship_2(
self, user_id: str, count: int, endpoint: str, cursor: str
) -> Result[User]:
params = {'count': count}
params['user_id'] = user_id
if cursor is not None:
params['cursor'] = cursor

response = self.http.get(
endpoint,
params=params,
headers=self._base_headers
).json()

users = response['users']
results = []
for user in users:
results.append(User(self, build_user_data(user)))

previous_cursor = response['previous_cursor']
next_cursor = response['next_cursor']

return Result(
results,
partial(self._get_user_friendship_2, user_id,
count, endpoint, next_cursor),
next_cursor,
partial(self._get_user_friendship_2, user_id,
count, endpoint, previous_cursor),
previous_cursor
)

def get_user_followers(
self, user_id: str, count: int = 20, cursor: str | None = None
) -> Result[User]:
Expand All @@ -2847,6 +2879,34 @@ def get_user_followers(
cursor
)

def get_latest_followers(
self, user_id: str, count: int = 200, cursor: str | None = None
) -> Result[User]:
"""
Retrieves the latest followers.
Max count : 200
"""
return self._get_user_friendship_2(
user_id,
count,
Endpoint.FOLLOWERS2,
cursor
)

def get_latest_friends(
self, user_id: str, count: int = 200, cursor: str | None = None
) -> Result[User]:
"""
Retrieves the latest friends (following users).
Max count : 200
"""
return self._get_user_friendship_2(
user_id,
count,
Endpoint.FOLLOWING2,
cursor
)

def get_user_verified_followers(
self, user_id: str, count: int = 20, cursor: str | None = None
) -> Result[User]:
Expand Down Expand Up @@ -2947,6 +3007,105 @@ def get_user_subscriptions(
cursor
)

def _get_friendship_ids(
self,
user_id: str | None,
screen_name: str | None,
count: int,
endpoint: str,
cursor: str | None
) -> Result[int]:
params = {'count': count}
if user_id is not None:
params['user_id'] = user_id
elif user_id is not None:
params['screen_name'] = screen_name

if cursor is not None:
params['cursor'] = cursor

response = self.http.get(
endpoint,
params=params,
headers=self._base_headers
).json()
previous_cursor = response['previous_cursor']
next_cursor = response['next_cursor']

return Result(
response['ids'],
partial(self._get_friendship_ids, user_id,
screen_name, count, endpoint, next_cursor),
next_cursor,
partial(self._get_friendship_ids, user_id,
screen_name, count, endpoint, previous_cursor),
previous_cursor
)

def get_followers_ids(
self,
user_id: str | None = None,
screen_name: str | None = None,
count: int = 5000,
cursor: str | None = None
) -> Result[int]:
"""
Fetches the IDs of the followers of a specified user.
Parameters
----------
user_id : :class:`str` | None, default=None
The ID of the user for whom to return results.
screen_name : :class:`str` | None, default=None
The screen name of the user for whom to return results.
count : :class:`int`, default=5000
The maximum number of IDs to retrieve.
Returns
-------
:class:`Result`[:class:`int`]
A Result object containing the IDs of the followers.
"""
return self._get_friendship_ids(
user_id,
screen_name,
count,
Endpoint.FOLLOWERS_IDS,
cursor
)

def get_friends_ids(
self,
user_id: str | None = None,
screen_name: str | None = None,
count: int = 5000,
cursor: str | None = None
) -> Result[int]:
"""
Fetches the IDs of the friends (following users) of a specified user.
Parameters
----------
user_id : :class:`str` | None, default=None
The ID of the user for whom to return results.
screen_name : :class:`str` | None, default=None
The screen name of the user for whom to return results.
count : :class:`int`, default=5000
The maximum number of IDs to retrieve.
Returns
-------
:class:`Result`[:class:`int`]
A Result object containing the IDs of the friends.
"""
return self._get_friendship_ids(
user_id,
screen_name,
count,
Endpoint.FRIENDS_IDS,
cursor
)

def _send_dm(
self,
conversation_id: str,
Expand Down
159 changes: 159 additions & 0 deletions twikit/twikit_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2842,6 +2842,38 @@ async def _get_user_friendship(
next_cursor
)

async def _get_user_friendship_2(
self, user_id: str, count: int, endpoint: str, cursor: str
) -> Result[User]:
params = {'count': count}
params['user_id'] = user_id
if cursor is not None:
params['cursor'] = cursor

response = (await self.http.get(
endpoint,
params=params,
headers=self._base_headers
)).json()

users = response['users']
results = []
for user in users:
results.append(User(self, build_user_data(user)))

previous_cursor = response['previous_cursor']
next_cursor = response['next_cursor']

return Result(
results,
partial(self._get_user_friendship_2, user_id,
count, endpoint, next_cursor),
next_cursor,
partial(self._get_user_friendship_2, user_id,
count, endpoint, previous_cursor),
previous_cursor
)

async def get_user_followers(
self, user_id: str, count: int = 20, cursor: str | None = None
) -> Result[User]:
Expand All @@ -2867,6 +2899,34 @@ async def get_user_followers(
cursor
)

async def get_latest_followers(
self, user_id: str, count: int = 200, cursor: str | None = None
) -> Result[User]:
"""
Retrieves the latest followers.
Max count : 200
"""
return await self._get_user_friendship_2(
user_id,
count,
Endpoint.FOLLOWERS2,
cursor
)

async def get_latest_friends(
self, user_id: str, count: int = 200, cursor: str | None = None
) -> Result[User]:
"""
Retrieves the latest friends (following users).
Max count : 200
"""
return await self._get_user_friendship_2(
user_id,
count,
Endpoint.FOLLOWING2,
cursor
)

async def get_user_verified_followers(
self, user_id: str, count: int = 20, cursor: str | None = None
) -> Result[User]:
Expand Down Expand Up @@ -2967,6 +3027,105 @@ async def get_user_subscriptions(
cursor
)

async def _get_friendship_ids(
self,
user_id: str | None,
screen_name: str | None,
count: int,
endpoint: str,
cursor: str | None
) -> Result[int]:
params = {'count': count}
if user_id is not None:
params['user_id'] = user_id
elif user_id is not None:
params['screen_name'] = screen_name

if cursor is not None:
params['cursor'] = cursor

response = (await self.http.get(
endpoint,
params=params,
headers=self._base_headers
)).json()
previous_cursor = response['previous_cursor']
next_cursor = response['next_cursor']

return Result(
response['ids'],
partial(self._get_friendship_ids, user_id,
screen_name, count, endpoint, next_cursor),
next_cursor,
partial(self._get_friendship_ids, user_id,
screen_name, count, endpoint, previous_cursor),
previous_cursor
)

async def get_followers_ids(
self,
user_id: str | None = None,
screen_name: str | None = None,
count: int = 5000,
cursor: str | None = None
) -> Result[int]:
"""
Fetches the IDs of the followers of a specified user.
Parameters
----------
user_id : :class:`str` | None, default=None
The ID of the user for whom to return results.
screen_name : :class:`str` | None, default=None
The screen name of the user for whom to return results.
count : :class:`int`, default=5000
The maximum number of IDs to retrieve.
Returns
-------
:class:`Result`[:class:`int`]
A Result object containing the IDs of the followers.
"""
return await self._get_friendship_ids(
user_id,
screen_name,
count,
Endpoint.FOLLOWERS_IDS,
cursor
)

async def get_friends_ids(
self,
user_id: str | None = None,
screen_name: str | None = None,
count: int = 5000,
cursor: str | None = None
) -> Result[int]:
"""
Fetches the IDs of the friends (following users) of a specified user.
Parameters
----------
user_id : :class:`str` | None, default=None
The ID of the user for whom to return results.
screen_name : :class:`str` | None, default=None
The screen name of the user for whom to return results.
count : :class:`int`, default=5000
The maximum number of IDs to retrieve.
Returns
-------
:class:`Result`[:class:`int`]
A Result object containing the IDs of the friends.
"""
return await self._get_friendship_ids(
user_id,
screen_name,
count,
Endpoint.FRIENDS_IDS,
cursor
)

async def _send_dm(
self,
conversation_id: str,
Expand Down
5 changes: 5 additions & 0 deletions twikit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,13 @@ class Endpoint:
HOME_TIMELINE = 'https://twitter.com/i/api/graphql/-X_hcgQzmHGl29-UXxz4sw/HomeTimeline'
HOME_LATEST_TIMELINE = 'https://twitter.com/i/api/graphql/U0cdisy7QFIoTfu3-Okw0A/HomeLatestTimeline'
FOLLOWERS = 'https://twitter.com/i/api/graphql/gC_lyAxZOptAMLCJX5UhWw/Followers'
FOLLOWERS2 = 'https://api.twitter.com/1.1/followers/list.json'
FOLLOWERS_IDS = 'https://api.twitter.com/1.1/followers/ids.json'
FRIENDS_IDS = 'https://api.twitter.com/1.1/friends/ids.json'
INCOMING_FRIENDSHIPS = 'https://api.twitter.com/1.1/friendships/incoming.json'
BLUE_VERIFIED_FOLLOWERS = 'https://twitter.com/i/api/graphql/VmIlPJNEDVQ29HfzIhV4mw/BlueVerifiedFollowers'
FOLLOWING = 'https://twitter.com/i/api/graphql/2vUj-_Ek-UmBVDNtd8OnQA/Following'
FOLLOWING2 = 'https://api.twitter.com/1.1/friends/list.json'
FOLLOWERS_YOU_KNOW = 'https://twitter.com/i/api/graphql/f2tbuGNjfOE8mNUO5itMew/FollowersYouKnow'
SUBSCRIPTIONS = 'https://twitter.com/i/api/graphql/Wsm5ZTCYtg2eH7mXAXPIgw/UserCreatorSubscriptions'
SEND_DM = 'https://twitter.com/i/api/1.1/dm/new2.json'
Expand Down

0 comments on commit 4019384

Please sign in to comment.