/[Grep]/lib/Grep/Action/Fetch.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/Grep/Action/Fetch.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 25 by dpavlin, Sun Feb 18 20:45:59 2007 UTC revision 72 by dpavlin, Fri Feb 23 09:54:28 2007 UTC
# Line 13  use base qw/Grep::Action Jifty::Action/; Line 13  use base qw/Grep::Action Jifty::Action/;
13  use XML::Feed;  use XML::Feed;
14  use LWP::UserAgent;  use LWP::UserAgent;
15    
16    use Grep::Search;
17    use Grep::Source;
18    
19  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
20    
21  use Jifty::Param::Schema;  use Jifty::Param::Schema;
# Line 41  use Jifty::Action schema { Line 44  use Jifty::Action schema {
44          param item_fragment =>          param item_fragment =>
45                  label is 'Show',                  label is 'Show',
46                  render as 'select',                  render as 'select',
47                  valid are qw/result result_short/,  #               valid are qw/result result_short/,
48                  available are qw/result result_short/;  #               available are qw/result result_short/;
49                    available are qw/long short title/;
50    
51  };  };
52    
53  =head2 take_action  =head2 take_action
54    
55    Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
56    also be stored in local cache.
57    
58  =cut  =cut
59    
60  sub take_action {  sub take_action {
# Line 55  sub take_action { Line 62  sub take_action {
62            
63      # Custom action code      # Custom action code
64    
65          my $feed = Grep::Model::Feed->new();          my $feed_id = $self->argument_value('feed') or die "no feed?";
66          my $feed_id = $self->argument_value('feed');          my $q = $self->argument_value('q') or die "no q?";
   
         if (! $feed_id) {  
                 $self->result->message("Need feed ID");  
                 return 0;  
         }  
67    
68            my $feed = Grep::Model::Feed->new();
69          $feed->load_by_cols( id => $feed_id );          $feed->load_by_cols( id => $feed_id );
70            die "can't load feed ", $feed_id unless ($feed->id);
71    
72          if (! $feed->id) {          my $source = Grep::Source->new({ feed => $feed });
73          $self->result->message("Can't fetch feed $feed_id");          my $items = $source->search( $q );
74                  return 0;          my $new = $source->new_items;
75          }  
76            if ( my $count = $items->count ) {
77    
78                    # construct a proper sentence :-)
79                    my $message = $count
80                            . ( $new == $count ? ' new' : '' )
81                            . ( $new == 0 ? ' old' : '' )
82                            . ' results '  
83                            . ( $new && $new < $count ? "of which $new new " : '' )
84                            . "for '$q' in " . $source->feed->title;
85            
86                    $self->result->message( $message );
87    
88          my $uri = $feed->uri;                  $self->result->content( items => $items );
89          if ($uri =~ m/%s/) {                  $self->result->content( count => $items->count );
                 $uri = $feed->search_uri( $self->argument_value('q') );  
                 Jifty->log->info("Searching ", $feed->title, " at $uri");  
         } else {  
                 Jifty->log->info("Fetching ", $feed->title, " at $uri");  
         }  
90    
91          my $ua = LWP::UserAgent->new;                  return $items;
         $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;  
   
         my $xml_feed = XML::Feed->parse( \$content )  
                 or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n";  
   
         warn "getting entries from ", $xml_feed->title, "\n";  
   
         my @items;  
   
         for my $entry ($xml_feed->entries) {  
                 my $i = Grep::Model::Item->new();  
   
                 $i->load_or_create(  
                         in_feed => $feed,  
                         title => $entry->title,  
                         link => $entry->link,  
                         content => $entry->content->body,  
                         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,  
                 );  
   
                 if ( $i->id ) {  
                         push @items, $i;  
   
         #               Grep::Event::Result->new( $i )->publish;  
   
                         Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) );  
                 } else {  
                         warn "can't create item from entry ", dump( $entry ) unless ( $i->id );  
                 }  
   
         }  
   
         if ( @items ) {  
   
                 $self->result->message( $self->argument_value('q') . ' => ' .  
                         $xml_feed->entries . ' items: ' . join(",", map { $_->id } @items)  
                 );  
   
                 $self->result->content( items => \@items );  
                 return 1;  
92    
93          } else {          } else {
94                    return $self->result->error( "No results for '$q'" );
                 $self->result->error( 'No results found' );  
                 return 0;  
   
95          }          }
96  }  }
97    
98    
99  1;  1;
100    

Legend:
Removed from v.25  
changed lines
  Added in v.72

  ViewVC Help
Powered by ViewVC 1.1.26