Changeset 1559

Show
Ignore:
Timestamp:
08/21/06 15:01:01
Author:
miyagawa
Message:

Summary::HatenaBookmark?: use /entry/rss. No need cookies

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/hackathon-summary/plagger/lib/Plagger/Plugin/Summary/HatenaBookmark.pm

    r1556 r1559  
    55use Plagger::UserAgent; 
    66use URI; 
    7 use XML::LibXML; 
    87 
    98sub register { 
     
    1817    my($self, $context, $args) = @_; 
    1918 
    20     my $uri = URI->new("http://b.hatena.ne.jp/atomfeed"); 
    21     $uri->query_form(url => $args->{entry}->permalink); 
     19    my $permalink = $args->{entry}->permalink; 
     20       $permalink =~ s/\#/%23/; 
     21    my $uri = "http://b.hatena.ne.jp/entry/rss/$permalink"; 
    2222 
    2323    my $agent = Plagger::UserAgent->new; 
    24     my $res = $agent->fetch($uri); 
    25     if ($res->is_error) { 
    26         $context->log(warn => "Fetch $uri failed: ", $res->status); 
     24    my $summary; 
     25    eval { 
     26        my $feed = $agent->fetch_parse($uri); 
     27        $summary = $feed->tagline; 
     28    }; 
     29 
     30    if ($@) { 
     31        $context->log(warn => "Fetch $uri failed: $@"); 
    2732        return; 
    2833    } 
    2934 
    30     my $doc = XML::LibXML->new->parse_string($res->content); 
    31     my($node) = $doc->findnodes("/*[local-name()='entry']/*[local-name()='summary']"); 
    32  
    33     return $node->textContent if $node && $node->textContent; 
    34     return; 
     35    return $summary; 
    3536} 
    3637 
     
    4950 
    5051This plugin calls Hatena Bookmark Feed API (atomfeed) to get summary 
    51 from a certain web page. Since Hatena Bookmark I<atomfeed> endpoint 
    52 requires either Cookie or WSSE based authentication, you need to share 
    53 cookies using L<Plagger::Cookies>. 
    54  
    55 =head1 TODO 
    56  
    57 Make I<UserAgent::AuthenRequest::WSSE> to transparently do WSSE authentication. 
     52from a certain web page. 
    5853 
    5954=head1 AUTHOR 
     
    6358=head1 SEE ALSO 
    6459 
    65 L<Plagger>, L<Plagger::Cookies> 
     60L<Plagger> 
    6661 
    6762=cut 
  • branches/hackathon-summary/plagger/t/plugins/Summary-HatenaBookmark/base.t

    r1556 r1559  
    44test_requires_network; 
    55test_plugin_deps; 
    6 test_requires('HTTP::Cookies::w3m'); 
    7  
    8 my $cookie = "$ENV{HOME}/.w3m/cookie"; 
    9 unless (-e $cookie and slurp_file($cookie) =~ /\.hatena\.ne\.jp/) { 
    10     plan skip_all => "This plugin requires w3m cookies"; 
    11 } 
    126 
    137plan 'no_plan'; 
     
    1610__END__ 
    1711 
    18 === Loading Summary::HatenaBookmark 
    19 --- input config 
    20 plugins: 
    21   - module: Summary::HatenaBookmark 
    22 --- expected 
    23 ok 1, $block->name; 
    24  
    2512=== use Hatebu Atom feed to get summary 
    2613--- input config 
    27 global: 
    28   user_agent: 
    29     cookies: $ENV{HOME}/.w3m/cookie 
    3014plugins: 
    3115  - module: CustomFeed::Debug 
     
    4226=== Something bogus 
    4327--- input config 
    44 global: 
    45   user_agent: 
    46     cookies: $ENV{HOME}/.w3m/cookie 
    4728plugins: 
    4829  - module: CustomFeed::Debug