From 45836c80b1c963bee5467ba00a63a90e0fa85863 Mon Sep 17 00:00:00 2001 From: Nikolaos Pothitos Date: Sun, 21 Apr 2024 15:43:26 +0000 Subject: [PATCH 1/6] Prevent IndexError while searching tweets --- twikit/client.py | 5 ++++- twikit/twikit_async/client.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/twikit/client.py b/twikit/client.py index d9c89470..3a34e41b 100644 --- a/twikit/client.py +++ b/twikit/client.py @@ -481,7 +481,10 @@ 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'] user_info = tweet_info['core']['user_results']['result'] diff --git a/twikit/twikit_async/client.py b/twikit/twikit_async/client.py index cbff6a16..1186925f 100644 --- a/twikit/twikit_async/client.py +++ b/twikit/twikit_async/client.py @@ -485,7 +485,10 @@ 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'] user_info = tweet_info['core']['user_results']['result'] From de7b2d1f76b2e1c5561fc91ddcdca809a835f165 Mon Sep 17 00:00:00 2001 From: Nikolaos Pothitos Date: Sun, 21 Apr 2024 15:56:53 +0000 Subject: [PATCH 2/6] Prevent KeyError while initializing tweets --- twikit/tweet.py | 8 ++++---- twikit/twikit_async/tweet.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/twikit/tweet.py b/twikit/tweet.py index bfb965b9..e07e945f 100644 --- a/twikit/tweet.py +++ b/twikit/tweet.py @@ -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. @@ -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. @@ -147,7 +147,7 @@ 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') @@ -155,7 +155,7 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None: 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: diff --git a/twikit/twikit_async/tweet.py b/twikit/twikit_async/tweet.py index 5ed871e7..ac465962 100644 --- a/twikit/twikit_async/tweet.py +++ b/twikit/twikit_async/tweet.py @@ -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. @@ -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. @@ -144,7 +144,7 @@ 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') @@ -152,7 +152,7 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None: 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: From 3bc1c9825608822073122cce114f68df0c7ad76c Mon Sep 17 00:00:00 2001 From: Nikolaos Pothitos Date: Mon, 22 Apr 2024 19:28:20 +0000 Subject: [PATCH 3/6] Prevent KeyError while searching tweets --- twikit/client.py | 2 ++ twikit/twikit_async/client.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/twikit/client.py b/twikit/client.py index 3a34e41b..797dda5b 100644 --- a/twikit/client.py +++ b/twikit/client.py @@ -487,6 +487,8 @@ def search_tweet( tweet_info = tweet_info[0] if 'tweet' in tweet_info: tweet_info = tweet_info['tweet'] + 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))) diff --git a/twikit/twikit_async/client.py b/twikit/twikit_async/client.py index 1186925f..ac38b656 100644 --- a/twikit/twikit_async/client.py +++ b/twikit/twikit_async/client.py @@ -491,6 +491,8 @@ async def search_tweet( tweet_info = tweet_info[0] if 'tweet' in tweet_info: tweet_info = tweet_info['tweet'] + 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))) From 21b1ab34adaab008299141c3e0cd80719844329b Mon Sep 17 00:00:00 2001 From: Nikolaos Pothitos Date: Mon, 22 Apr 2024 19:55:55 +0000 Subject: [PATCH 4/6] Prevent IndexError while searching tweets --- twikit/client.py | 5 ++++- twikit/twikit_async/client.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/twikit/client.py b/twikit/client.py index 797dda5b..91f2a4f3 100644 --- a/twikit/client.py +++ b/twikit/client.py @@ -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] diff --git a/twikit/twikit_async/client.py b/twikit/twikit_async/client.py index ac38b656..a5861797 100644 --- a/twikit/twikit_async/client.py +++ b/twikit/twikit_async/client.py @@ -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] From 1aec953aed73b44545fa7cc893a24c9a67984635 Mon Sep 17 00:00:00 2001 From: Nikolaos Pothitos Date: Tue, 23 Apr 2024 05:47:37 +0000 Subject: [PATCH 5/6] Prevent KeyError while initializing tweet --- twikit/client.py | 3 ++- twikit/twikit_async/client.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/twikit/client.py b/twikit/client.py index 91f2a4f3..27d79ea7 100644 --- a/twikit/client.py +++ b/twikit/client.py @@ -493,7 +493,8 @@ def search_tweet( 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': diff --git a/twikit/twikit_async/client.py b/twikit/twikit_async/client.py index a5861797..ddeee10b 100644 --- a/twikit/twikit_async/client.py +++ b/twikit/twikit_async/client.py @@ -497,7 +497,8 @@ async def search_tweet( 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': From 90b4848299e0a6c1d45bee6b13ced6e966c7a618 Mon Sep 17 00:00:00 2001 From: Nikolaos Pothitos Date: Tue, 23 Apr 2024 13:54:05 +0000 Subject: [PATCH 6/6] Prevent KeyError while searching tweets --- twikit/client.py | 2 ++ twikit/twikit_async/client.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/twikit/client.py b/twikit/client.py index 27d79ea7..aa69ff34 100644 --- a/twikit/client.py +++ b/twikit/client.py @@ -490,6 +490,8 @@ def search_tweet( 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'] diff --git a/twikit/twikit_async/client.py b/twikit/twikit_async/client.py index ddeee10b..d141ea81 100644 --- a/twikit/twikit_async/client.py +++ b/twikit/twikit_async/client.py @@ -494,6 +494,8 @@ async def search_tweet( 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']