Changeset 231

Show
Ignore:
Timestamp:
03/02/06 09:54:09
Author:
miyagawa
Message:
  • Added cookie_jar method to Plugin cache
  • Added nasty workaroud for CAPTCHA in Yahoo! 360 login
  • Yahoo360JP: Save cookie_jar when logged in
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger/Cache.pm

    r225 r231  
    22use strict; 
    33use File::Spec; 
     4use HTTP::Cookies; 
    45use UNIVERSAL::require; 
    56 
     
    6263} 
    6364 
     65sub cookie_jar { 
     66    my($self, $ns) = @_; 
     67    my $file = $ns ? "global.dat" : "$ns.dat"; 
     68 
     69    my $dir = File::Spec->catfile($self->{base}, 'cookies'); 
     70    mkdir $dir, 0755 unless -e $dir && -d _; 
     71 
     72    return HTTP::Cookies->new( 
     73        file => File::Spec->catfile($dir, $file), 
     74        autosave => 1, 
     75    ); 
     76} 
     77 
    64781; 
  • trunk/plagger/lib/Plagger/CacheProxy.pm

    r225 r231  
    2020} 
    2121 
     22sub cookie_jar { 
     23    my $self = shift; 
     24    $self->{cache}->cookie_jar($self->{namespace}); 
     25} 
     26 
    22271; 
  • trunk/plagger/lib/Plagger/Plugin/CustomFeed/Yahoo360JP.pm

    r226 r231  
    2828    my($self, $context, $args) = @_; 
    2929 
    30     # TODO: save cookies 
    31     my $start = "https://login.yahoo.co.jp/config/login?.src=360&.done=http%3A%2F%2F360.yahoo.co.jp%2F%3F.login%3D1"; # SSL 
    32  
    33     my $mech = WWW::Mechanize->new
     30    my $start = "http://360.yahoo.co.jp/"; 
     31 
     32    my $mech = WWW::Mechanize->new(cookie_jar => $self->cache->cookie_jar); 
     33    $mech->agent_alias( 'Windows IE 6' )
    3434    $mech->get($start); 
    35     $mech->submit_form( 
    36         fields => { 
    37             login  => $self->conf->{username}, 
    38             passwd => $self->conf->{password}, 
    39             '.persistent' => 'y', 
    40         }, 
    41     ); 
    42  
    43     if ($mech->content =~ m!<span class="error">!) { 
    44         $context->log(error => "Login to Yahoo! failed."); 
    45         return; 
     35 
     36    if ($mech->content =~ /mgb_login/) { 
     37        $self->login($mech) or return; 
    4638    } 
    4739 
     
    142134} 
    143135 
     136sub login { 
     137    my($self, $mech) = @_; 
     138 
     139    $mech->submit_form( 
     140        fields => { 
     141            login  => $self->conf->{username}, 
     142            passwd => $self->conf->{password}, 
     143            '.persistent' => 'y', 
     144        }, 
     145    ); 
     146 
     147    while ($mech->content =~ m!<span class="error">!) { 
     148        Plagger->context->log(error => "Login to Yahoo! failed."); 
     149        if ($mech->content =~ m!(https://captcha.yahoo.co.jp/img/.*\.jpg)!) { 
     150            my $captcha = $self->prompt_captcha($1) or return; 
     151            $mech->submit_form( 
     152                fields => { 
     153                    login  => $self->conf->{username}, 
     154                    passwd => $self->conf->{password}, 
     155                    '.secword'    => $captcha, 
     156                    '.persistent' => 'y', 
     157                }, 
     158            ); 
     159        } else { 
     160            return; 
     161        } 
     162    } 
     163 
     164    return 1; 
     165} 
     166 
    144167sub add_entry { 
    145168    my($self, $feed, $args, $now, $format) = @_; 
     
    179202} 
    180203 
     204sub prompt_captcha { 
     205    my($self, $url) = @_; 
     206    print STDERR "CAPTCHA:\n$url\nEnter the code: "; 
     207 
     208    # use alarm timeout for cron job 
     209    my $key; 
     210    eval { 
     211        local $SIG{ALRM} = sub { die "alarm\n" }; 
     212        alarm 30; 
     213        chomp($key = <STDIN>); 
     214        alarm 0; 
     215    }; 
     216    return if $@; 
     217 
     218    return $key; 
     219} 
     220 
    1812211; 
    182222