--- lib/Grep/Action/Fetch.pm 2007/02/17 23:32:33 9 +++ lib/Grep/Action/Fetch.pm 2007/02/19 18:25:12 29 @@ -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,27 @@ 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 $ua = LWP::UserAgent->new; + $ua->default_header( 'Cookie' => $feed->cookie ); + my $r = $ua->get( $uri ); + die $feed->title . " returned " . $r->status_line . " for $uri\n" unless ( $r->is_success ); + + my $content = $r->content; + + die "No content returned from $uri" unless length( $content ) > 1; - warn "fetching ", $xml_feed->title, "\n"; + my $xml_feed = XML::Feed->parse( \$content ) + or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n"; - my @items; + warn "getting entries from ", $xml_feed->title, "\n"; + + my $items = Grep::Model::ItemCollection->new(); for my $entry ($xml_feed->entries) { my $i = Grep::Model::Item->new(); @@ -89,37 +114,39 @@ 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 ); - - push @items, $i; + if ( $i->id ) { + $items->add_record( $i ); -# Grep::Event::Result->new( $i )->publish; + Jifty->log->debug("added ", $i->id, " to collection"); + } else { + warn "can't add entry ", dump( $entry ) unless ( $i->id ); + } - Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) ); } - if ( @items ) { + if ( $items ) { - $self->result->message( $self->argument_value('q') . ' => ' . - $xml_feed->entries . ' items: ' . join(",", map { $_->id } @items) - ); + $self->result->message( $q . ' => ' . $xml_feed->entries . ' results' ); - $self->result->content( items => \@items ); - return 1; + if ( $publish ) { + Grep::Event::Result->new({ q => $q, coll => $items, item_fragment => 'title' })->publish; + Jifty->log->debug( $items->count, " for '$q' published" ); + } else { + $self->result->content( items => $items ); + Jifty->log->debug( $items->count, " for '$q' found" ); + } } else { - $self->result->message( 'No results found' ); + $self->result->error( 'No results found' ); - # with default sticky_on_failure, this will keep form data - $self->result->failure( 1 ); - - return 0; } + + return $items; } 1;