How to use Tweepy to retweet with a comment - python

So i am stuck trying to figure out how to retweet a tweet with a comment, this was added to twitter recently.
this is when you click retweet and add a comment to the retweet and retweet it.
basically this is what i am talking about :
i was looking at the api and count find a method dedicated to this. And even the retweet method does not have a parameter where i can pass text.
So i was wondering is there a way to do this?

Tweepy doesn't have functionality to retweet with your own text, but what you can do is make a url like this https://twitter.com/<user_displayname>/status/<tweet_id> and include it with the text you want comment. It's not a retweet but you are embedding the tweet in your new tweet.
user_displayname - display name of person, whose tweet you are retweeting
tweet_id - tweet id of tweet you are retweeting

September 2021 Update
Tweepy does have the functionality to quote retweet. Just provide the url of the tweet you want to quote into attachment_url of the API.update_status method.
Python example:
# Get the tweet you want to quote
tweet_to_quote_url="https://twitter.com/andypiper/status/903615884664725505"
# Quote it in a new status
api.update_status("text", attachment_url=tweet_to_quote_url)
# Done!

In the documentation, there is a quote_tweet_id parameter in create_tweet method.
You can create a new tweet with the tweet ID of the tweet you want to quote.
comment = "Yep!"
quote_tweet = 1592447141720780803
client = tweepy.Client(bearer_token=access_token)
client.create_tweet(text=comment, quote_tweet_id=quote_tweet, user_auth=False)

Related

How do i reply to a certain tweet using Tweepy?

I am trying to reply to create a code to reply to simply reply to a (given) tweet, i only know that api.update_status tweets but not how to reply to one, i did some researches but all are outdated and "in_reply_to_status_id" doesn't seem to work anymore
api = tweepy.API(auth)
api.update_status
In order to reply to a tweet using the Twitter API v2, you need to use the in_reply_to_tweet_id. This works as follows.
import tweepy
client = tweepy.Client(consumer_key='YOUR_CONSUMER_KEY',
consumer_secret='YOUR_CONSUMER_SECRET',
access_token='YOUR_ACCESS_TOKEN',
access_token_secret='YOUR_ACCESS_TOKEN_SECRET')
client.create_tweet(text='Some reply', in_reply_to_tweet_id=42)
This is described in the documentation here.
in_reply_to_tweet_id (Optional[Union[int, str]]) – Tweet ID of the Tweet being replied to.
Edit: If you are using API v1.1, then you need to include #username in the status text, where username is the author of the referenced Tweet. This is described here.
Hence, a working example looks as follows.
comment = '#usernameOfTweet Test Reply'
res = api.update_status(comment, in_reply_to_status_id=42)
It is also possible to use auto_populate_reply_metadata=True as discussed in this Github issue.
comment = 'Test reply'
api.update_status(comment, in_reply_to_status_id=42, auto_populate_reply_metadata=True)
This is also described in the documentation.
auto_populate_reply_metadata – If set to true and used with in_reply_to_status_id, leading #mentions will be looked up from the original Tweet, and added to the new Tweet from there.

Tweepy api.get_status().in_reply_to_status_id_str returns None

No matter what tweet I use when I call myTweet.text it returns the text for the tweet I want but when I call myTweet.in_reply_to_status_id_str it returns None even if there are replies to the tweet.
Am I using it wrong? This is the only way I can find online of how to get the replies to a tweet. Thank you in advance for your help, the code:
api = tweepy.API(auth)
myTweet = api.get_status('id')
print(myTweet)
print(myTweet.text)
print('---', myTweet.in_reply_to_status_id_str)
The in_reply_to_status_id_str attribute will contain the string representation of the original Tweet’s ID if the represented Tweet is a reply. See https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object.
Twitter's API does not have a direct method/endpoint to retrieve replies to a specific Tweet.
Instead, you'll have to use the search APIs and filter the results manually.

twitter tweet_mode = 'extended' not just giving me the text in the tweet

I'm trying to download tweets using tweepy. But the tweets keep getting cut off.
results = api.search(q=hashtag, lang="en", count=num, tweet_mode="extended")
for tweet in results:
tweet_list.append(tweet.full_text)
I end up getting outputs looking like this:
RT #Acosta: Trump also said at the meeting “why do we need more Haitians? Take them out,” a person familiar with today’s meeting confirms t…
I just want the actual full text part of the tweet.
Already answered here
Instead of full_text=True you need tweet_mode="extended"
Then, instead of text you should use full_text to get the full tweet text.
Your code should look like:
new_tweets = api.user_timeline(screen_name = screen_name,count=200, tweet_mode="extended")
Then in order to get the full tweets text:
tweets = [[tweet.full_text] for tweet in new_tweets]

Retweets of my tweets

So I am trying to find out retweets of my tweets using tweepy
# To get first tweet
firstTweet = api.user_timeline("zzaibis")[0]
# then getting the retweet data using tweet id and it gives me the results
resultsOfFirstTweet = api.retweets(firstTweet.id)
# but when I try to find any other tweet except first tweet, this returns nothing.
secondTweet = api.user_timeline("zzaibis")[1]
Any idea why this is not working beyond that, and also what I need to follow to get all the retweet data of my tweets considering that I don't have limited tweets and retweets in my account.
To find the number of retweets a user gets on each tweet from their timeline try the following code:
for tweet in api.user_timeline(screen_name = 'StackOverflow', count = 10):
print(tweet.retweet_count)
To edit the user who is being searched change the "screen_name" to the username of the person you want to search. To change the number of statuses is seaches change the "count".
you can also print information like the tweet id of the tweet being searched using:
print(tweet.id)

Original tweet or retweeted?

I am using Tweepy with python and trying to get the original tweets authored by set of users (i.e., I want to exclude any tweet in their timeline that is actually a retweet). How can I do this with Tweepy?
I tried something like this and I do not know if it works:
tweets = api.user_timeline(id=user['id'], count=30)
for tweet in tweets:
if not tweet.retweeted:
analyze_tweet(tweet)
Does the api.user_timeline() return only original tweets? Or retweets of this user as well?
Tweepy by default doesn't include retweets in user_timeline therefore tweet.retweeted will always be false. To include retweets you can specify include_rts as True like
tweets= api.user_timeline(id=user['id'], count=30,include_rts=True)
for tweet in tweets:
if not tweet.retweeted:
analyze_tweet(tweet)
else:
#do something with retweet

Categories

Resources