Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Script para saber quien twittea más en tu timeline
- * por jojo - www.eljojo.net - 2 enero 2011
- *
- * Necesita de la osombrosa clase de @abraham: twitteroauth. https://github.com/abraham/twitteroauth
- * nota: modificar twitteroauth/twitteroauth.php:138 -> json_decode($response, true); //con ",true" :D
- *
- * sería bacán orientarlo a objetos para trabajar más comodamente.
- */
- require_once('twitteroauth/twitteroauth.php');
- require_once('config.php');
- $access_token = array(
- 'oauth_token'=>'', //estos datos los tienes que poner tu.
- 'oauth_token_secret'=>'' //tambien tienes que ponerlos en el config.php.
- );
- /* Create a TwitterOauth object with consumer/user tokens. */
- $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
- /* If method is set change API call made. Test is called by default. */
- $tweets = array();
- $usernames = array();
- $neededUsernames=array();
- $counter = 0;
- $usercounter =0;
- $permanentfail = false;
- $lastfail = false;
- for($page=1;$page<=100;$page++) {
- if($permanentfail == true) continue; //ver linea 38
- $args = array('count'=>200,'page'=>$page,'trim_user'=>1,'include_entities'=>0);
- if(isset($content[99]['id'])) $args['max_id'] = $content[99]['id']-1;
- echo "fetcheando página $page\n";
- $content = $connection->get('statuses/home_timeline',$args);
- if(count ($content) == 0) {
- if($lastfail == true) $permanentfail = true;
- $lastfail = true;
- continue;
- }else{
- $counter = $counter + count($content);
- }
- foreach($content as $tweet) {
- $id = $tweet['user']['id'];
- if(!isset($usernames[$id]) && !isset($neededUsernames[$id])) {
- $neededUsernames[$id] = $id;
- if(count($neededUsernames) == 100) {
- echo "\t fetcheando usernames dentro\n";
- $usernamesImploded = implode(',', $neededUsernames);
- $users = $connection->get('users/lookup', array('user_id'=>$usernamesImploded));
- //print_r($users);
- foreach($users as $user) {
- $usernames[$user['id']] = $user['screen_name'];
- }
- $neededUsernames = array();
- $usercounter = $usercounter+100;
- }
- }
- if(!isset($tweets[$id])) $tweets[$id] =0;
- $tweets[$id]++;
- }
- }
- if(count($neededUsernames)>0) {
- echo "fetcheando usernames al final\n";
- $usernamesImploded = implode(',', $neededUsernames);
- $users = $connection->get('users/lookup', array('user_id'=>$usernamesImploded));
- //print_r($users);
- foreach($users as $user) {
- $usernames[$user['id']] = $user['screen_name'];
- }
- $usercounter = $usercounter+count($neededUsernames);
- }
- foreach ($tweets as $key=>$value) {
- $resultado[$usernames[$key]] = $value;
- }
- arsort($resultado);
- echo "¡listo! $counter tweets y $usercounter usernames fetcheados.\n";
- print_r($resultado);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement