Twitter API: Check if a tweet is a retweet - twitter

I have found this question. However I think this has changed on API version 1.1.
If I use the search/tweets method, how can I see if the tweet is a RT?
I see that the retweeted field always returns false. Is there another field I can use for this answer?

If it's a retweet, the tweet will contain a property named retweeted_status. To be complete, retweeted_status will not appear if the tweet is not a retweet. More info at: Tweets.

By simply checking the property name
"retweeted_status"
if you does not find then it's not RT.

As #Joe Mayo said, check for the retweeted_status key on the status object.
I have an example that is not caught by #Apoorv Ashutosh's suggestion.
See: https://twitter.com/gppvt/status/474162466776449024 this will redirect (because it is a retweet) to the original tweet. However if you get this tweet through the twitter API, it has retweeted_status. Notice the text does not contain "RT".

The retweeted field is false if the retweet is not done using the retweet button but rahter via RT, so in such a case, just search the "text" field of all tweets for this pattern
RT #
This pattern can be of help, but I don't think there is any other function for this.

The retweeted_status property will exist if the received tweet was retweeted, else you will get the AttributeError error. In the case, you want to get rid of retweeted tweets:
def on_status(self, status):
try:
print "Retweeted ************* \n" + str(status.retweeted_status)
return
except AttributeError:
print "there is no attribut with name retweeted_status"

