From f6cdf0c6c4ea5ba3d3082cd6fb9b57b499baebfd Mon Sep 17 00:00:00 2001 From: Madhav Date: Tue, 17 Jun 2025 22:37:18 +0530 Subject: [PATCH 1/3] notifications type: all, verified, mentions --- twitter/account.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/twitter/account.py b/twitter/account.py index cf3cf51..e17f94b 100644 --- a/twitter/account.py +++ b/twitter/account.py @@ -796,9 +796,12 @@ def clear_draft_tweets(self) -> None: if _id != user_id: self.gql('POST', Operation.DeleteDraftTweet, {'draft_tweet_id': _id}) - def notifications(self, params: dict = None) -> dict: + def notifications(self, params: dict = None, type: str = "all") -> dict: + """ + type: all, verified, mentions + """ r = self.session.get( - f'{self.v2_api}/notifications/all.json', + f'{self.v2_api}/notifications/{type}.json', headers=get_headers(self.session), params=params or live_notification_params ) From 9b4eea0e3ca5b833d360bc3f90582d1f8f13ca88 Mon Sep 17 00:00:00 2001 From: Madhav Date: Fri, 20 Jun 2025 15:49:30 +0530 Subject: [PATCH 2/3] Added get_user_mentions() functionality --- readme.md | 3 +++ twitter/account.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/readme.md b/readme.md index b964a5e..9398e9f 100644 --- a/readme.md +++ b/readme.md @@ -82,6 +82,9 @@ account.unbookmark(123456) account.pin(123456) account.unpin(123456) +# get self user mentions +account.get_user_mentions(x_handle="MY_X_HANDLE") + # users account.follow(1234) account.unfollow(1234) diff --git a/twitter/account.py b/twitter/account.py index e17f94b..a21ea57 100644 --- a/twitter/account.py +++ b/twitter/account.py @@ -809,6 +809,23 @@ def notifications(self, params: dict = None, type: str = "all") -> dict: log(self.logger, self.debug, r) return r.json() + def get_user_mentions(self, x_handle: str, last_mention_id: int = 0) -> dict: + """ + Monitor user mentions via 'mentions' type notifications. + """ + last_id = last_mention_id + notifications = self.notifications(type='mentions') + mentions = notifications.get('globalObjects', {}).get('tweets', {}) + new_mentions = [] + for tweet_id, tweet in mentions.items(): + if last_id and int(tweet_id) <= int(last_id): + continue + if f'@{x_handle.lower()}' in tweet.get('full_text', '').lower(): + new_mentions.append((tweet_id, tweet)) + # Sort by tweet_id ascending (oldest first) + new_mentions.sort(key=lambda x: int(x[0])) + return new_mentions + def recommendations(self, params: dict = None) -> dict: r = self.session.get( f'{self.v1_api}/users/recommendations.json', From 2214dad98c6c8523e3f8492120e16df0a41a9c5d Mon Sep 17 00:00:00 2001 From: Madhav Date: Tue, 1 Jul 2025 12:12:12 +0530 Subject: [PATCH 3/3] get_user_mentions via search instead of notifications --- readme.md | 15 ++++++++++++--- twitter/account.py | 32 ++++++++++++++++---------------- twitter/search.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/readme.md b/readme.md index 9398e9f..766e046 100644 --- a/readme.md +++ b/readme.md @@ -82,9 +82,6 @@ account.unbookmark(123456) account.pin(123456) account.unpin(123456) -# get self user mentions -account.get_user_mentions(x_handle="MY_X_HANDLE") - # users account.follow(1234) account.unfollow(1234) @@ -350,6 +347,18 @@ res = search.run( ) ``` +#### Monitor User Mentions + +```python +from twitter.search import Search + +email, username, password = ..., ..., ... +search = Search(email, username, password, save=True, debug=1) +user_mentions = search.get_user_mentions(x_handle="MY_X_HANDLE", search_count=30) +for tweet in user_mentions: + pritn(tweet) +``` + **Search Operators Reference** https://developer.twitter.com/en/docs/twitter-api/v1/rules-and-filtering/search-operators diff --git a/twitter/account.py b/twitter/account.py index a21ea57..177abe2 100644 --- a/twitter/account.py +++ b/twitter/account.py @@ -809,22 +809,22 @@ def notifications(self, params: dict = None, type: str = "all") -> dict: log(self.logger, self.debug, r) return r.json() - def get_user_mentions(self, x_handle: str, last_mention_id: int = 0) -> dict: - """ - Monitor user mentions via 'mentions' type notifications. - """ - last_id = last_mention_id - notifications = self.notifications(type='mentions') - mentions = notifications.get('globalObjects', {}).get('tweets', {}) - new_mentions = [] - for tweet_id, tweet in mentions.items(): - if last_id and int(tweet_id) <= int(last_id): - continue - if f'@{x_handle.lower()}' in tweet.get('full_text', '').lower(): - new_mentions.append((tweet_id, tweet)) - # Sort by tweet_id ascending (oldest first) - new_mentions.sort(key=lambda x: int(x[0])) - return new_mentions + # def get_new_user_mentions_outdated(self, x_handle: str, last_mention_id: int = 0) -> dict: + # """ + # Monitor user mentions via 'mentions' type notifications. + # """ + # last_id = last_mention_id + # notifications = self.notifications(type='mentions') + # mentions = notifications.get('globalObjects', {}).get('tweets', {}) + # new_mentions = [] + # for tweet_id, tweet in mentions.items(): + # if last_id and int(tweet_id) <= int(last_id): + # continue + # if f'@{x_handle.lower()}' in tweet.get('full_text', '').lower(): + # new_mentions.append((tweet_id, tweet)) + # # Sort by tweet_id ascending (oldest first) + # new_mentions.sort(key=lambda x: int(x[0])) + # return new_mentions def recommendations(self, params: dict = None) -> dict: r = self.session.get( diff --git a/twitter/search.py b/twitter/search.py index 2291741..476b255 100644 --- a/twitter/search.py +++ b/twitter/search.py @@ -174,3 +174,34 @@ def save_cookies(self, fname: str = None): """ Save cookies to file """ cookies = self.session.cookies Path(f'{fname or cookies.get("username")}.cookies').write_bytes(orjson.dumps(dict(cookies))) + + def get_user_mentions( + self, + x_handle: str, + search_count: int = 20 + ) -> list: + """ + Fetches user mentions via search method. + + Parameters + ---------- + x_handle : :class:`str` + The target user handle (eg: 5mknc5, elonmusk). + search_count : :class:`int`, default=20 + The number of latest tweets to retrieve in each request. + + Returns + ------- + A list of tweets mentions `x_handle`. + """ + result = self.run( + limit=search_count, + retries=5, + queries=[ + { + 'category': 'Latest', + 'query': f'@{x_handle}', + }, + ] + ) + return result