Changeset 58 in lab


Ignore:
Timestamp:
Oct 4, 2010 5:03:42 PM (14 years ago)
Author:
mitty
Message:
  • get user's timeline
    • usage: user_timeline.pl screen_name [number_of_pages|all [dump]]
File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/twitter/user_timeline.pl

    r56 r58  
    1313use FindBin qw($Bin); 
    1414use YAML::Tiny; 
     15use Data::Dumper; 
     16use Encode; 
     17 
     18if (@ARGV < 1) { 
     19    die "usage: $0 screen_name [number_of_pages|all [dump]]\n"; 
     20} 
     21my $screen_name = $ARGV[0]; 
     22my $pages = $ARGV[1] || 1; 
     23if ($pages eq 'all') { 
     24    $pages = -1; 
     25} 
     26my $dump = $ARGV[2] || 0; 
    1527 
    1628my $conf = loadconf("$Bin/config.yml"); 
     
    2638 
    2739eval { 
    28     foreach my $id (@ARGV) { 
    29         my $res = $bot->show_status($id); 
    30         use Data::Dumper; 
    31         foreach my $line (split /\n/, Dumper $res) { 
    32             if ($line =~ /undef/) { next; } 
    33             unless ($line =~ / => {/ 
    34                 ||  $line =~ / = / 
    35                 ||  $line =~ /status/ 
    36                 ||  $line =~ /'text'/ 
    37                 ||  $line =~ /created/ 
    38                 ||  $line =~ /'id'/ 
    39                 ||  $line =~ /name/ 
    40                 ||  $line =~ / },/ 
    41                 ||  $line =~ / };/ 
    42             ) { next; } 
    43             print $line, "\n"; 
     40    my $page = 0; 
     41    while ($pages - $page && $page <= 160) { 
     42        $page++; 
     43        my $res = $bot->user_timeline( 
     44            { 
     45                screen_name => $screen_name, 
     46                page        => $page, 
     47            } 
     48        ); 
     49         
     50        if ($dump) { 
     51            foreach my $line (split /\n/, Dumper $res) { 
     52                if ($line =~ /undef/) { next; } 
     53                print $line, "\n"; 
     54            } 
     55        } 
     56        else { 
     57            foreach my $status (@{$res}) { 
     58                my $text = ""; 
     59                $text .= $status->{user}{name}; 
     60                $text .= " [" . $status->{created_at} . "]"; 
     61                $text .= " (". $status->{id} . ")"; 
     62                $text .= " ". encode('utf8', $status->{text}); 
     63                $text =~ s/\n//; 
     64                print $text, "\n"; 
     65            } 
    4466        } 
    4567    } 
Note: See TracChangeset for help on using the changeset viewer.