/[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 9 by dpavlin, Sat Feb 17 23:32:33 2007 UTC revision 89 by dpavlin, Fri Feb 23 21:54:39 2007 UTC
# Line 11  package Grep::Action::Fetch; Line 11  package Grep::Action::Fetch;
11  use base qw/Grep::Action Jifty::Action/;  use base qw/Grep::Action Jifty::Action/;
12    
13  use XML::Feed;  use XML::Feed;
14    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    
# Line 37  use Jifty::Action schema { Line 41  use Jifty::Action schema {
41                          }];                          }];
42                  };                  };
43    
44            param item_fragment =>
45                    label is 'Show',
46                    render as 'select',
47    #               valid are qw/result result_short/,
48    #               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 48  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          my $uri = $feed->uri;  
78          if ($uri =~ m/%s/) {                  # construct a proper sentence :-)
79                  $uri = sprintf( $uri, $self->argument_value('q') );                  my $message = $count
80                  Jifty->log->info("Searching ", $feed->title, " at $uri");                          . ( $new == $count ? ' new' : '' )
81          } else {                          . ( $new == 0 ? ' old' : '' )
82                  Jifty->log->info("Fetching ", $feed->title, " at $uri");                          . ' results '  
83          }                          . ( $new && $new < $count ? "of which $new new " : '' )
84                            . "for '$q' in " . $source->feed->title;
85          my $xml_feed = XML::Feed->parse( URI->new($uri) )          
86                  or die XML::Feed->errstr;                  $self->result->message( $message );
   
         warn "fetching ", $xml_feed->title, "\n";  
87    
88          my @items;                  $self->result->content( items => $items );
89                    $self->result->content( count => $items->count );
90    
91          for my $entry ($xml_feed->entries) {                  return $items;
                 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,  
                 );  
   
                 die "can't create item from entry ", dump( $entry ) unless ( $i->id );  
   
                 push @items, $i;  
   
 #               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;  
92    
93          } else {          } else {
94                    return $self->result->error( "No results for '$q' in " . $feed->title );
                 $self->result->message( 'No results found' );  
   
                 # with default sticky_on_failure, this will keep form data  
                 $self->result->failure( 1 );  
   
                 return 0;  
95          }          }
96  }  }
97    
98    
99  1;  1;
100    

Legend:
Removed from v.9  
changed lines
  Added in v.89

  ViewVC Help
Powered by ViewVC 1.1.26