- print scalar localtime, " - Polling for updates" if DEBUG;
- my $tweets = $twit->friends_timeline(
- { since => HTTP::Date::time2str($last_poll) } )
- || [];
- foreach my $t ( reverse @$tweets ) {
- my $text = decode_entities( $t->{text} );
- $text =~ s/%/%%/g;
- $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
- my $prefix = "";
- if ( Irssi::settings_get_bool("show_reply_context")
- and $t->{in_reply_to_screen_name} ne $user
- and $t->{in_reply_to_screen_name}
- and not exists $friends{ $t->{in_reply_to_screen_name} } )
- {
- $nicks{ $t->{in_reply_to_screen_name} } = time;
- my $context = $twit->show_status( $t->{in_reply_to_status_id} );
- if ($context) {
- my $ctext = decode_entities( $context->{text} );
- $ctext =~ s/%/%%/g;
- $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
- printf $fh "[%%B\@%s%%n] %s\n",
- $context->{user}{screen_name}, $ctext;
- $prefix = "\--> ";
- }
+ my $error = 0;
+ $error += &do_updates( $fh, $user, $twit );
+ foreach ( keys %twits ) {
+ next if $_ eq $user;
+ $error += &do_updates( $fh, $_, $twits{$_} );
+ }
+
+ my ( $added, $removed ) = &load_friends($fh);
+ if ( $added + $removed ) {
+ print $fh "type:debug %R***%n Friends list updated: ",
+ join( ", ",
+ sprintf( "%d added", $added ),
+ sprintf( "%d removed", $removed ) ),
+ "\n";
+ }
+ print $fh "__friends__\n";
+ foreach ( sort keys %friends ) {
+ print $fh "$_ $friends{$_}\n";
+ }
+
+ if ($error) {
+ print $fh "type:debug Update encountered errors. Aborted\n";
+ print $fh $last_poll;
+ } else {
+ print $fh $new_poll;
+ }
+ close $fh;
+ exit;
+ }
+ print scalar localtime, " - get_updates ends" if &debug;
+}
+
+sub do_updates {
+ my ( $fh, $username, $obj ) = @_;
+
+ print scalar localtime, " - Polling for updates for $username" if &debug;
+ my $tweets;
+ eval {
+ $tweets = $obj->friends_timeline(
+ { since => HTTP::Date::time2str($last_poll) } );
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during friends_timeline call. Aborted.\n";
+ return 1;
+ }
+
+ unless ( ref $tweets ) {
+ if ( $obj->can("get_error") ) {
+ print $fh "type:debug API Error during friends_timeline call: ",
+ JSON::Any->jsonToObj( $obj->get_error() ), " Aborted.\n";
+ } else {
+ print $fh
+ "type:debug API Error during friends_timeline call. Aborted.\n";
+ }
+ return 1;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ my $text = decode_entities( $t->{text} );
+ $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cC/g;
+ $text =~ s/[\n\r]/ /g;
+ my $reply = "tweet";
+ if ( Irssi::settings_get_bool("show_reply_context")
+ and $t->{in_reply_to_screen_name} ne $username
+ and $t->{in_reply_to_screen_name}
+ and not exists $friends{ $t->{in_reply_to_screen_name} } )
+ {
+ $nicks{ $t->{in_reply_to_screen_name} } = time;
+ my $context;
+ eval {
+ $context = $obj->show_status( $t->{in_reply_to_status_id} );
+ };
+
+ if ($context) {
+ my $ctext = decode_entities( $context->{text} );
+ $ctext =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cC/g;
+ $ctext =~ s/[\n\r]/ /g;
+ printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
+ $context->{id}, $username,
+ $context->{user}{screen_name}, $ctext;
+ $reply = "reply";
+ } elsif ($@) {
+ print $fh "type:debug request to get context failed: $@";
+ } else {
+ print $fh
+"type:debug Failed to get context from $t->{in_reply_to_screen_name}"
+ if &debug;