/[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 44 by dpavlin, Tue Feb 20 21:55:24 2007 UTC
# Line 41  use Jifty::Action schema { Line 41  use Jifty::Action schema {
41          param item_fragment =>          param item_fragment =>
42                  label is 'Show',                  label is 'Show',
43                  render as 'select',                  render as 'select',
44                  valid are qw/result result_short/,  #               valid are qw/result result_short/,
45                  available are qw/result result_short/;  #               available are qw/result result_short/;
46                    available are qw/long short title/;
47    
48  };  };
49    
50  =head2 take_action  =head2 take_action
51    
52    Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
53    also be stored in local cache.
54    
55  =cut  =cut
56    
57  sub take_action {  sub take_action {
# Line 57  sub take_action { Line 61  sub take_action {
61    
62          my $feed = Grep::Model::Feed->new();          my $feed = Grep::Model::Feed->new();
63          my $feed_id = $self->argument_value('feed');          my $feed_id = $self->argument_value('feed');
64            my $q = $self->argument_value('q');
65            my $user_id = Jifty->web->current_user->id;
66    
67          if (! $feed_id) {          if (! $feed_id) {
68                  $self->result->message("Need feed ID");                  $self->result->message("Need feed ID");
# Line 70  sub take_action { Line 76  sub take_action {
76                  return 0;                  return 0;
77          }          }
78    
79            my $message;
80          my $uri = $feed->uri;          my $uri = $feed->uri;
81          if ($uri =~ m/%s/) {          if ($uri =~ m/%s/) {
82                  $uri = $feed->search_uri( $self->argument_value('q') );                  $uri = $feed->search_uri( $q );
83                  Jifty->log->info("Searching ", $feed->title, " at $uri");                  $message = 'Searching';
84          } else {          } else {
85                  Jifty->log->info("Fetching ", $feed->title, " at $uri");                  $message = 'Fetching';
86          }          }
87            $message .= ' ' . $feed->title . " at $uri";
88    
89            Jifty->log->info( $message );
90    
91            my $items = Grep::Model::ItemCollection->new();
92    
93          my $ua = LWP::UserAgent->new;          my $ua = LWP::UserAgent->new;
94          $ua->default_header( 'Cookie' => $feed->cookie );          $ua->default_header( 'Cookie' => $feed->cookie );
95          my $r = $ua->get( $uri );          my $r = $ua->get( $uri );
96          die $feed->title . " returned " . $r->status_line . " for $uri\n" unless ( $r->is_success );          return $self->result->error(
97                    $feed->title . " returned " . $r->status_line . " for $uri\n"
98            ) unless ( $r->is_success );
99    
100          my $content = $r->content;          my $content = $r->content;
101    
102          die "No content returned from $uri" unless length( $content ) > 1;          return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1;
103    
104    
105          my $xml_feed = XML::Feed->parse( \$content )          my $xml_feed = XML::Feed->parse( \$content )
106                  or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n";                  or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr, " for $uri" );
107    
108          warn "getting entries from ", $xml_feed->title, "\n";          warn "getting entries from ", $xml_feed->title, "\n";
109    
         my @items;  
   
110          for my $entry ($xml_feed->entries) {          for my $entry ($xml_feed->entries) {
111                  my $i = Grep::Model::Item->new();                  my $i = Grep::Model::Item->new();
112    
# Line 105  sub take_action { Line 118  sub take_action {
118                          summary => $entry->summary->body,                          summary => $entry->summary->body,
119                          category => $entry->category,                          category => $entry->category,
120                          author => $entry->author,                          author => $entry->author,
121                          issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef,                          issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S") : undef,
122                          modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef,                          modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S") : undef,
123                  );                  );
124    
125                  if ( $i->id ) {                  if ( $i->id ) {
126                          push @items, $i;                          $items->add_record( $i );
   
         #               Grep::Event::Result->new( $i )->publish;  
127    
128                          Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) );                          Jifty->log->debug("added ", $i->id, " to collection");
129                  } else {                  } else {
130                          warn "can't create item from entry ", dump( $entry ) unless ( $i->id );                          warn "can't add entry ", dump( $entry ) unless ( $i->id );
131                  }                  }
132    
133          }          }
134    
135          if ( @items ) {          if ( my $count = $items->count ) {
136    
137                  $self->result->message( $self->argument_value('q') . ' => ' .                  my $message = "$count results for '$q' in " . $feed->title;
                         $xml_feed->entries . ' items: ' . join(",", map { $_->id } @items)  
                 );  
   
                 $self->result->content( items => \@items );  
                 return 1;  
138    
139          } else {                  $self->result->message( $message );
140    
141                  $self->result->error( 'No results found' );                  $self->result->content( items => $items );
142                  return 0;                  $self->result->content( count => $items->count );
143    
144            } else {
145                    return $self->result->error( "No results for '$q' in " . $feed->title );
146          }          }
147    
148            return $items;
149  }  }
150    
151  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26