Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
d60 authored Mar 30, 2024
1 parent 339901f commit 0809142
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 52 deletions.
44 changes: 35 additions & 9 deletions twikit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ def search_tweet(
<Tweet id="...">
...
...
>>> latest_tweets = tweets.next() # Retrieve latest tweets
"""
product = product.capitalize()

Expand All @@ -481,11 +483,14 @@ def search_tweet(
items = items[0]['content']['items']

next_cursor = None
previous_cursor = None

results = []
for item in items:
if item['entryId'].startswith('cursor-bottom'):
next_cursor = item['content']['value']
if item['entryId'].startswith('cursor-top'):
previous_cursor = item['content']['value']
if not item['entryId'].startswith(('tweet', 'search-grid')):
continue
tweet_info = find_dict(item, 'result')[0]
Expand All @@ -496,16 +501,21 @@ def search_tweet(

if next_cursor is None:
if product == 'Media':
next_cursor = find_dict(
entries = find_dict(
instructions, 'entries'
)[0][-1]['content']['value']
)[0]
next_cursor = entries[-1]['content']['value']
previous_cursor = entries[-2]['content']['value']
else:
next_cursor = instructions[-1]['entry']['content']['value']
previous_cursor = instructions[-2]['entry']['content']['value']

return Result(
results,
lambda:self.search_tweet(query, product, count, next_cursor),
next_cursor
next_cursor,
lambda:self.search_tweet(query, product, count, previous_cursor),
previous_cursor
)

def search_user(
Expand Down Expand Up @@ -1362,19 +1372,26 @@ def _get_tweet_engagements(
return Result([])
items = items_[0]
next_cursor = items[-1]['content']['value']
previous_cursor = items[-2]['content']['value']

results = []
for item in items:
if not item['entryId'].startswith('user'):
continue
user_info = find_dict(item, 'result')[0]
user_info_ = find_dict(item, 'result')
if not user_info_:
continue
user_info = user_info_[0]
results.append(User(self, user_info))

return Result(
results,
lambda:self._get_tweet_engagements(
tweet_id, count, next_cursor, endpoint),
next_cursor
next_cursor,
lambda:self._get_tweet_engagements(
tweet_id, count, previous_cursor, endpoint),
previous_cursor
)

def get_retweeters(
Expand Down Expand Up @@ -1541,6 +1558,7 @@ def get_user_tweets(

items = instructions[-1]['entries']
next_cursor = items[-1]['content']['value']
previous_cursor = items[-2]['content']['value']

if tweet_type == 'Media':
if cursor is None:
Expand Down Expand Up @@ -1587,7 +1605,10 @@ def get_user_tweets(
results,
lambda:self.get_user_tweets(
user_id, tweet_type, count, next_cursor),
next_cursor
next_cursor,
lambda:self.get_user_tweets(
user_id, tweet_type, count, previous_cursor),
previous_cursor
)

def get_timeline(
Expand Down Expand Up @@ -2017,6 +2038,7 @@ def get_bookmarks(
return Result([])
items = items_[0]
next_cursor = items[-1]['content']['value']
previous_cursor = items[-2]['content']['value']

results = []
for item in items:
Expand All @@ -2029,7 +2051,9 @@ def get_bookmarks(
return Result(
results,
lambda:self.get_bookmarks(count, next_cursor),
next_cursor
next_cursor,
lambda:self.get_bookmarks(count, previous_cursor),
previous_cursor
)

def delete_all_bookmarks(self) -> Response:
Expand Down Expand Up @@ -2786,7 +2810,8 @@ def get_dm_history(
))
return Result(
messages,
lambda:self.get_dm_history(user_id, messages[-1].id)
lambda:self.get_dm_history(user_id, messages[-1].id),
messages[-1].id
)

def send_dm_to_group(
Expand Down Expand Up @@ -2901,7 +2926,8 @@ def get_group_dm_history(
))
return Result(
messages,
lambda:self.get_group_dm_history(group_id, messages[-1].id)
lambda:self.get_group_dm_history(group_id, messages[-1].id),
messages[-1].id
)

def get_group(self, group_id: str) -> Group:
Expand Down
60 changes: 51 additions & 9 deletions twikit/twikit_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ async def search_tweet(
<Tweet id="...">
...
...
>>> latest_tweets = await tweets.next() # Retrieve latest tweets
"""
product = product.capitalize()

Expand All @@ -484,11 +486,14 @@ async def search_tweet(
items = items[0]['content']['items']

next_cursor = None
previous_cursor = None

results = []
for item in items:
if item['entryId'].startswith('cursor-bottom'):
next_cursor = item['content']['value']
if item['entryId'].startswith('cursor-top'):
previous_cursor = item['content']['value']
if not item['entryId'].startswith(('tweet', 'search-grid')):
continue
tweet_info = find_dict(item, 'result')[0]
Expand All @@ -499,19 +504,29 @@ async def search_tweet(

if next_cursor is None:
if product == 'Media':
next_cursor = find_dict(
entries = find_dict(
instructions, 'entries'
)[0][-1]['content']['value']
)[0]
next_cursor = entries[-1]['content']['value']
previous_cursor = entries[-2]['content']['value']
else:
next_cursor = instructions[-1]['entry']['content']['value']
previous_cursor = instructions[-2]['entry']['content']['value']

async def _fetch_next_result():
return await self.search_tweet(query, product, count, next_cursor)

async def _fetch_previous_result():
return await self.search_tweet(
query, product, count, previous_cursor
)

return Result(
results,
_fetch_next_result,
next_cursor
next_cursor,
_fetch_previous_result,
previous_cursor
)

async def search_user(
Expand Down Expand Up @@ -1381,23 +1396,34 @@ async def _get_tweet_engagements(
return Result([])
items = items_[0]
next_cursor = items[-1]['content']['value']
previous_cursor = items[-2]['content']['value']

results = []
for item in items:
if not item['entryId'].startswith('user'):
continue
user_info = find_dict(item, 'result')[0]
user_info_ = find_dict(item, 'result')
if not user_info_:
continue
user_info = user_info_[0]
results.append(User(self, user_info))

async def _fetch_next_result():
return await self._get_tweet_engagements(
tweet_id, count, next_cursor, endpoint
)

async def _fetch_previous_result():
return await self._get_tweet_engagements(
tweet_id, count, previous_cursor, endpoint
)

return Result(
results,
_fetch_next_result,
next_cursor
next_cursor,
_fetch_previous_result,
previous_cursor
)

async def get_retweeters(
Expand Down Expand Up @@ -1566,6 +1592,7 @@ async def get_user_tweets(

items = instructions[-1]['entries']
next_cursor = items[-1]['content']['value']
previous_cursor = items[-2]['content']['value']

if tweet_type == 'Media':
if cursor is None:
Expand Down Expand Up @@ -1613,10 +1640,17 @@ async def _fetch_next_result():
user_id, tweet_type, count, next_cursor
)

async def _fetch_previous_result():
return await self.get_user_tweets(
user_id, tweet_type, count, previous_cursor
)

return Result(
results,
_fetch_next_result,
next_cursor
next_cursor,
_fetch_previous_result,
previous_cursor
)

async def get_timeline(
Expand Down Expand Up @@ -2054,6 +2088,7 @@ async def get_bookmarks(
return Result([])
items = items_[0]
next_cursor = items[-1]['content']['value']
previous_cursor = items[-2]['content']['value']

results = []
for item in items:
Expand All @@ -2066,10 +2101,15 @@ async def get_bookmarks(
async def _fetch_next_result():
return await self.get_bookmarks(count, next_cursor)

async def _fetch_previous_result():
return await self.get_bookmarks(count, previous_cursor)

return Result(
results,
_fetch_next_result,
next_cursor
next_cursor,
_fetch_previous_result,
previous_cursor
)

async def delete_all_bookmarks(self) -> Response:
Expand Down Expand Up @@ -2836,7 +2876,8 @@ async def _fetch_next_result():

return Result(
messages,
_fetch_next_result
_fetch_next_result,
messages[-1].id
)

async def send_dm_to_group(
Expand Down Expand Up @@ -2955,7 +2996,8 @@ async def _fetch_next_result():

return Result(
messages,
_fetch_next_result
_fetch_next_result,
messages[-1].id
)

async def get_group(self, group_id: str) -> Group:
Expand Down
20 changes: 10 additions & 10 deletions twikit/twikit_async/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ async def unmute(self) -> Response:
"""
return await self._client.unmute_user(self.id)

async def get_followers(self, count: int = 20) -> list[User]:
async def get_followers(self, count: int = 20) -> Result[User]:
"""
Retrieves a list of followers for the user.
Expand All @@ -289,7 +289,7 @@ async def get_followers(self, count: int = 20) -> list[User]:
Returns
-------
list[User]
Result[User]
A list of User objects representing the followers.
See Also
Expand All @@ -298,7 +298,7 @@ async def get_followers(self, count: int = 20) -> list[User]:
"""
return await self._client.get_user_followers(self.id, count)

async def get_verified_followers(self, count: int = 20) -> list[User]:
async def get_verified_followers(self, count: int = 20) -> Result[User]:
"""
Retrieves a list of verified followers for the user.
Expand All @@ -309,7 +309,7 @@ async def get_verified_followers(self, count: int = 20) -> list[User]:
Returns
-------
list[User]
Result[User]
A list of User objects representing the verified followers.
See Also
Expand All @@ -318,7 +318,7 @@ async def get_verified_followers(self, count: int = 20) -> list[User]:
"""
return await self._client.get_user_verified_followers(self.id, count)

async def get_followers_you_know(self, count: int = 20) -> list[User]:
async def get_followers_you_know(self, count: int = 20) -> Result[User]:
"""
Retrieves a list of followers whom the user might know.
Expand All @@ -329,7 +329,7 @@ async def get_followers_you_know(self, count: int = 20) -> list[User]:
Returns
-------
list[User]
Result[User]
A list of User objects representing the followers you might know.
See Also
Expand All @@ -338,7 +338,7 @@ async def get_followers_you_know(self, count: int = 20) -> list[User]:
"""
return await self._client.get_user_followers_you_know(self.id, count)

async def get_following(self, count: int = 20) -> list[User]:
async def get_following(self, count: int = 20) -> Result[User]:
"""
Retrieves a list of users whom the user is following.
Expand All @@ -349,7 +349,7 @@ async def get_following(self, count: int = 20) -> list[User]:
Returns
-------
list[User]
Result[User]
A list of User objects representing the users being followed.
See Also
Expand All @@ -358,7 +358,7 @@ async def get_following(self, count: int = 20) -> list[User]:
"""
return await self._client.get_user_following(self.id, count)

async def get_subscriptions(self, count: int = 20) -> list[User]:
async def get_subscriptions(self, count: int = 20) -> Result[User]:
"""
Retrieves a list of users whom the user is subscribed to.
Expand All @@ -369,7 +369,7 @@ async def get_subscriptions(self, count: int = 20) -> list[User]:
Returns
-------
list[User]
Result[User]
A list of User objects representing the subscribed users.
See Also
Expand Down
Loading

0 comments on commit 0809142

Please sign in to comment.