Changeset 115 in lab
- Timestamp:
- Mar 17, 2011 6:28:37 PM (14 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/Dev/twitter/update_timeline.pl
r114 r115 18 18 my $help = sub { 19 19 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 ] 20 usage: $0 [text_file_to_tweet] 28 21 EOM 29 22 }; … … 32 25 } 33 26 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 27 my $file = $ARGV[0] || 'update.log'; 42 28 my $conf = loadconf("$Bin/config.yml"); 43 29 if (! defined $conf) { … … 50 36 } 51 37 52 38 open TWEET, $file or die "$0: cannot open file: $file"; 53 39 eval { 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 } 62 50 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"; 95 64 } 96 65 };
Note: See TracChangeset
for help on using the changeset viewer.