Querying Most Popular Tweets using Tweepy V2 API - python

I am scraping Twitter tweets using the Tweepy V2 API, but am having trouble querying for more popular tweets with higher retweets and likes.
My application requires that I ignore retweets and replies for tweets via -is:retweet -is:reply. I am searching for the original tweets themselves.
However, I currently have numerous tweets which have very low likes and retweets, which is inadequate for my application.
Is there any method to query based on popularity or retweets count in Tweepy V2? The popularity filter seems to only exist in Tweepy V1 (Is there a way to search for Top tweets with tweepy instead of latest tweets?).
Code is shown below:
query_str = "#chosen_topic lang:en -is:retweet -is:reply"
client = tweepy.Client(bearer_token=config.BEARER_TOKEN, consumer_key=
config.CONSUMER_KEY,consumer_secret= config.CONSUMER_SECRET,access_token=
config.ACCESS_TOKEN,access_token_secret= config.ACCESS_TOKEN_SECRET)
for tweet_batch in tweepy.Paginator(client.search_all_tweets, query=query_str,
tweet_fields=['context_annotations','created_at', 'public_metrics','author_id', 'lang', 'geo', 'entities'],
user_fields=['name','username','location','verified','description'],
max_results=100, expansions='author_id'):
tweet_data = tweet_batch.data
for tweet in tweet_data:
print(tweet.public_metrics['reply_count'])

You can add the the sort_order='relevancy' argument to your search (see here).
I don't know if this is the exact equivalent of the result_type='popular' argument in the V1 API, but it seems to be the best way to do what you're asking for in the V2 API.

Related

Retrieve viral tweets topic from Twitter API

I'm trying to retrieve viral tweets for an academic project using the Twitter API v2. There is a Topic on Twitter called "viral tweets" which is perfect for what I want.
It has id 1284234742661963776 so my thoughts was to query using context annotations. However there is no entity related to viral tweets on the API. I've tried to input this entity id using context id 131 which corresponds to the Twitter Unified Taxonomy, but got nothing. Did someone manage to retrieve them? Are not all topics on Twitter available on the API?
Here is an example of the code I used
Not all topics available on Twitter are part of the context annotations provided via the Twitter API. You can read more about this and see an example in this answer.

get replys to a specific tweet with twitter api v2

anyone knows how to get a the relys for a specific tweet by it's ID maybe? i been trying but can't find any on the tweepy docs not discord's, can't seem to find anything about how to do it with twitter api v2, also is there a way to requests latest tweets from multiple users in bulk?
import tweepy
twitter_client = tweepy.Client(bearer_token=bearer)
all_tweets = twitter_client.get_users_tweets(id=(1334299956241461248,1346973288040263680,154449778,1516418305455763466))
print(all_tweets)
this is how i try it, but it's returning me an error
The `id` query parameter value [(1334299956241461248, 1346973288040263680, 154449778, 1516418305455763466)] is not valid
any help appreiciated, and thank you
and i found the answer at the end
def check_replys(tweet_ID):
query = f"conversation_id:{tweet_ID} is:reply"
replys= twitter_client.search_recent_tweets(query= query )
return replys
you can find more info about making a query at https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-query
You're asking two questions:
How to get replies by Tweet ID
How to lookup the latest Tweets for several users
For 1 - you can search for Tweets by conversation ID. The conversation ID is the ID of the original Tweet that led to the replies. The only wrinkle here is that by default (v2 Essential access) you can only search for Tweets within the past 7 days, so if the Tweet you want replies for is older than that, you'll need Academic access for full archive search.
For 2 - you cannot pass multiple values to the id parameter all at once. If you look at the Tweepy documentation and at the Twitter API docs, you'll see that id gets substituted into the URL path for the call, so you have to call the API multiple times, one for each user ID. That should be possible using a loop.

Get users tweets excluding retweets using tweepy cursor

I want to extract exactly 400 tweets for each user whose id is in a list.
I am doing this using Tweepy and Cursor and my code looks like the following:
for user_id in users:
for tweet in tweepy.Cursor(
api.user_timeline,
id=user_id
).items(400)
The code above retrieves all the recent (400) tweets but it also includes retweets which I don't want included.
Retweets can be filtered using if hasattr(tweet, 'retweeted_status') but in case the user has retweets, the code will only return (400 - retweets) tweets.
As far as I know, there isn't an option in Cursor to exclude the retweets. Is there a way I can pull this off?
Replying as this has been solved. The python-twitter API wrapper provides a parameter include_rts=False that filters out the retweets

Twitter API - Retrieve all replies to a certain tweet

I am trying to use Twitter API with the Python wrapper Twython and I want to retrieve all replies (the comments below a tweet) to a certain tweet find using some patterns.
At the moment to achieve this, I perform the search of a string, I retrieve the screen_name field of user field in the response, related to the original tweets and then I use again the API in order to search the latest tweets directed to the user, using in the query the substring to:screen_name.
Is there a better solution? The only questions related to this topic that I found were written in '14 and I hope that, in the mean time, there were some improvements.

Count of retweets with tweepy with RESTful API?

So I am doing fairly trivial task of retrieving tweets from a particular user. What I really want to do is to count how many times each of his tweets have been retweeted. I have looked at python's tweepy's API, and I have not been able to find a clear answer. Is there a way to do this in python?
tweepy, might not have method for every endpoint in the twitter API.
tweepy should allow you to call an arbitrary endpoint, though. When in doubt, you can check out the official twitter API to see what is offered.
https://dev.twitter.com/docs/api/1.1
It appears that you might only be able to get 100 retweets for a given tweet_id
https://dev.twitter.com/docs/api/1.1/get/statuses/retweets/%3Aid
https://dev.twitter.com/docs/api/1.1/get/statuses/retweeters/ids

Categories