xnite

twitterdump.php

May 5th, 2013
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. if(!$argv[1]) { die("DIAF\n"); }
  4. //https://dev.twitter.com/docs/working-with-timelines
  5. function min_by_key($arr, $key){
  6.     $min = array();
  7.     foreach ($arr as $val) {
  8.         if (!isset($val[$key]) and is_array($val)) {
  9.             $min2 = min_by_key($val, $key);
  10.             $min[$min2] = 1;
  11.         } elseif (!isset($val[$key]) and !is_array($val)) {
  12.             return false;
  13.         } elseif (isset($val[$key])) {
  14.             $min[$val[$key]] = 1;
  15.         }
  16.     }
  17.     return min( array_keys($min) );
  18. }
  19.  
  20. $username = $argv[1];
  21. $maxid = "";
  22. while(true)
  23. {
  24.     $url = "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=".$username."&count=200";
  25.  
  26.     if(strlen($maxid) > 0)
  27.     {$url .= "&max_id=".$maxid;}
  28.  
  29.     $data = file_get_contents($url);
  30.     $result = json_decode($data, true);
  31.  
  32.     if(count($result) == 1)
  33.     {
  34.         break;
  35.     }
  36.     if(count($result) > 0)
  37.     {
  38.         $maxid = number_format(min_by_key($result,'id_str') - 2, 0, '.', '');
  39.  
  40.         foreach ($result as $data)
  41.         {
  42.             echo $data['created_at'] ." ". $data['id_str'] ." ". $data['text'] . "\n";
  43.         }
  44.     }
  45.     else
  46.     {
  47.         break;
  48.     }
  49. }
  50.  
  51. ?>
Add Comment
Please, Sign In to add comment