Telling Whether A Tweet Is A Retweet Or Not?

2019-01-26 05:33发布

I'm using the Twitter API in order to pull tweets from specific users. I have it working exactly as I want, except for being able to tell whether or not a specific tweet is original from the user or if it is a retweet.

I'm using the following call: https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=philiprucker&count=1

When looking at the results, it appears that I should be able to pull retweeted from the results and it should return true or false. However, this is only returning the string retweet.

$url = "http://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name=$screen_name&count=200" ;
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
curl_close ($ch);

$twelement = new SimpleXMLElement($xml);
foreach ($twelement->status as $status) {
    $text = dbEscape(trim($status->text));
    $time = strtotime($status->created_at);
    $id = $status->id;
    $num_retweets = $status->retweet_count;
    $retweet = $status->retweeted;
    dbQuery("INSERT INTO `twitter` (`id`,`screen_name`,`time`,`text`,`hidden`, `numRetweets`, `retweet`) VALUES ('$id','$screen_name','$time','$text','n','$num_retweets','retweet')");
    // dbQuery("INSERT INTO `twitter` (`id`,`screen_name`,`time`,`text`,`hidden`) VALUES ('$id','$screen_name','$time','$text','n')");
}

This is the code doing what I describe. I believe all the relevant code is there. Any help would be greatly appreciated!

标签: api twitter
4条回答
乱世女痞
2楼-- · 2019-01-26 06:11

You are specifically putting the string retweet into the table, not the value of $retweet.

查看更多
在下西门庆
3楼-- · 2019-01-26 06:14

Quick clarifying question: are you looking to store whether a tweet has ever been retweeted, or are you looking to store whether the user making the request retweeted the tweet?

The "retweeted" boolean field attached to a status is perspectival to the user making the request -- it indicates whether the current user retweeted the tweet or not, not whether the tweet has been retweeted. A non-zero retweet_count would be a better indicator as to whether the tweet has been retweeted or not.

查看更多
我只想做你的唯一
4楼-- · 2019-01-26 06:29

In case anyone is still reading this, I think that perhaps this thread gets to the point more succinctly, and is more correct for the current api (1.1) https://stackoverflow.com/a/24041978/1449799

tl;dr check for existence of retweeted_status on the status object.

查看更多
ら.Afraid
5楼-- · 2019-01-26 06:34

Using a simple GET request along with Yahoo Query Language (aka YQL) to access the retweeted node can be achieved like this:

jsFiddle DEMO: YQL Rest Query for Twitter Retweeted Status via JSON

The callback json results are shown below:

cbfunc({
 "query": {
  "count": 1,
  "created": "2012-12-31T09:51:58Z",
  "lang": "en-US",
  "results": {
   "json": {
    "retweeted": "false"
   }
  }
 }
});

And you can also have that returned as xml too:

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="1" yahoo:created="2012-12-31T09:54:49Z" yahoo:lang="en-US">
    <results>
        <json>
            <retweeted>false</retweeted>
        </json>
    </results>
</query>

Here's how the YQL Statement would look like to return the retweeted node with ether true or false as the result:

SELECT retweeted FROM json WHERE url="https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=philiprucker&count=1"

And that simple GET request is just a ajax(); request with success function like this:

// The YQL Statement used below is shown next, starting with the word SELECT:
//
// SELECT retweeted FROM json WHERE url="https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=philiprucker&count=1"
//
// View the above YQL Statement using Yahoo Console at: 
// http://developer.yahoo.com/yql/console/?q=SELECT%20retweeted%20FROM%20json%20WHERE%20url%3D%22https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fuser_timeline.json%3Finclude_entities%3Dtrue%26include_rts%3Dtrue%26screen_name%3Dphiliprucker%26count%3D1%22&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
//
// The variable 'q' below is the Yahoo Rest Query. That is provided after creating the above Yahoo Statement.
// It's provided at the bottom of that web page.

var q = 'http://query.yahooapis.com/v1/public/yql?q=SELECT%20retweeted%20FROM%20json%20WHERE%20url%3D%22https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fuser_timeline.json%3Finclude_entities%3Dtrue%26include_rts%3Dtrue%26screen_name%3Dphiliprucker%26count%3D1%22&format=json';

// Use simple jQuery .ajax along with the above YQL Rest Statement.
// The benefit of YQL rest statement is we can select the desired node, in this case "retweeted"
$.ajax({
    url: q,
    dataType: "json",
    success: function(data) {

        // Enable to show the jQuery data Object received in the browsers console.
        //console.log(data);

        // If we have data, continue.
        if (data) {

            // Display retweeted value of 'true' or 'false' via browser alert.
            alert('The retweeted status is: ' +  data.query.results.json.retweeted );

        }

    }
});

EDIT 2: Retrieve Global Retweeted Value.

Reference: OP's Tweet with Viewable Retweet Count

jsFiddle DEMO: YQL Rest Query for Twitter Global Retweeted Status via HTML

This time, the total retweeted global count of that tweet will be data-scrapped directly from the Twitter page with the tweet. This method does not require to visit the original tweet, as any retweet will show this global count.

To verify that the jsFiddle is showing the actual retweeted count, reference the OP's tweet page using the provided link above.

The process is similar to what's described above, except this time our .ajax() dataType is html and the YQL Statement is as follows:

SELECT * FROM html WHERE url="https://twitter.com/PostBaron/status/286544211556319233" AND xpath="//a[@class='request-retweeted-popup']"

Notice in the above YQL Statement that XPATH is also used to access the specific data required. This prevents from downloading the whole HTML webpage, and just that specific data for Global Tweet Count is accessed. This is possible since that value is in the webpage anchor with unique classname request-retweeted-popup. To discover that classname quickly, I used Inspect Element tool that the browser has when I hovered over that value on the webpage. Once the classname was known, I fined-tuned the response to return the value in the <strong> tags which is the number we want to know.

Naturally, a value of 0 means the tweet has never been retweeted but more importantly signifies that this is the original tweet itself.

The end result is no Twitter API is required since your data-scraping the actual public webpage in order to get the Global Tweet Count.

$.ajax({
    url: q,
    dataType: "html",
    success: function(data) {

        // If we have data, continue.
        if ( $(data).find('.request-retweeted-popup') ) {

            // Store the value of total retweets in this variable.
            var totalRetweets = $(data).find('strong').text();

            // Show browser alert of the totalRetweets value. Change alert() to console.log() for console log browser responses.
            alert('The following Tweet has been retweeted: ' + totalRetweets +  ' times');

        }

    }
});
查看更多
登录 后发表回答