Advertisement
Guest User

Script para saber quien twittea más en tu timeline

a guest
Jan 1st, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Script para saber quien twittea más en tu timeline
  4.  * por jojo - www.eljojo.net - 2 enero 2011
  5.  *
  6.  * Necesita de la osombrosa clase de @abraham: twitteroauth. https://github.com/abraham/twitteroauth
  7.  * nota: modificar twitteroauth/twitteroauth.php:138 -> json_decode($response, true); //con ",true" :D
  8.  *
  9.  * sería bacán orientarlo a objetos para trabajar más comodamente.
  10.  */
  11.  
  12.  
  13. require_once('twitteroauth/twitteroauth.php');
  14. require_once('config.php');
  15.  
  16. $access_token = array(
  17.     'oauth_token'=>'', //estos datos los tienes que poner tu.
  18.     'oauth_token_secret'=>'' //tambien tienes que ponerlos en el config.php.   
  19. );
  20.  
  21. /* Create a TwitterOauth object with consumer/user tokens. */
  22. $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
  23.  
  24. /* If method is set change API call made. Test is called by default. */
  25. $tweets = array();
  26. $usernames = array();
  27. $neededUsernames=array();
  28. $counter = 0;
  29. $usercounter =0;
  30. $permanentfail = false;
  31. $lastfail = false;
  32. for($page=1;$page<=100;$page++) {
  33.     if($permanentfail == true) continue; //ver linea 38
  34.     $args = array('count'=>200,'page'=>$page,'trim_user'=>1,'include_entities'=>0);
  35.     if(isset($content[99]['id'])) $args['max_id'] = $content[99]['id']-1;
  36.     echo "fetcheando página $page\n";
  37.     $content = $connection->get('statuses/home_timeline',$args);
  38.     if(count ($content) == 0) {
  39.         if($lastfail == true) $permanentfail = true;
  40.         $lastfail = true;
  41.         continue;
  42.     }else{
  43.          $counter = $counter + count($content);
  44.     }
  45.     foreach($content as $tweet) {
  46.         $id = $tweet['user']['id'];
  47.         if(!isset($usernames[$id]) && !isset($neededUsernames[$id])) {
  48.             $neededUsernames[$id] = $id;
  49.             if(count($neededUsernames) == 100) {
  50.                 echo "\t fetcheando usernames dentro\n";
  51.                 $usernamesImploded = implode(',', $neededUsernames);
  52.                 $users = $connection->get('users/lookup', array('user_id'=>$usernamesImploded));
  53.                 //print_r($users);
  54.                 foreach($users as $user) {
  55.                     $usernames[$user['id']] = $user['screen_name'];
  56.                 }
  57.                 $neededUsernames = array();
  58.                 $usercounter = $usercounter+100;
  59.             }
  60.         }
  61.         if(!isset($tweets[$id])) $tweets[$id] =0;
  62.         $tweets[$id]++;
  63.     }
  64. }
  65.  
  66. if(count($neededUsernames)>0) {
  67.     echo "fetcheando usernames al final\n";
  68.     $usernamesImploded = implode(',', $neededUsernames);
  69.     $users = $connection->get('users/lookup', array('user_id'=>$usernamesImploded));
  70.     //print_r($users);
  71.     foreach($users as $user) {
  72.         $usernames[$user['id']] = $user['screen_name'];
  73.     }
  74.     $usercounter = $usercounter+count($neededUsernames);
  75. }
  76.  
  77. foreach ($tweets as $key=>$value) {
  78.     $resultado[$usernames[$key]] = $value;
  79. }
  80. arsort($resultado);
  81. echo "¡listo! $counter tweets y $usercounter usernames fetcheados.\n";
  82. print_r($resultado);
  83.  
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement