简体   繁体   中英

Retrieving a list of users who interacted with a Twitter account?

I have been working on this project for quite some time at work. Let me explain the goal first: We have four Twitter accounts (irrelevant for the code) and there is a guy in our office who spends hours weeding through the interactions per account. He jots down the handles (or screen_name according the API) of anyone who 1. mentions our account 2. favorites a tweet of ours 3. retweets one of our tweets. I am working with 4 different files (one per Twitter account) so essentially just need to focus on one for now.

Unnecessary information: We are checking to see when the last #ff was tweeted because after gathering all the handles, the guy in our office tweets back saying something like #ff thank you @foo @bar for interacting with us. So once it finds a tweet containing #ff in a list of recent tweets it gets the date of that tweet. Simply put, we are gathering all of the interactions after this date (weekly basis in a way).

<?php
require_once('./top.php');

// oAuth library
$twitter = new TwitterAPIExchange($valpo_settings);

// get recents
$string_recents = json_decode($twitter->setGetfield($getfield.$valpo_id)->buildOauth($url_recents, 'GET')->performRequest(), $assoc = true);

// get mentions
$string_mentions = json_decode($twitter->setGetfield($getfield.$valpo_id)->buildOauth($url_mentions, 'GET')->performRequest(), $assoc = true);

// get retweets
$string_retweets = json_decode($twitter->setGetfield($getfield.$valpo_id)->buildOauth($url_retweets, 'GET')->performRequest(), $assoc = true);

// error (recents)
if($string_recents["errors"][0]["message"] != "") {echo "<h1>error (recents)</h1><p>".$string_recents[errors][0]["message"]."</p>";exit();}

// error (mentions)
if($string_mentions["errors"][0]["message"] != "") {echo "<h1>error (mentions)</h1><p>".$string_mentions[errors][0]["message"]."</p>";exit();}

// error (retweets)
if($string_retweets["errors"][0]["message"] != "") {echo "<h1>error (mentions)</h1><p>".$string_retweets[errors][0]["message"]."</p>";exit();}

// filter recents
foreach($string_recents as $items_recents) {
    // echo "<pre>";
    // print_r($items_recents);
    // echo "</pre>";

    // filter hashtags
    foreach($items_recents['entities']['hashtags'] as $items_hashtags) {
        $hashtag_text = strtolower($items_hashtags['text']);

        if ($hashtag_text == $hashtag && $vi != 1) {
            $date = date('z', strtotime($items_recents['created_at']));
            $time = date('H-i-s', strtotime($items_recents['created_at']));
            $vi = 1; // prevents finding the date of previous tweets
        }
    }
}

// filter mentions
foreach($string_mentions as $items_mentions) {
    if (date('z', strtotime($items_mentions['created_at'])) == $date && date('H-i-s', strtotime($items_mentions['created_at'])) >= $time || date('z', strtotime($items_mentions['created_at'])) > $date) {
        $names[] .= $items_mentions['user']['screen_name'];
        $vxm++;
    }
}

// filter retweets
foreach($string_retweets as $items_retweets) {
    echo "<pre>";
    print_r($items_retweets);
    echo "</pre>";

    foreach ($items_retweets['entities']['user_mentions'] as $items_retweets_user) {
        if (date('z', strtotime($items_retweets['created_at'])) == $date && date('H-i-s', strtotime($items_retweets['created_at'])) >= $time || date('z', strtotime($items_retweets['created_at'])) > $date) {
            $names[] .= $items_retweets_user['screen_name'];
            $vxr++;
        }
    }
}

// eliminates duplicate handles
$unames = array_unique($names);

// creates directory if needed
if (!is_dir("./".date('Y-m-d'))) {
    mkdir("./".date('Y-m-d'), 0777, true);
}

$file = fopen("./".date('Y-m-d')."/".date('H-i-s')."-valpo.txt", "w");

foreach ($unames as $value) {
    fwrite($file, '@'.$value.' ');
    $unames_size += strlen($value) + 2; // + 2 accomadates for @ and a space
}

fclose($file);

$unames_size = ceil($unames_size / 140);

echo "
            <div class='row'>
                <div class='col x3'></div>

                <div class='col x6'>
                    <span class='title'>ValpoLife</span>

                    <span class='info'>
                        <p><i class='fa fa-comments'></i> ".$vxm." &nbsp; <i class='fa fa-star'></i> ".$vxm." &nbsp; <i class='fa fa-retweet'></i> ".$vxr." &nbsp; <i class='fa fa-user'></i> ".count($unames)."</p>
                        <p><i class='fa fa-slack'></i> ".$hashtag." &nbsp; <i class='fa fa-calendar-o'></i> ".($curdate - $date)." days ago</p>
                        <p>Appoximately ".$unames_size." tweet(s) needed</p>
                    </span>

                    <a href='./index.php' class='button sec animated' target='blank'><i class='fa fa-chevron-left'></i></a><a href='./".date('Y-m-d')."/".date('H-i-s')."-valpo.txt' class='button pri animated' target='blank'><i class='fa fa-file-text'></i> View handles</a>
                </div>
            </div>

            <footer>
                &copy; Ideas In Motion Media. All Rights Reserved.
            </footer>
        </div>
    </body>
</html>
";
?>

Above is all the code for one of the account's files. As you can see, it is including top.php which simply has some global variables, the header for the html, and the oAuth information per account. Now everything is working except for retrieving favorites and retweets. Mentions are working fine and so are recents. I have tried several things to get favorites or retweets to work but nothing seems to do it. I have eliminated the code for the favorites above as I am more concerned about getting retweets. If the favorites code is needed please let me know as I am happy to share it. Thank you for any help.

tl;dr

Using the Twitter API, I have successfully retrieved recent tweets and any tweet that mentions our Twitter account. Now I am trying to retrieve the names / handles of people who favorite or retweet one of our tweets.

It is possible to get the names of everyone who retweeted a Tweet.

https://dev.twitter.com/rest/reference/get/statuses/retweeters/ids

Pass it an Tweet's ID and you'll get the list of people who retweeted it.

For favourites, you're not going to like this answer. There's no REST API which will let you see who has favourited a Tweet.

You can use the Streaming API to receive a message whenever your Tweets are marked as a Favourite.

https://dev.twitter.com/streaming/overview/messages-types#Events_event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM