Changeset 165

Show
Ignore:
Timestamp:
02/26/06 18:36:33
Author:
miyagawa
Message:
  • Remove the duplicate codes from ::Simple and ::Xango by subclassing
  • Added nasty caller hack to $context->log
Files:

Legend:

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

    r157 r165  
    179179sub log { 
    180180    my($self, $level, $msg) = @_; 
    181     my $caller = caller(0); 
     181 
     182    # hack to get the original caller as Plugin or Rule 
     183    my $caller; 
     184    my $i = 0; 
     185    while (my $c = caller($i++)) { 
     186        last if $c !~ /Plugin|Rule/; 
     187        $caller = $c; 
     188    } 
     189    $caller ||= caller(0); 
     190 
    182191    chomp($msg); 
    183192    if ($self->should_log($level)) { 
  • trunk/plagger/lib/Plagger/Plugin/Aggregator/Simple.pm

    r123 r165  
    2222    my $url = $args->{feed}->url; 
    2323    $context->log(info => "Fetch $url"); 
    24     my $remote = eval { XML::Feed->parse(URI->new($url)) }; 
     24 
     25    my $agent = Plagger::UserAgent->new; 
     26    my $response = $agent->get($url); 
     27 
     28    unless ($response->is_success) { 
     29        $context->log(error => "GET $url failed: " . $response->status_line); 
     30        return; 
     31    } 
     32 
     33    $self->handle_feed($url, \$response->content); 
     34
     35 
     36sub handle_feed { 
     37    my($self, $url, $xml_ref) = @_; 
     38 
     39    my $context = Plagger->context; 
     40    my $remote = eval { XML::Feed->parse($xml_ref) }; 
    2541 
    2642    unless ($remote) { 
    27         $context->log(info => "Parsing $url failed. " . XML::Feed->errstr); 
     43        $context->log(error => "Parsing $url failed. " . XML::Feed->errstr); 
    2844        next; 
    2945    } 
  • trunk/plagger/lib/Plagger/Plugin/Aggregator/Xango.pm

    r162 r165  
    66package Plagger::Plugin::Aggregator::Xango; 
    77use strict; 
    8 use base qw( Plagger::Plugin ); 
     8use base qw( Plagger::Plugin::Aggregator::Simple ); 
    99use POE; 
    1010use Xango::Broker::Push; 
     
    2121    $self->{xango_alias} = $xango_args{Alias}; 
    2222    Plagger::Plugin::Aggregator::Xango::Crawler->spawn( 
    23         Context => $context
     23        Plugin => $self
    2424    ); 
    2525    Xango::Broker::Push->spawn(%xango_args); 
     
    4343    POE::Kernel->run; 
    4444} 
    45      
     45 
    4646package Plagger::Plugin::Aggregator::Xango::Crawler; 
    4747use strict; 
     
    5555 
    5656    POE::Session->create( 
    57         heap => { CONTEXT => $args{Context} }, 
     57        heap => { PLUGIN => $args{Plugin} }, 
    5858        package_states => [ 
    5959            $class => [ qw(_start _stop apply_policy handle_response) ] 
     
    6666sub handle_response { 
    6767    my $job = $_[ARG0]; 
    68     my $context = $_[HEAP]->{CONTEXT}; 
     68    my $plugin = $_[HEAP]->{PLUGIN}; 
    6969 
    7070    my $r = $job->notes('http_response'); 
    7171    my $url    = $job->uri; 
    72     my $remote = eval { XML::Feed->parse($r->content_ref) }; 
    7372 
    74     unless ($remote) { 
    75         $context->log(info => "Parsing $url failed. $@"); 
    76         next; 
    77     } 
    78  
    79     my $feed = Plagger::Feed->new; 
    80     $feed->title($remote->title); 
    81     $feed->url($url); 
    82     $feed->link($remote->link); 
    83     $feed->description($remote->tagline); 
    84     $feed->language($remote->language); 
    85     $feed->author($remote->author); 
    86     $feed->updated($remote->modified); 
    87  
    88     if ($remote->format eq 'Atom') { 
    89         $feed->id( $remote->{atom}->id ); 
    90     } 
    91  
    92     if ($remote->format =~ /^RSS/) { 
    93         $feed->image( $remote->{rss}->image ) 
    94             if $remote->{rss}->image; 
    95     } elsif ($remote->format eq 'Atom') { 
    96         $feed->image({ url => $remote->{atom}->logo }) 
    97             if $remote->{atom}->logo; 
    98     } 
    99  
    100     for my $e ($remote->entries) { 
    101         my $entry = Plagger::Entry->new; 
    102         $entry->title($e->title); 
    103         $entry->author($e->author); 
    104         $entry->tags([ $e->category ]) if $e->category; 
    105         $entry->date( Plagger::Date->rebless($e->issued) ) 
    106             if eval { $e->issued }; 
    107         $entry->link($e->link); 
    108         $entry->id($e->id); 
    109         $entry->body($e->content->body); 
    110  
    111         $feed->add_entry($entry); 
    112     } 
    113  
    114     $context->log(info => "Aggregate $url success: " . $feed->count . " entries."); 
    115     $context->update->add($feed); 
     73    $plugin->handle_feed($url, $r->content_ref); 
    11674} 
    11775