Changeset 50 in lab


Ignore:
Timestamp:
Sep 13, 2010 9:30:51 PM (14 years ago)
Author:
mitty
Message:
  • help script for Dump tweet information
File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/twitter/show_status.pl

    r49 r50  
    1818    die "$0: cannot parse config file.\n"; 
    1919} 
    20 my $stat = loadconf("$Bin/status.yml"); 
    21 if (! defined $stat) { 
    22     $stat = { 
    23         # do not set to 0 
    24         since_id => 1, 
    25     }; 
    26 } 
    2720 
    2821my $bot = login($conf); 
     
    3124} 
    3225 
    33 my %tweets; 
    34 my $tweet; 
    3526 
    36 $tweet = or_search($bot, $conf->{hashtag}, $stat->{since_id}); 
    37 if ($tweet) { 
    38     %tweets = (%tweets, %$tweet); 
     27eval { 
     28    foreach my $id (@ARGV) { 
     29        my $res = $bot->show_status($id); 
     30        use Data::Dumper; 
     31        print Dumper $res; 
     32    } 
     33}; 
     34if ($@) { 
     35    evalrescue($@); 
    3936} 
    40  
    41 $tweet = mentions_ids($bot, $stat->{since_id}); 
    42 if ($tweet) { 
    43     %tweets = (%tweets, %$tweet); 
    44 } 
    45  
    46 foreach my $id (sort keys %tweets) { 
    47     if ($tweets{$id} eq 'retweet') { 
    48         next; 
    49     } 
    50     sleep($conf->{sleep}); 
    51     # retweet found tweet 
    52     #   $tweets->{$id} eq 'search'  => found by search API 
    53     #                  eq 'mention' => found by mention API 
    54     my $res; 
    55     eval { 
    56         $res = $bot->retweet($id); 
    57     }; 
    58     if ($@) { 
    59         evalrescue($@); 
    60         warn "status_id => $id\n"; 
    61         next; 
    62     } 
    63      
    64     $stat->{since_id}   = $id; 
    65 } 
    66  
    67 if (%tweets) { 
    68     # save last status to yaml file 
    69     YAML::Tiny::DumpFile("$Bin/status.yml", $stat); 
    70 } 
     37print "done\n"; 
    7138 
    7239 
     
    10572} 
    10673 
    107 sub or_search { 
    108     # search tweets containing keywords 
    109     #   param   => Net::Twitter::Lite object, ArrayRef of keywords, since_id 
    110     #   ret     => HashRef of status_id (timeline order is destroyed) 
    111     #               or undef (none is found) 
    112      
    113     my $bot      = shift @_; 
    114     my $keywords = shift @_; 
    115     my $since_id = shift @_; 
    116      
    117     my $key = ""; 
    118     foreach my $word (@$keywords) { 
    119         if ($key) { 
    120             $key .= " OR $word"; 
    121         } 
    122         else { 
    123             $key = $word; 
    124         } 
    125     } 
    126      
    127     my $res; 
    128     my $ids = {}; 
    129     eval { 
    130         if ($key) { 
    131             $res = $bot->search( 
    132                 { 
    133                     q           => $key, 
    134                     since_id    => $since_id, 
    135                 } 
    136             ); 
    137         } 
    138         if ($res->{results}) { 
    139             foreach my $tweet (@{$res->{results}}) { 
    140                 my $res = $bot->show_status($tweet->{id}); 
    141                 if ($res->{retweeted_status}) { 
    142                     $ids->{$tweet->{id}} = 'retweet'; 
    143                 } 
    144                 else { 
    145                     $ids->{$tweet->{id}} = 'search'; 
    146                 } 
    147             } 
    148         } 
    149     }; 
    150     if ($@) { 
    151         evalrescue($@); 
    152     } 
    153      
    154     return $ids; 
    155 } 
    156  
    157 sub mentions_ids { 
    158     # return status_ids mentioned to me 
    159     #   param   => Net::Twitter::Lite object, since_id 
    160     #   ret     => HashRef of status_id (timeline order is destroyed) 
    161     #               or undef (none is found) 
    162      
    163     my $bot      = shift @_; 
    164     my $since_id = shift @_; 
    165      
    166     my $res; 
    167     eval { 
    168         $res = $bot->mentions( 
    169             { 
    170                 since_id    => $since_id, 
    171             } 
    172         ); 
    173     }; 
    174     if ($@) { 
    175         evalrescue($@); 
    176     } 
    177      
    178     my $ids; 
    179     if ($res && @{$res}) { 
    180         $ids = { 
    181             map { $_->{id} => 'mention' } @{$res} 
    182         }; 
    183     } 
    184      
    185     return $ids; 
    186 } 
    187  
    18874sub evalrescue { 
    18975    # output error message at eval error 
Note: See TracChangeset for help on using the changeset viewer.