/[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 19 by dpavlin, Sun Feb 18 12:51:26 2007 UTC revision 46 by dpavlin, Tue Feb 20 22:44:59 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 Data::Dump qw/dump/;  use Data::Dump qw/dump/;
17    
# Line 37  use Jifty::Action schema { Line 38  use Jifty::Action schema {
38                          }];                          }];
39                  };                  };
40    
41            param item_fragment =>
42                    label is 'Show',
43                    render as 'select',
44    #               valid are qw/result result_short/,
45    #               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 50  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 63  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;
94            $ua->default_header( 'Cookie' => $feed->cookie );
95            my $r = $ua->get( $uri );
96            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;
101    
102            return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1;
103    
104    
105          my $xml_feed = XML::Feed->parse( URI->new($uri) )          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 "fetching ", $xml_feed->title, "\n";          warn "getting entries from ", $xml_feed->title, "\n";
109    
110          my @items;          my $new;
111    
112          for my $entry ($xml_feed->entries) {          for my $entry ($xml_feed->entries) {
113                  my $i = Grep::Model::Item->new();                  my $i = Grep::Model::Item->new();
114    
115                  $i->load_or_create(                  my ($ok,$msg) = $i->load_or_create(
116                          in_feed => $feed,                          in_feed => $feed,
117                          title => $entry->title,                          title => $entry->title,
118                          link => $entry->link,                          link => $entry->link,
# Line 89  sub take_action { Line 120  sub take_action {
120                          summary => $entry->summary->body,                          summary => $entry->summary->body,
121                          category => $entry->category,                          category => $entry->category,
122                          author => $entry->author,                          author => $entry->author,
123                          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,
124                          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,
125                  );                  );
126    
127                  die "can't create item from entry ", dump( $entry ) unless ( $i->id );                  if ( $ok ) {
128                            Jifty->log->debug("item ", $i->id, ": $msg");
129                            $items->add_record( $i );
130                            # count new objects
131                            $new++ if ($msg !~ m/^Found/);
132                    } else {
133                            warn "can't add entry ", dump( $entry ), "\n";
134                    }
135    
                 push @items, $i;  
   
 #               Grep::Event::Result->new( $i )->publish;  
   
                 Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) );  
136          }          }
137    
138          if ( @items ) {          if ( my $count = $items->count ) {
139    
140                  $self->result->message( $self->argument_value('q') . ' => ' .                  # construct a proper sentence :-)
141                          $xml_feed->entries . ' items: ' . join(",", map { $_->id } @items)                  my $message = $count
142                  );                          . ( $new == $count ? ' new' : '' )
143                            . ( $new == 0 ? ' old' : '' )
144                            . ' results '  
145                            . ( $new && $new < $count ? "of which $new new " : '' )
146                            . "for '$q' in " . $feed->title;
147            
148                    $self->result->message( $message );
149    
150                  $self->result->content( items => \@items );                  $self->result->content( items => $items );
151                  return 1;                  $self->result->content( count => $items->count );
152    
153          } else {          } else {
154                    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;  
155          }          }
156    
157            return $items;
158  }  }
159    
160  1;  1;

Legend:
Removed from v.19  
changed lines
  Added in v.46

  ViewVC Help
Powered by ViewVC 1.1.26