def search_content( self, content: List[str], *, max_tweets: Optional[int] = MAX_TWEETS, lang: Optional[str] = None, allow_retweets: bool = True, collecting: bool = False, callback: Callable = dummy_callback, ) -> Optional[Corpus]: """ Search recent tweets by content (keywords). Parameters ---------- content A list of key-words to search for. max_tweets Limits the number of downloaded tweets. If none use APIs maximum. lang A language's code (either ISO 639-1 or ISO 639-3 formats). allow_retweets Whether to download retweets. collecting Whether to collect results across multiple search calls. callback Function to report the progress Returns ------- Corpus with tweets """ if not collecting: self.reset() max_tweets = max_tweets or MAX_TWEETS def build_query(): assert len(content) > 0, "At leas one keyword required" q = " OR ".join(['"{}"'.format(q) for q in content]) if not allow_retweets: q += " -is:retweet" if lang: q += f" lang:{lang}" return q paginator = tweepy.Paginator( self.api.search_recent_tweets, build_query(), **request_settings ) count = self._fetch(paginator, max_tweets, callback=callback) self.append_history("Content", content, lang or "Any", allow_retweets, count) return self._create_corpus()
def get_tweets(searchtext, limit=100): client = tweepy.Client(bearer_token=get_twitter_bearer_token()) tweets = [] for i, tweet in enumerate( tweepy.Paginator(client.search_recent_tweets, searchtext, tweet_fields=[ 'author_id', 'public_metrics', 'lang', 'attachments' ], user_fields=['public_metrics'], max_results=100).flatten(limit=limit)): tweets.append(tweet) return tweets
def search_authors( self, authors: List[str], *, max_tweets: Optional[int] = MAX_TWEETS, collecting: bool = False, callback: Callable = dummy_callback, ) -> Optional[Corpus]: """ Search recent tweets by authors. Parameters ---------- authors A list of authors to search for. max_tweets Limits the number of downloaded tweets. If none use APIs maximum. collecting Whether to collect results across multiple search calls. callback Function to report the progress Returns ------- Corpus with tweets """ if not collecting: self.reset() count_sum = 0 n = len(authors) for i, author in enumerate(authors): author_ = self.api.get_user(username=author) if author_.data is None: raise NoAuthorError(author) paginator = tweepy.Paginator( self.api.get_users_tweets, author_.data.id, **request_settings ) count_sum += self._fetch( paginator, max_tweets, callback=wrap_callback(callback, i / n, (i + 1) / n), ) self.append_history("Author", authors, None, None, count_sum) return self._create_corpus()
# for user in page: # print(".") # ret = batch.append(str(user.id)) # client.add_list_member(id=list.id, user_id=user.id, user_auth=True) # print(ret) # separator = ',' # batch_users = separator.join(batch) # print(len(batch)) # print(batch_users) # for page in tweepy.Cursor(client.get_users_following, screen_name=screen_name, # count=100).pages(10): # for user in page: # print(f"{user.id} {user.screen_name}") friends = 0 for response in tweepy.Paginator(client.get_users_following, id=58932896, max_results=1000, limit=5): # print(response.meta) # print(response) for user in response.data: print(user.id) client.add_list_member(id=list_id, user_id=user.id, user_auth=True) time.sleep(3) friends += 1 print(f"added {friends} members to list") # len(response.data) # for user in response.data: # print(user.id) # ret = api.add_list_members(list_id=list.id, user_id=batch_users) # print(ret)
response = client.get_user(username=user_to_mute) user = response.data print(f"id of user '@{user_to_mute}': {user.id}") print("-" * 50) # mute a user result = client.mute(target_user_id=user.id) print(f"user muted? {result.data['muting']}") print("-" * 50) # get a list of all muted users muted = [] for response in tweepy.Paginator(client.get_muted, max_results=1000, limit=10): for user in response.data: muted.append(f"@{user.username} - {user.name} - {user.id}") print("Muted users:") for entry in muted: print(entry) print("-" * 50) # unmute a user result = client.unmute(target_user_id=user.id) print(f"user muted? {result.data['muting']}") print("-" * 50)
response = client.get_user(username=user_to_follow) user = response.data print(f"id of user '@{user_to_follow}': {user.id}") print("-" * 50) # follow a user result = client.follow_user(target_user_id=user.id) print(f"user followed? {result.data['following']}") print("-" * 50) # get a list of all users PythonFriday is following friends = [] for response in tweepy.Paginator(client.get_users_following, id=1492915170166943745, max_results=1000, limit=10): for user in response.data: friends.append(f"@{user.username} - {user.name} - {user.id}") print("Friends:") for entry in friends: print(entry) print("-" * 50) # unfollow a user result = client.unfollow_user(target_user_id=user.id) print(f"user followed? {result.data['following']}") print("-" * 50)
def get_tweet_timeline(id, from_date, to_date): timeline = tweepy.Paginator(client.get_users_tweets, id=me.data.id, max_results=10, limit=1).flatten() response = client.get_tweet(id)
api_bearer_token = api_keys["bearer_token"] return api_bearer_token def get_user_id(client, username): user = client.get_user(username=username) return user.data.id client = tw.Client(bearer_token=get_api_auth_token()) user_id = get_user_id(client, USERNAME) # get the tweets tweets = tw.Paginator( client.get_users_tweets, id=user_id, tweet_fields=['id', 'public_metrics', 'created_at', 'text'], exclude='retweets', max_results=100).flatten(limit=5000) tweet_data = [[ tweet.id, tweet.public_metrics['like_count'], tweet.public_metrics['retweet_count'], tweet.public_metrics['quote_count'], tweet.created_at, tweet.text ] for tweet in tweets] # use pandas to get the top 5 tweet_frame = pd.DataFrame( data=tweet_data, columns=['id', 'likes', 'retweets', 'quotes', 'created_at', 'text']) tweet_frame['created_date'] = tweet_frame['created_at'].dt.date tweet_frame['total_retweets'] = tweet_frame['retweets'] + tweet_frame['quotes']
import tweepy def read_twitter_credentials(filename: Union[str, Path]) -> str: if isinstance(filename, str): filename = Path(filename) filename = filename.expanduser() if not filename.exists(): raise ValueError(f"file {str(filename)} not found") try: with filename.open() as stream: return yaml.safe_load(stream) except: return None if __name__ == "__main__": credentials = read_twitter_credentials( "~/Documents/Config/twitter-credentials.yml") if not credentials: raise RuntimeError("Can't get the bearer token") bearer_token = credentials.get("bearer_token") client = tweepy.Client(bearer_token) for tweet in tweepy.Paginator(client.search_recent_tweets, "#Russia", max_results=100).flatten(limit=250): print(tweet.id, tweet.text) # EOF