Changeset 115 in lab


Ignore:
Timestamp:
Mar 17, 2011 6:28:37 PM (13 years ago)
Author:
mitty
Message:
  • reads text file and tweets it
  • usage: update_timeline.pl [text_file_to_tweet]
  • text file should be UTF-8
File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Dev/twitter/update_timeline.pl

    r114 r115  
    1818my $help = sub { 
    1919    die <<EOM; 
    20 usage: $0 
    21     [{u}ser_timeline(default)|{r}etweeted_by_me|{m}sentions 
    22         [screen_name 
    23             [number_of_pages|all 
    24                 [dump] 
    25             ] 
    26         ] 
    27     ] 
     20usage: $0 [text_file_to_tweet] 
    2821EOM 
    2922}; 
     
    3225} 
    3326 
    34 my $method = $ARGV[0] || 'user_timeline'; 
    35 my $screen_name = $ARGV[1] || ''; 
    36 my $pages = $ARGV[2] || 1; 
    37 if ($pages eq 'all') { 
    38     $pages = -1; 
    39 } 
    40 my $dump = $ARGV[3] || 0; 
    41  
     27my $file = $ARGV[0] || 'update.log'; 
    4228my $conf = loadconf("$Bin/config.yml"); 
    4329if (! defined $conf) { 
     
    5036} 
    5137 
    52  
     38open TWEET, $file or die "$0: cannot open file: $file"; 
    5339eval { 
    54     my $page = 0; 
    55     while ($pages - $page && $page <= 160) { 
    56         $page++; 
    57          
    58         my $param = ($screen_name) 
    59             ? { page => $page, screen_name => $screen_name, } 
    60             : { page => $page, } 
    61         ; 
     40    my $update = ""; 
     41    my $res; 
     42    foreach my $line (<TWEET>) { 
     43        chomp $line; 
     44        $update .= $line; 
     45    } 
     46    if (length $update) { 
     47        $update = decode('utf8', $update); 
     48        $res = $bot->update($update); 
     49    } 
    6250     
    63         my $res; 
    64         if ($method eq 'user_timeline' || $method eq 'u') { 
    65             $res = $bot->user_timeline($param); 
    66         } 
    67         elsif ($method eq 'retweeted_by_me' || $method eq 'r') { 
    68             $res = $bot->retweeted_by_me($param); 
    69         } 
    70         elsif ($method eq 'mentions' || $method eq 'm') { 
    71             $res = $bot->mentions($param); 
    72         } 
    73         else { 
    74             warn "$0: unknown method '$method'"; 
    75             &{$help}; 
    76         } 
    77          
    78         if ($dump) { 
    79             foreach my $line (split /\n/, Dumper $res) { 
    80                 if ($line =~ /undef/) { next; } 
    81                 print $line, "\n"; 
    82             } 
    83         } 
    84         else { 
    85             foreach my $status (@{$res}) { 
    86                 my $text = ""; 
    87                 $text .= "(". $status->{id} . ") "; 
    88                 $text .= $status->{user}{name}; 
    89                 $text .= " [" . $status->{created_at} . "]"; 
    90                 $text .= " ".  $status->{text}; 
    91                 $text =~ s/\n//; 
    92                 print encode('utf8', $text), "\n"; 
    93             } 
    94         } 
     51    foreach my $line (split /\n/, Dumper $res) { 
     52        if ($line =~ /undef/) { next; } 
     53        print STDERR $line, "\n"; 
     54    } 
     55    if (ref $res) { 
     56        my $status = $res; 
     57        my $text = ""; 
     58        $text .= "(". $status->{id} . ") "; 
     59        $text .= ($status->{user}{name}) ? $status->{user}{name} : $status->{from_user}; 
     60        $text .= " [" . $status->{created_at} . "]"; 
     61        $text .= " ".  $status->{text}; 
     62        $text =~ s/\n//; 
     63        print encode('sjis', $text), "\n"; 
    9564    } 
    9665}; 
Note: See TracChangeset for help on using the changeset viewer.