--- lib/Grep/Action/Fetch.pm 2007/02/17 23:32:33 9 +++ lib/Grep/Action/Fetch.pm 2007/02/19 20:40:55 31 @@ -11,6 +11,7 @@ use base qw/Grep::Action Jifty::Action/; use XML::Feed; +use LWP::UserAgent; use Data::Dump qw/dump/; @@ -37,10 +38,23 @@ }]; }; + param item_fragment => + label is 'Show', + render as 'select', +# valid are qw/result result_short/, +# available are qw/result result_short/; + available are qw/long short title/; + + param publish => + label is 'In which queue?'; + }; =head2 take_action +Returns C of fatched items from Feed which will +also be stored in local cache. + =cut sub take_action { @@ -50,6 +64,8 @@ my $feed = Grep::Model::Feed->new(); my $feed_id = $self->argument_value('feed'); + my $publish = $self->argument_value('publish'); + my $q = $self->argument_value('q'); if (! $feed_id) { $self->result->message("Need feed ID"); @@ -65,18 +81,43 @@ my $uri = $feed->uri; if ($uri =~ m/%s/) { - $uri = sprintf( $uri, $self->argument_value('q') ); + $uri = $feed->search_uri( $q ); Jifty->log->info("Searching ", $feed->title, " at $uri"); } else { Jifty->log->info("Fetching ", $feed->title, " at $uri"); } - my $xml_feed = XML::Feed->parse( URI->new($uri) ) - or die XML::Feed->errstr; + my $items = Grep::Model::ItemCollection->new(); + + sub abort { + my $message = join(" ", @_); + if ( $publish ) { + Grep::Event::Result->new({ + q => $q, coll => $items, + message => $message, class => 'error', + })->publish; + return 0; + } else { + $self->result->error( $message ); + return 0; + } + } + my $ua = LWP::UserAgent->new; + $ua->default_header( 'Cookie' => $feed->cookie ); + my $r = $ua->get( $uri ); + return abort( + $feed->title . " returned " . $r->status_line . " for $uri\n" + ) unless ( $r->is_success ); + + my $content = $r->content; + + return abort( "No content returned from $uri" ) unless length( $content ) > 1; + - warn "fetching ", $xml_feed->title, "\n"; + my $xml_feed = XML::Feed->parse( \$content ) + or return abort( $feed->title, " returned ", XML::Feed->errstr, " for $uri" ); - my @items; + warn "getting entries from ", $xml_feed->title, "\n"; for my $entry ($xml_feed->entries) { my $i = Grep::Model::Item->new(); @@ -89,37 +130,43 @@ summary => $entry->summary->body, category => $entry->category, author => $entry->author, - issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef, - modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef, + issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S") : undef, + modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S") : undef, ); - die "can't create item from entry ", dump( $entry ) unless ( $i->id ); + if ( $i->id ) { + $items->add_record( $i ); - push @items, $i; + Jifty->log->debug("added ", $i->id, " to collection"); + } else { + warn "can't add entry ", dump( $entry ) unless ( $i->id ); + } -# Grep::Event::Result->new( $i )->publish; - - Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) ); } - if ( @items ) { - - $self->result->message( $self->argument_value('q') . ' => ' . - $xml_feed->entries . ' items: ' . join(",", map { $_->id } @items) - ); - - $self->result->content( items => \@items ); - return 1; + if ( $items ) { - } else { + my $message = $q . ' => ' . $xml_feed->entries . ' results'; - $self->result->message( 'No results found' ); + $self->result->message( $message ); - # with default sticky_on_failure, this will keep form data - $self->result->failure( 1 ); + if ( $publish ) { + Grep::Event::Result->new({ + q => $q, coll => $items, + item_fragment => $self->argument_value('item_fragment'), + message => $message, class => 'messages', + })->publish; + Jifty->log->debug( $items->count, " for '$q' published" ); + } else { + $self->result->content( items => $items ); + Jifty->log->debug( $items->count, " for '$q' found" ); + } - return 0; + } else { + return abort( 'No results found' ); } + + return $items; } 1;