diff --git a/readme.md b/readme.md index b964a5e..766e046 100644 --- a/readme.md +++ b/readme.md @@ -347,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 cf3cf51..177abe2 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 ) @@ -806,6 +809,23 @@ def notifications(self, params: dict = None) -> dict: log(self.logger, self.debug, r) return r.json() + # 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( f'{self.v1_api}/users/recommendations.json', 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