+ 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/%/%%/g;
+ $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/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/%/%%/g;
+ $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/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;
+ }
+ }
+ next
+ if $t->{user}{screen_name} eq $username
+ and not Irssi::settings_get_bool("show_own_tweets");
+ printf $fh "id:%d account:%s nick:%s type:%s %s\n",
+ $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
+ }
+
+ print scalar localtime, " - Polling for replies" if &debug;
+ eval {
+ $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
+ || [];
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during replies call. Aborted.\n";
+ return 1;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ next
+ if exists $friends{ $t->{user}{screen_name} };
+
+ my $text = decode_entities( $t->{text} );
+ $text =~ s/%/%%/g;
+ $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
+ $text =~ s/[\n\r]/ /g;
+ printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
+ $t->{id}, $username, $t->{user}{screen_name}, $text;
+ }
+
+ print scalar localtime, " - Polling for DMs" if &debug;
+ eval {
+ $tweets =
+ $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
+ || [];
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during direct_messages call. Aborted.\n";
+ return 1;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ my $text = decode_entities( $t->{text} );
+ $text =~ s/%/%%/g;
+ $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
+ $text =~ s/[\n\r]/ /g;
+ printf $fh "id:%d account:%s nick:%s type:dm %s\n",
+ $t->{id}, $username, $t->{sender_screen_name}, $text;
+ }
+ print scalar localtime, " - Done" if &debug;
+
+ return 0;