Igor Gariev (gariev) wrote in changelog,
Igor Gariev
gariev
changelog

[ljcom] r9333: LJSUP-6527: Twitter crossposting is not ...

Committer: gariev
LJSUP-6527: Twitter crossposting is not working (UTF-8 problem)
U   trunk/cgi-bin/LJ/API/Twitter.pm
U   trunk/cgi-bin/LJ/Worker/Repost/CommentToTwitter.pm
U   trunk/cgi-bin/LJ/Worker/Repost/EntryToTwitter.pm
Modified: trunk/cgi-bin/LJ/API/Twitter.pm
===================================================================
--- trunk/cgi-bin/LJ/API/Twitter.pm	2010-08-12 10:49:18 UTC (rev 9332)
+++ trunk/cgi-bin/LJ/API/Twitter.pm	2010-08-13 04:11:14 UTC (rev 9333)
@@ -178,21 +178,46 @@
     my $http_method = $opts{'http_method'} || 'GET';
     die 'invalid HTTP method' unless $http_method =~ /^(?:GET|POST)$/;
 
-    my $params  = $opts{'params'}  || {};
+    my $extra_params  = $opts{'params'}  || {};
+    
+    ##
+    ## Net::OAuth, Twitter, LJ and Unicode strings.
+    ##
+    ## Net::OAuth::Message uses URI::Escape::uri_escape_utf8, 
+    ## which processes byte strings incorrectly (double utf encoding):
+    ##
+    ##      print URI::Escape::uri_escape_utf8("я", '^\w.~-');
+    ##
+    ## When wide-char strings are used (with utf8 flag on), 
+    ## then they are not encoded to utf8 (byte) strings
+    ## before passing to Twitter:
+    ##
+    ##      my $request = Net::OAuth::Request->new(
+    ##          extra_params => { status => Encode::decode_utf8("я"), }
+    ##      );
+    ##      print Data::Dumper::Dumper( $request->to_hash );
+    ##
 
     my %params = $class->default_request_params;
     $params{'request_method'} = $http_method;
 
     my $request_url = 'https://api.twitter.com/1/' . $api_method . '.json';
 
+    ##
+    ## turn UTF-8 flag ON here, 
+    ## don't change elements of original hash (caller's params)
+    ##
+    my %extra_params;
+    while (my ($k, $v) = each %$extra_params) {
+        $extra_params{$k} = Encode::decode_utf8($v);
+    }
+
     my $request = Net::OAuth::ProtectedResourceRequest->new(
         %params,
         token => $access_token->{'public'},
         token_secret => $access_token->{'secret'},
         request_url => $request_url,
-        extra_params => {
-            %$params,
-        },
+        extra_params => \%extra_params,
     );
 
     $request->sign;
@@ -202,7 +227,15 @@
     if ($http_method eq 'GET') {
         $res = $ua->get($request->to_url);
     } else {
-        $res = $ua->post( $request_url, $request->to_hash );
+        ##
+        ## turn UTF-8 flag OFF here,
+        ## TODO: what to do with GET requests?
+        ##
+        my $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 );
     }
 
     unless ($res->is_success) {

Modified: trunk/cgi-bin/LJ/Worker/Repost/CommentToTwitter.pm
===================================================================
--- trunk/cgi-bin/LJ/Worker/Repost/CommentToTwitter.pm	2010-08-12 10:49:18 UTC (rev 9332)
+++ trunk/cgi-bin/LJ/Worker/Repost/CommentToTwitter.pm	2010-08-13 04:11:14 UTC (rev 9333)
@@ -25,7 +25,7 @@
 
     $title = LJ::Text->truncate_to_word_with_ellipsis(
         'str' => $title,
-        'chars' => 139 - length($title),
+        'chars' => 139 - length($short_url),
     );
 
     my $status = "$title $short_url";

Modified: trunk/cgi-bin/LJ/Worker/Repost/EntryToTwitter.pm
===================================================================
--- trunk/cgi-bin/LJ/Worker/Repost/EntryToTwitter.pm	2010-08-12 10:49:18 UTC (rev 9332)
+++ trunk/cgi-bin/LJ/Worker/Repost/EntryToTwitter.pm	2010-08-13 04:11:14 UTC (rev 9333)
@@ -25,7 +25,7 @@
 
     $title = LJ::Text->truncate_to_word_with_ellipsis(
         'str' => $title,
-        'chars' => 139 - length($title),
+        'chars' => 139 - length($short_url),
     );
 
     my $status = "$title $short_url";

Tags: gariev, ljcom, 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