changelog_bot (changelog_bot) wrote in changelog,
changelog_bot
changelog_bot
changelog

[ljcom] r11703: LJSV-1266: Twitter digest doesn't work f...

Committer: nnikulochkina
LJSV-1266: Twitter digest doesn't work for some users
Loading twitter digest by parts.
U   trunk/cgi-bin/LJ/Client/Twitter.pm
U   trunk/cgi-bin/LJ/TwitterDigest.pm
Modified: trunk/cgi-bin/LJ/Client/Twitter.pm
===================================================================
--- trunk/cgi-bin/LJ/Client/Twitter.pm	2012-04-12 13:26:12 UTC (rev 11702)
+++ trunk/cgi-bin/LJ/Client/Twitter.pm	2012-04-13 08:57:44 UTC (rev 11703)
@@ -272,55 +272,62 @@
 
     my $ua = LJ::get_useragent( 'role' => 'twitter_auth',
                                 'timeout' => $LJ::TWITTER_API_TIMEOUT, );
-    my $res;
-    if ($http_method eq 'GET') {
-        $res = $ua->get($request->to_url);
-    } else {
-        ##
+    my $post_params;
+    if ($http_method eq 'POST') {
+	##
         ## turn UTF-8 flag OFF here,
         ## TODO: what to do with GET requests?
         ##
-        my $post_params = $request->to_hash;
+        $post_params = $request->to_hash;
         while (my ($k, $v) = each %$post_params) {
             $post_params->{$k} = Encode::encode_utf8($v);
         }
-        $res = $ua->post( $request_url, $post_params );
     }
 
-    if ($res->is_success) {
-        return LJ::JSON->from_json($res->content);
-    } else {
+    my $retry_number = $LJ::TWITTER_API_RETRY->{$api_method} || 1;
+
+  RETRY:
+    while($retry_number){
+        my $res = ( $http_method eq 'GET' ) ? 
+            $ua->get($request->to_url) : 
+            $ua->post($request_url, $post_params);
 	
-	${$opts{'http_code'}} = $res->code if ( ref($opts{'http_code'}) );
-
-        if ($res->code eq '401' && $u) {
-            $u->log_event ("twitter_failed", { text => '401 Unauthorized' } )
-                if $opts{'call_from_digest'};
-
-            $u->clear_prop($_)
-                foreach qw(
-                    twitter_access_token
-                    twitter_access_token_secret
-                    twitter_name
-                    twitter_link
-                );
-
-            my $lang = $u->prop('browselang') || $LJ::DEFAULT_LANG;
-            LJ::send_mail( {
-                to       => $u->email_raw,
-                from     => $LJ::BOGUS_EMAIL,
-                wrap     => 1,
-                charset  => $u->mailencoding || 'utf-8',
-                subject  => LJ::Lang::get_text($lang, 'twitter.disconnect_letter.subject', undef),
-                body     => LJ::Lang::get_text($lang, 'twitter.disconnect_letter.body', undef, { user => $u->username }),
-            } ) or warn "Unable to send_mail";
-	    die "twitter error: 401 Unauthorized\n";
-        } else {
-            $u->log_event ("twitter_failed", { text => $res->status_line } )
-                if $opts{'call_from_digest'};
-            die "twitter connectivity error: " . $res->status_line;
-        }
-        return undef;
+	if ($res->is_success) {
+	    return LJ::JSON->from_json($res->content);
+	} else {
+	    if ($res->code eq '401' && $u) {
+		$u->log_event ("twitter_failed", { text => '401 Unauthorized' } )
+		    if $opts{'call_from_digest'};
+                
+		$u->clear_prop($_)
+		    foreach qw(
+			       twitter_access_token
+			       twitter_access_token_secret
+			       twitter_name
+			       twitter_link
+			       );
+                
+		my $lang = $u->prop('browselang') || $LJ::DEFAULT_LANG;
+		LJ::send_mail( {
+		    to       => $u->email_raw,
+		    from     => $LJ::BOGUS_EMAIL,
+		    wrap     => 1,
+		    charset  => $u->mailencoding || 'utf-8',
+		    subject  => LJ::Lang::get_text($lang, 'twitter.disconnect_letter.subject', undef),
+		    body     => LJ::Lang::get_text($lang, 'twitter.disconnect_letter.body', undef, { user => $u->username }),
+		} ) or warn "Unable to send_mail";
+		die "twitter error: 401 Unauthorized\n";
+	    } else {
+		$u->log_event ("twitter_failed", { text => $res->status_line } )
+		    if $opts{'call_from_digest'};
+		if( $res->code eq '502' && --$retry_number ){
+		    next RETRY;
+		} else {
+		    die "twitter connectivity error: " . $res->status_line;
+		}
+	    }
+	    return undef;
+	}
     }
 }
 
@@ -459,38 +466,39 @@
                   };
     $params->{'since_id'} = $u->{'last_tweet_id'} if $u->{'last_tweet_id'};
     
