Get users tweets excluding retweets using tweepy cursor - python

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

Related

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.

Querying Most Popular Tweets using Tweepy V2 API

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.

Get live tweets from a specific user list... getting duplicates tweepy python

I have seen here, here and here.
I have a list of twitter users I want to stream live tweets for. But I am getting duplicate tweets. And the tweets are not live per se.
Here is the code:
users_to_follow = ['twitterid_1', 'twitterid_2', 'twitterid_3']
mystream = tweepy.Stream(self.auth, self.listener)
try:
mystream.filter(follow=users_to_follow)
except:
print("error!")
mystream.disconnect()
It is bringing back the tweets but the same tweets are being duplicated. What am I doing wrong?
Cheers
Per the Twitter documentation on the follow parameter:
follow
A comma-separated list of user IDs, indicating the users whose Tweets
should be delivered on the stream. Following protected users is not
supported. For each user specified, the stream will contain:
Tweets created by the user.
Tweets which are retweeted by the user.
Replies to any Tweet created by the user.
Retweets of any Tweet created by the user.
Manual replies, created without pressing a reply
button (e.g. “#twitterapi I agree”).
The stream will not contain:
Tweets mentioning the user (e.g. “Hello #twitterapi!”).
Manual Retweets created without pressing a Retweet button (e.g. “RT
#twitterapi The API is great”).
Tweets by protected users.
When you say that "the same Tweets are being duplicated", do you mean that you're seeing the same Tweet IDs multiple times?
You also mentioned that the "Tweets are not live" but it is not clear what you mean by this.

Find location from tweet ID

I have a list of tweets. For each tweet I have different attributes (user, date, text and tweet IDs).
To scrape that data, I’m using the project of Jefferson Henrique (https://github.com/Jefferson-Henrique/GetOldTweets-python).
In addition to that, I would like to know two geographical elements for each tweet:
where tweets were generated (location or long, lat)?
where the user resides?
Do you have any idea to get those two information either from tweet IDs or something else?
You might want to post the json file of the tweet to let me locate where the information is.
From my experience, the location of the tweet may not always be available, depending on whether the user allows location sharing when tweeting.
For the user location, it is usually not in the tweet. Scrape the user profile and you should easily find it.
You could query the Twitter API directly using the tweet id. This would allow you to retrieve more data about the tweet, including the location if available.
According to the Twitter API documentation:
If the Tweet is geo-tagged, there will a "place" object included.

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.

Categories