Show
Ignore:
Timestamp:
11/19/07 03:30:51 (12 months ago)
Author:
mattn
Message:

lang/perl/Net-Kotonoha/trunk/README,
lang/perl/Net-Kotonoha/trunk/lib/Net/Kotonoha.pm:

added README file.
added following methods to module.

hot_list
answered_list
posted_list
stream_list
subscribed_list

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Net-Kotonoha/trunk/lib/Net/Kotonoha.pm

    r1224 r1769  
    88use HTML::Selector::XPath qw/selector_to_xpath/; 
    99use HTML::TreeBuilder::XPath; 
     10use HTML::Entities qw/decode_entities/; 
    1011use Net::Kotonoha::Koto; 
    1112 
     
    6364    my $self = shift; 
    6465    my $xpath = shift; 
     66    my $page = shift || 'http://kotonoha.cc/home'; 
    6567 
    6668    $self->login unless defined $self->{loggedin}; 
    6769 
    68     my $res = $self->{mech}->get('http://kotonoha.cc/home'); 
     70    my $res = $self->{mech}->get( $page ); 
    6971    croak "can't login kotonoha.cc" unless $res->is_success; 
    7072    return unless $res->is_success; 
     
    9193} 
    9294 
     95sub _get_stream { 
     96    my $self = shift; 
     97    my $xpath = shift; 
     98    my $page = shift || 'http://kotonoha.cc/stream'; 
     99 
     100    $self->login unless defined $self->{loggedin}; 
     101 
     102    my $res = $self->{mech}->get( $page ); 
     103    croak "can't login kotonoha.cc" unless $res->is_success; 
     104    return unless $res->is_success; 
     105 
     106    my @list; 
     107 
     108    my $tree = HTML::TreeBuilder::XPath->new; 
     109    $tree->parse($res->content); 
     110    $tree->eof; 
     111    foreach my $line ($tree->findnodes(selector_to_xpath($xpath))) { 
     112        my $html = decode_entities($line->as_HTML); 
     113        if ($html =~ /<a href="\/user\/(\w+)">([^<]+)<\/a>[^<]+<a href="\/no\/(\d+)">([^<]+)<\/a>([^ ]+)( [^ ]+ (.+))?$/) { 
     114            push @list, { 
     115                user    => $1, 
     116                name    => $2, 
     117                comment => $7 || '', 
     118                answer  => $5, 
     119                koto_no => $3, 
     120                title   => $4, 
     121            } 
     122        } 
     123    } 
     124    $tree->delete; 
     125    return @list; 
     126} 
     127 
    93128sub newer_list { 
    94129    return shift->_get_list('dl#newkoto a'); 
     
    97132sub recent_list { 
    98133    return shift->_get_list('dl#recentkoto a'); 
     134} 
     135 
     136sub hot_list { 
     137    return shift->_get_list('dl#hot20 a'); 
     138} 
     139 
     140sub answered_list { 
     141    return shift->_get_list('dl#answeredkoto a'); 
     142} 
     143 
     144sub posted_list { 
     145    return shift->_get_list('dl#postedkoto a'); 
     146} 
     147 
     148sub stream_list { 
     149    return shift->_get_stream('dl#stream li'); 
     150} 
     151 
     152sub subscribed_list { 
     153    return shift->_get_stream('dl#subscribelist li', 'http://kotonoha.cc/inbox'); 
    99154} 
    100155