+    my $min_post_time = time - 86400;
+
+    my @tweets;
     my $res;
-    my $retry_number = $LJ::TWITTER_API_USER_TIMELINE_RETRY || 1;
 
-    my $http_code;
+  TW_PART:
+    until ( $res && scalar(@$res) < $params->{count} ) {
+     	$res = LJ::Client::Twitter->call(
+		    'api_method' => 'statuses/user_timeline',
+                    'user' => $u,
+                    'http_method' => 'GET',
+                    'params' => $params,
+                    %$opts,
+        );
 
-  RETRY:
-    foreach my $retry_count ( 1..$retry_number ) {
-	eval {
-	    $res = LJ::Client::Twitter->call(
-                'api_method' => 'statuses/user_timeline',
-                'user' => $u,
-                'http_method' => 'GET',
-                'params' => $params,
-                %$opts,
-		http_code => \$http_code,
-	    );
-        };
+	return [] unless $res;
+	
+	return \@tweets unless @$res;
+	
+        foreach (@$res) {
+            next if $_->{in_reply_to_status_id};
+            my $tw = LJ::Client::Twitter::Tweet->from_hash($_);
+            last TW_PART if $tw->post_time < $min_post_time;
+            push @tweets, $tw;
+        } 
 
-	if( $@ && $http_code eq '502' && $retry_count < $retry_number ) {
-	    # we recieved 502 error
-	    # wait and try again
-	    sleep 1;
-	} elsif ($@) {
-	    die $@;
-	} else {
-	    last RETRY;
-	}
+        my $oldest_tw = LJ::Client::Twitter::Tweet->from_hash( $res->[-1] );
+        last TW_PART if $oldest_tw->post_time < $min_post_time;
+        
+        $params->{max_id} = $oldest_tw->id - 1;
     }
 
-    return [ map { LJ::Client::Twitter::Tweet->from_hash($_) } 
-             grep { not $_->{in_reply_to_status_id} } # without replies
-             @$res ];
+    return \@tweets;
 }
 
 sub get_userinfo {

Modified: trunk/cgi-bin/LJ/TwitterDigest.pm
===================================================================
--- trunk/cgi-bin/LJ/TwitterDigest.pm	2012-04-12 13:26:12 UTC (rev 11702)
+++ trunk/cgi-bin/LJ/TwitterDigest.pm	2012-04-13 08:57:44 UTC (rev 11703)
@@ -262,20 +262,12 @@
         return;
     }
 
-    my @tweets_filtered =
-        sort { $a->post_time <=> $b->post_time }
-        grep { $_->post_time > time - 86400 }
-        @$tweets;
+    $u->{'last_tweet_id'} = $tweets->[0]->id;
 
-    unless (@tweets_filtered) {
-        $u->log_event ("twitter_skipped", { text => "No filtered tweets to import" });
-        return;
-    }
+    my @tweets_sorted = reverse @$tweets;
     
-    $u->{'last_tweet_id'} = $tweets_filtered[-1]->id;
-
     my @tweets_display;
-    foreach my $tw (@tweets_filtered) {
+    foreach my $tw (@tweets_sorted) {
         my $dt = DateTime->from_epoch( 
             'epoch' => $tw->post_time,
             'time_zone' => $u->prop('timezone') || 'GMT',
@@ -341,7 +333,7 @@
     }, \$errs, { 'noauth' => 1 });
 
     unless ($errs) {
-        $u->log_event ("twitter_success", { text => $res->{'url'} });
+        $u->log_event ("twitter_success", { text => scalar(@tweets_sorted) . " tweet(s) posted: ". $res->{'url'} });
     }
 
     warn $errs if $errs;

Tags: changelog_bot, ljcom, nnikulochkina, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments