Skip to content

Commit

Permalink
Merge pull request #75 from viralerts/main
Browse files Browse the repository at this point in the history
Avoid exceptions while searching for tweets
  • Loading branch information
d60 authored Apr 23, 2024
2 parents 1068958 + 90b4848 commit 539b1a3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
17 changes: 14 additions & 3 deletions twikit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,10 @@ def search_tweet(
product = product.capitalize()

response = self._search(query, product, count, cursor)
instructions = find_dict(response, 'instructions')[0]
instructions = find_dict(response, 'instructions')
if not instructions:
return Result([])
instructions = instructions[0]

if product == 'Media' and cursor is not None:
items = find_dict(instructions, 'moduleItems')[0]
Expand All @@ -481,11 +484,19 @@ def search_tweet(
previous_cursor = item['content']['value']
if not item['entryId'].startswith(('tweet', 'search-grid')):
continue
tweet_info = find_dict(item, 'result')[0]
tweet_info = find_dict(item, 'result')
if not tweet_info:
continue
tweet_info = tweet_info[0]
if 'tweet' in tweet_info:
tweet_info = tweet_info['tweet']
if 'core' not in tweet_info:
continue
if 'result' not in tweet_info['core']['user_results']:
continue
user_info = tweet_info['core']['user_results']['result']
results.append(Tweet(self, tweet_info, User(self, user_info)))
if 'legacy' in tweet_info:
results.append(Tweet(self, tweet_info, User(self, user_info)))

if next_cursor is None:
if product == 'Media':
Expand Down
8 changes: 4 additions & 4 deletions twikit/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Tweet:
The count of favorites or likes for the tweet.
favorited : :class:`bool`
Indicates if the tweet is favorited.
view_count: :class:`int`
view_count: :class:`int` | None
The count of views.
retweet_count : :class:`int`
The count of retweets for the tweet.
Expand All @@ -65,7 +65,7 @@ class Tweet:
Indicates if the tweet is eligible for editing.
edits_remaining : :class:`int`
The remaining number of edits allowed for the tweet.
state : :class:`str`
state : :class:`str` | None
The state of the tweet views.
replies: Result[:class:`Tweet`] | None
Replies to the tweet.
Expand Down Expand Up @@ -147,15 +147,15 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None:
self.reply_count: int = legacy['reply_count']
self.favorite_count: int = legacy['favorite_count']
self.favorited: bool = legacy['favorited']
self.view_count: int = data['views'].get('count')
self.view_count: int = data['views'].get('count') if 'views' in data else None
self.retweet_count: int = legacy['retweet_count']
self.editable_until_msecs: int = data['edit_control'].get(
'editable_until_msecs')
self.is_translatable: bool = data.get('is_translatable')
self.is_edit_eligible: bool = data['edit_control'].get(
'is_edit_eligible')
self.edits_remaining: int = data['edit_control'].get('edits_remaining')
self.state: str = data['views'].get('state')
self.state: str = data['views'].get('state') if 'views' in data else None
self.has_community_notes: bool = data.get('has_birdwatch_notes')

if 'birdwatch_pivot' in data:
Expand Down
17 changes: 14 additions & 3 deletions twikit/twikit_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ async def search_tweet(
product = product.capitalize()

response = await self._search(query, product, count, cursor)
instructions = find_dict(response, 'instructions')[0]
instructions = find_dict(response, 'instructions')
if not instructions:
return Result([])
instructions = instructions[0]

if product == 'Media' and cursor is not None:
items = find_dict(instructions, 'moduleItems')[0]
Expand All @@ -485,11 +488,19 @@ async def search_tweet(
previous_cursor = item['content']['value']
if not item['entryId'].startswith(('tweet', 'search-grid')):
continue
tweet_info = find_dict(item, 'result')[0]
tweet_info = find_dict(item, 'result')
if not tweet_info:
continue
tweet_info = tweet_info[0]
if 'tweet' in tweet_info:
tweet_info = tweet_info['tweet']
if 'core' not in tweet_info:
continue
if 'result' not in tweet_info['core']['user_results']:
continue
user_info = tweet_info['core']['user_results']['result']
results.append(Tweet(self, tweet_info, User(self, user_info)))
if 'legacy' in tweet_info:
results.append(Tweet(self, tweet_info, User(self, user_info)))

if next_cursor is None:
if product == 'Media':
Expand Down
8 changes: 4 additions & 4 deletions twikit/twikit_async/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Tweet:
The count of favorites or likes for the tweet.
favorited : :class:`bool`
Indicates if the tweet is favorited.
view_count: :class:`int`
view_count: :class:`int` | None
The count of views.
retweet_count : :class:`int`
The count of retweets for the tweet.
Expand All @@ -64,7 +64,7 @@ class Tweet:
Indicates if the tweet is eligible for editing.
edits_remaining : :class:`int`
The remaining number of edits allowed for the tweet.
state : :class:`str`
state : :class:`str` | None
The state of the tweet views.
replies: Result[:class:`Tweet`] | None
Replies to the tweet.
Expand Down Expand Up @@ -144,15 +144,15 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None:
self.reply_count: int = legacy['reply_count']
self.favorite_count: int = legacy['favorite_count']
self.favorited: bool = legacy['favorited']
self.view_count: int = data['views'].get('count')
self.view_count: int = data['views'].get('count') if 'views' in data else None
self.retweet_count: int = legacy['retweet_count']
self.editable_until_msecs: int = data['edit_control'].get(
'editable_until_msecs')
self.is_translatable: bool = data.get('is_translatable')
self.is_edit_eligible: bool = data['edit_control'].get(
'is_edit_eligible')
self.edits_remaining: int = data['edit_control'].get('edits_remaining')
self.state: str = data['views'].get('state')
self.state: str = data['views'].get('state') if 'views' in data else None
self.has_community_notes: bool = data.get('has_birdwatch_notes')

if 'birdwatch_pivot' in data:
Expand Down

0 comments on commit 539b1a3

Please sign in to comment.