Just to add a bit more. (using twitter gem (ruby language))
You can check if its a retweet by checking the tweet and then getting what you need from the retweeted_status hash
t = client.status(#########) #function that obtains tweet based on ID where # = tweet ID
puts t.retweeted_status? # returns true or false
t.retweeted_status # returns the actual hash for that

As everyone else has mentioned, you can check to see if the retweeted_status property exists in the response subfield for that Tweet.
However, per the current version of the API, every Tweet object has the field retweeted that stores a boolean value (True or False) that will tell you if a Tweet has been retweeted.

Make sure your "re-tweet" is not actually a quote of an another tweet. In this case, I had to use the quoted_status field to get the original tweet, rather than the retweeted_status one.

To the ones who are using Hydrator1 and relying on its CSV export of the hydrated JSONL file: you can check reweet_id (no it's not a typo in version 0.0.12), and if it's NA then it's an original tweet as I found.
original_twitter_tweets = twitter_csv_data %>% filter(is.na(reweet_id))

I've noticed that retweets are distinguished from regular tweets by the line "RT #..."
The Python code following can be used to identify them:
tweettext = str( tweet.text.lower())
if tweettext.startswith("rt #") == True:
print(tweet.id)
Tweet object is the tweet variable.
Tweepy is another tool you may use to manage the Twitter API more easily.

Related

Can the time stamp of a Retweet be accessed?

I'm trying to use the Twitter API: GET statuses/retweets/:id
https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-statuses-retweets-id
It doesn't seem like the API returns the time a Tweet was retweeted. It only shows when it was created. Is this true? And if so, is there any other way of being able to retrieve the retweet timestamp?
From Introduction to Tweet JSON:
If you are working with a Retweet object, then that object will contain two Tweet objects, complete with two User objects. The Tweet that was Retweeted is referred to as the 'original' Tweet and is displayed under the 'retweeted_status' key. If a Retweet gets Retweeted, the 'retweet_status' will still point to the original Tweet, meaning the intermediate Retweet is not included.
The top-level created_at attribute should be the time of the retweet that you're looking for. The original tweet is embedded inside the retweet object as the retweeted_status and it has its own (earlier) timestamp.

How to find all 'retweet with comments' for a particular Tweet using API?

I want to get all the 'retweets with comments' of a tweet.
Here are few things I noticed with twitter api
Retweets with comments are treated as tweets. The retweet count does not increase if you add a comment, also the twitter message is "XYZ quoted you instead retweeted you'
You clearly can't use this API endpoint https://dev.twitter.com/rest/reference/post/statuses/retweet/:id
Is there a way to find all the 'tweet/retweet with comment' if you can supply the original Tweet/Id?
So you're referring to Quoted Tweets (retweet with comments). There is no official method for that from the REST API yet, however, there are couple of ways to do it.
Since all quoted tweets contain the short url to the original one,
you can still use in_reply_to_status_id and filter by short url of the original tweet
Search for tweets that contain the field quoted_status_id this can be done either through REST or STREAMING API.
quoted_status_id: This field only surfaces when the Tweet is a quote Tweet. This field contains the integer value Tweet ID of the quoted Tweet.
This isn't easy to accomplish. Here's the brute-force API method to find Quote Tweets.
Take your tweet id, it'll be something like 750176081987014657.
GET search/tweets
Use the Twitter API and search for the tweet.
https://dev.twitter.com/rest/reference/get/search/tweets
The GET request will look something like this:
https://api.twitter.com/1.1/search/tweets.json?q=750176081987014657&count=100
The q "query" argument is the tweet id. I've set the result count argument to the maximum (100).
Authorization
Of course with the Twitter API there's a whole bunch of tricky Authorization header work you have to do as well in order to complete the GET request. That's documented elsewhere and is beyond the scope of this answer.
Results and Conclusion
When you have the JSON results of this GET request, focus on the statuses collection. For each tweet in the collection (otherwise known as a "status"), check if it contains a quoted_status_id field. If it does, and the field value matches your tweet id, the tweet is a quote tweet. If it does not, it is simply a retweet with no added comment. You will also have to deal with iterating through the pagination of results if there are more than 100. That's done by looking for a search_metadata.next_results field and retrieving the next GET query string from it, which will be provided for you.

How do I properly 'Retweet with a comment' via Twitter's API

Twitter has recently implemented an automatic way of quoting tweets. It basically extracts an url from a tweet and shows it embedded within your tweet.
My problem is: how do I 'retweet with a comment' without showing the original tweet URL via API?
Retweeting from the website makes the RT look like this .
But when I'm manually prepending a tweet url to my tweet, it looks like this .
notice the literal url.
your tweet has a quoted_status, just put the tweet url as attachment_url args, e.g.
client.update('cool', attachment_url: 'https://twitter.com/emorima/status/1061581991798169600')
it's ruby code with twitter gem
My understanding is that you are achieving the closest solution that Twitter API offers. This thread talks about doing the following:
do a status update, with the 'status' field containing the text of the original tweet, followed by the link to the original tweet. The 'in_reply_to_status_id' field should not be set. This action does not result in the "retweet count" being increased.
do a retweet, which will increase the retweet count.
Based on lfx_cool's answer, I was able to achieve it with this:
statuses/update
post('https://api.twitter.com/1.1/statuses/update.json', {
status:'Some comment', attachment_url:`https://twitter.com/${user.screen_name}/status/${id_str}`,
});

Getting only the most recent tweet - Monkeh Tweets

I'm using ColdFusion MonkehTweets component. But actually, all I'm trying to do is get the single most recent tweet for #allblacks.
Here is the URL MonkehTweets generates. I have blanked out the keys etc for the purposes of this post:
https://api.twitter.com/1.1/search/tweets.json?checkheader=false&count=1&include_entities=true&lang=en&oauth_consumer_key=blah123&oauth_nonce=blah123&oauth_signature=blah123&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1378795729&oauth_token=blah123&oauth_version=1.0&q=from%253Aallblacks
This constantly returns the first Tweet on 7th September at https://twitter.com/allblacks
instead of the latest tweet.
Not sure what's going on and would appreciate some help please.
According to documentation: https://dev.twitter.com/docs/api/1.1/get/search/tweets you could use the optional parameter result_type with value recent. That should return only the most recent results in the response.
And I'd use #allblacks for the q parameter.

Twitter API - Display all tweets with a certain hashtag?

How would I go about displaying tweets that contain a certain hashtag using the Twitter API? Thanks
I'd also like to know if there is a way to get all tweets from a certain hashtag in a separate file, also the ones that don't show up in your feed anymore. I suppose that's what the earlier question was about, too.
This answer was written in 2010. The API it uses has since been retired. It is kept for historical interest only.
Search for it.
Make sure include_entities is set to true to get hashtag results. See Tweet Entities
Returns 5 mixed results with Twitter.com user IDs plus entities for the term "blue angels":
GET http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&with_twitter_user_id=true&result_type=mixed
UPDATE for v1.1:
Rather than giving q="search_string" give it q="hashtag" in URL encoded form to return results with HASHTAG ONLY. So your query would become:
GET https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames
%23 is URL encoded form of #. Try the link out in your browser and it should work.
You can optimize the query by adding since_id and max_id parameters detailed here. Hope this helps !
Note: Search API is now a OAUTH authenticated call, so please include your access_tokens to the above call
Updated
Twitter Search doc link:
https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
The answer here worked better for me as it isolates the search on the hashtag, not just returning results that contain the search string. In the answer above you would still need to parse the JSON response to see if the entities.hashtags array is not empty.

Resources