+ print scalar localtime, " - get_updates ends" if &debug;
+}
+
+sub do_updates {
+ my ( $fh, $username, $obj, $cache ) = @_;
+
+ my $rate_limit = $obj->rate_limit_status();
+ if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
+ ¬ice("Rate limit exceeded for $username");
+ return undef;
+ }
+
+ print scalar localtime, " - Polling for updates for $username" if &debug;
+ my $tweets;
+ my $new_poll_id = 0;
+ eval {
+ if ( $id_map{__last_id}{$username}{timeline} )
+ {
+ $tweets = $obj->friends_timeline( { count => 100 } );
+ } else {
+ $tweets = $obj->friends_timeline();
+ }
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during friends_timeline call: Aborted.\n";
+ print $fh "type:debug : $_\n" foreach split /\n/, Dumper($@);
+ return undef;
+ }
+
+ unless ( ref $tweets ) {
+ if ( $obj->can("get_error") ) {
+ my $error = "Unknown error";
+ eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
+ unless ($@) { $error = $obj->get_error() }
+ print $fh
+ "type:debug API Error during friends_timeline call: Aborted\n";
+ print $fh "type:debug : $_\n" foreach split /\n/, Dumper($error);
+
+ } else {
+ print $fh
+ "type:debug API Error during friends_timeline call. Aborted.\n";
+ }
+ return undef;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ my $text = decode_entities( $t->{text} );
+ $text = &hilight($text);
+ 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;
+ unless ( $cache->{ $t->{in_reply_to_status_id} } ) {
+ eval {
+ $cache->{ $t->{in_reply_to_status_id} } =
+ $obj->show_status( $t->{in_reply_to_status_id} );
+ };
+
+ }
+ $context = $cache->{ $t->{in_reply_to_status_id} };
+
+ if ($context) {
+ my $ctext = decode_entities( $context->{text} );
+ $ctext = &hilight($ctext);
+ if ( $context->{truncated} and ref($obj) ne 'Net::Identica' ) {
+ $ctext .=
+ " -- http://twitter.com/$context->{user}{screen_name}"
+ . "/status/$context->{id}";
+ }
+ printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
+ $context->{id}, $username,
+ $context->{user}{screen_name}, $ctext;
+ $reply = "reply";
+ }
+ }
+ next
+ if $t->{user}{screen_name} eq $username
+ and not Irssi::settings_get_bool("show_own_tweets");
+ if ( $t->{truncated} and ref($obj) ne 'Net::Identica' ) {
+ $text .= " -- http://twitter.com/$t->{user}{screen_name}"
+ . "/status/$t->{id}";
+ }
+ printf $fh "id:%d account:%s nick:%s type:%s %s\n",
+ $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
+ $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
+ }
+ printf $fh "id:%d account:%s type:last_id timeline\n",
+ $new_poll_id, $username;
+
+ print scalar localtime, " - Polling for replies" if &debug;
+ $new_poll_id = 0;
+ eval {
+ if ( $id_map{__last_id}{$username}{reply} )
+ {
+ $tweets = $obj->replies(
+ { since_id => $id_map{__last_id}{$username}{reply} } )
+ || [];
+ } else {
+ $tweets = $obj->replies() || [];
+ }
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during replies call. Aborted.\n";
+ return undef;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ next
+ if exists $friends{ $t->{user}{screen_name} };
+
+ my $text = decode_entities( $t->{text} );
+ $text = &hilight($text);
+ if ( $t->{truncated} ) {
+ $text .= " -- http://twitter.com/$t->{user}{screen_name}"
+ . "/status/$t->{id}";
+ }
+ printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
+ $t->{id}, $username, $t->{user}{screen_name}, $text;
+ $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
+ }
+ printf $fh "id:%d account:%s type:last_id reply\n", $new_poll_id, $username;
+
+ print scalar localtime, " - Polling for DMs" if &debug;
+ $new_poll_id = 0;
+ eval {
+ if ( $id_map{__last_id}{$username}{dm} )
+ {
+ $tweets = $obj->direct_messages(
+ { since_id => $id_map{__last_id}{$username}{dm} } )
+ || [];
+ } else {
+ $tweets = $obj->direct_messages() || [];
+ }
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during direct_messages call. Aborted.\n";
+ return undef;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ my $text = decode_entities( $t->{text} );
+ $text = &hilight($text);
+ printf $fh "id:%d account:%s nick:%s type:dm %s\n",
+ $t->{id}, $username, $t->{sender_screen_name}, $text;
+ $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
+ }
+ printf $fh "id:%d account:%s type:last_id dm\n", $new_poll_id, $username;
+
+ print scalar localtime, " - Polling for subscriptions" if &debug;
+ if ( $obj->can('search') and $id_map{__searches}{$username} ) {
+ my $search;
+ foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
+ print $fh "type:debug searching for $topic since ",
+ "$id_map{__searches}{$username}{$topic}\n";
+ eval {
+ $search = $obj->search(
+ {
+ q => $topic,
+ since_id => $id_map{__searches}{$username}{$topic}
+ }
+ );
+ };
+
+ if ($@) {
+ print $fh
+ "type:debug Error during search($topic) call. Aborted.\n";
+ return undef;
+ }
+
+ unless ( $search->{max_id} ) {
+ print $fh "type:debug Invalid search results when searching",
+ " for $topic. Aborted.\n";
+ return undef;
+ }
+
+ $id_map{__searches}{$username}{$topic} = $search->{max_id};
+ printf $fh "id:%d account:%s type:searchid topic:%s\n",
+ $search->{max_id}, $username, $topic;
+
+ foreach my $t ( reverse @{ $search->{results} } ) {
+ my $text = decode_entities( $t->{text} );
+ $text = &hilight($text);
+ printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
+ $t->{id}, $username, $t->{from_user}, $topic, $text;
+ $new_poll_id = $t->{id}
+ if not $new_poll_id
+ or $t->{id} < $new_poll_id;
+ }
+ }
+ }
+
+ print scalar localtime, " - Done" if &debug;
+
+ return 1;