--- lib/Grep/Action/Fetch.pm 2007/02/17 21:24:48 8 +++ lib/Grep/Action/Fetch.pm 2007/02/22 18:12:35 71 @@ -11,6 +11,9 @@ use base qw/Grep::Action Jifty::Action/; use XML::Feed; +use LWP::UserAgent; + +use Grep::Search; use Data::Dump qw/dump/; @@ -37,10 +40,20 @@ }]; }; + 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/; + }; =head2 take_action +Returns C of fatched items from Feed which will +also be stored in local cache. + =cut sub take_action { @@ -50,6 +63,8 @@ my $feed = Grep::Model::Feed->new(); my $feed_id = $self->argument_value('feed'); + my $q = $self->argument_value('q'); + my $user_id = Jifty->web->current_user->id; if (! $feed_id) { $self->result->message("Need feed ID"); @@ -63,25 +78,83 @@ return 0; } + my $message; my $uri = $feed->uri; if ($uri =~ m/%s/) { - $uri = sprintf( $uri, $self->argument_value('q') ); - Jifty->log->info("Searching ", $feed->title, " at $uri"); + $uri = $feed->search_uri( $q ); + $message = 'Searching'; } else { - Jifty->log->info("Fetching ", $feed->title, " at $uri"); + $message = 'Fetching'; } + $message .= ' ' . $feed->title . " at $uri"; + + Jifty->log->info( $message ); + + my $ua = LWP::UserAgent->new; + $ua->default_header( 'Cookie' => $feed->cookie ); + my $r = $ua->get( $uri ); + return $self->result->error( + $feed->title . " returned " . $r->status_line . " for $uri\n" + ) unless ( $r->is_success ); + + my $content = $r->content; + + return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1; + + my ( $items, $new ) = $self->collection_from_feed( $feed, $content ); + + if ( my $count = $items->count ) { + + # construct a proper sentence :-) + my $message = $count + . ( $new == $count ? ' new' : '' ) + . ( $new == 0 ? ' old' : '' ) + . ' results ' + . ( $new && $new < $count ? "of which $new new " : '' ) + . "for '$q' in " . $feed->title; + + $self->result->message( $message ); + + $self->result->content( items => $items ); + $self->result->content( count => $items->count ); + + Grep::Search->finish if $new; + + return $items; + + } else { + return $self->result->error( "No results for '$q' in " . $feed->title ); + } +} + - my $xml_feed = XML::Feed->parse( URI->new($uri) ) - or die XML::Feed->errstr; +=head2 collection_from_feed - warn "fetching ", $xml_feed->title, "\n"; + my ( $collection, $new ) = $self->collection_from_feed( $feed, 'souce of feed'); + +=cut + +sub collection_from_feed { + my $self = shift; + + my ( $feed, $content ) = @_; + + die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed'); + + my $xml_feed = XML::Feed->parse( \$content ) + or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr ); - my @results; + + warn "getting entries from ", $xml_feed->title, "\n"; + + my $items = Grep::Model::ItemCollection->new(); + + my $new; for my $entry ($xml_feed->entries) { my $i = Grep::Model::Item->new(); - $i->load_or_create( + my ($ok,$msg) = $i->load_or_create( in_feed => $feed, title => $entry->title, link => $entry->link, @@ -89,35 +162,27 @@ 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 ); + $msg ||= ''; - push @results, $i->id; - - Jifty->log->debug("item ", $i->id, " = ",dump( $entry ) ); + if ( $ok ) { + Jifty->log->debug("item ", $i->id, ": $msg"); + $items->add_record( $i ); + + # is new record? + if ( $msg !~ m/^Found/ ) { + $new++; + Grep::Search->add( $i ); + } + } else { + warn "can't add entry ", dump( $entry ), "\n"; + } } - if ( @results ) { - - $self->result->message( $self->argument_value('q') . ' => ' . - $xml_feed->entries . ' items: ' . join(",", @results) - ); - - $self->result->content( results => \@results ); - return 1; - - } else { - - $self->result->message( 'No results found' ); - - # with default sticky_on_failure, this will keep form data - $self->result->failure( 1 ); - - return 0; - } + return ($items, $new); } 1;