/[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 5 by dpavlin, Sat Feb 17 18:06:42 2007 UTC revision 71 by dpavlin, Thu Feb 22 18:12:35 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    
18  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
19    
# Line 24  use Jifty::Action schema { Line 27  use Jifty::Action schema {
27    
28          param feed =>          param feed =>
29                  label is 'From feed',                  label is 'From feed',
30                  render as 'combobox',                  render as 'select',
31                  available are defer {                  available are defer {
32                          my $feeds = Grep::Model::FeedCollection->new;                          my $feeds = Grep::Model::FeedCollection->new;
33                          $feeds->order_by({ column => 'title', order => 'ASC' });                          $feeds->order_by({ column => 'title', order => 'ASC' });
# Line 37  use Jifty::Action schema { Line 40  use Jifty::Action schema {
40                          }];                          }];
41                  };                  };
42    
43            param item_fragment =>
44                    label is 'Show',
45                    render as 'select',
46    #               valid are qw/result result_short/,
47    #               available are qw/result result_short/;
48                    available are qw/long short title/;
49    
50  };  };
51    
52  =head2 take_action  =head2 take_action
53    
54    Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
55    also be stored in local cache.
56    
57  =cut  =cut
58    
59  sub take_action {  sub take_action {
# Line 50  sub take_action { Line 63  sub take_action {
63    
64          my $feed = Grep::Model::Feed->new();          my $feed = Grep::Model::Feed->new();
65          my $feed_id = $self->argument_value('feed');          my $feed_id = $self->argument_value('feed');
66            my $q = $self->argument_value('q');
67            my $user_id = Jifty->web->current_user->id;
68    
69          if (! $feed_id) {          if (! $feed_id) {
70                  $self->result->message("Need feed ID");                  $self->result->message("Need feed ID");
# Line 63  sub take_action { Line 78  sub take_action {
78                  return 0;                  return 0;
79          }          }
80    
81          Jifty->log->info("Fetching ", $feed->title, " from ", $feed->uri);          my $message;
82            my $uri = $feed->uri;
83            if ($uri =~ m/%s/) {
84                    $uri = $feed->search_uri( $q );
85                    $message = 'Searching';
86            } else {
87                    $message = 'Fetching';
88            }
89            $message .= ' ' . $feed->title . " at $uri";
90    
91            Jifty->log->info( $message );
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            my ( $items, $new ) = $self->collection_from_feed( $feed, $content );
105    
106            if ( my $count = $items->count ) {
107    
108                    # construct a proper sentence :-)
109                    my $message = $count
110                            . ( $new == $count ? ' new' : '' )
111                            . ( $new == 0 ? ' old' : '' )
112                            . ' results '  
113                            . ( $new && $new < $count ? "of which $new new " : '' )
114                            . "for '$q' in " . $feed->title;
115            
116                    $self->result->message( $message );
117    
118                    $self->result->content( items => $items );
119                    $self->result->content( count => $items->count );
120    
121                    Grep::Search->finish if $new;
122    
123                    return $items;
124    
125            } else {
126                    return $self->result->error( "No results for '$q' in " . $feed->title );
127            }
128    }
129    
130    
131    =head2 collection_from_feed
132    
133      my ( $collection, $new ) = $self->collection_from_feed( $feed, '<xml>souce of feed</xml>');
134    
135    =cut
136    
137    sub collection_from_feed {
138            my $self = shift;
139            
140            my ( $feed, $content ) = @_;
141    
142          my $xml_feed = XML::Feed->parse( URI->new( $feed->uri ) )          die "feed is not a Grep::Model::Feed but ", ref $feed unless $feed->isa('Grep::Model::Feed');
143                  or die XML::Feed->errstr;  
144            my $xml_feed = XML::Feed->parse( \$content )
145                    or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr );
146    
147    
148            warn "getting entries from ", $xml_feed->title, "\n";
149    
150            my $items = Grep::Model::ItemCollection->new();
151    
152            my $new;
153    
         warn "fetching ", $xml_feed->title, "\n";  
154          for my $entry ($xml_feed->entries) {          for my $entry ($xml_feed->entries) {
155                  my $i = Grep::Model::Item->new();                  my $i = Grep::Model::Item->new();
156    
157                  $i->load_or_create(                  my ($ok,$msg) = $i->load_or_create(
158                          in_feed => $feed,                          in_feed => $feed,
159                          title => $entry->title,                          title => $entry->title,
160                          link => $entry->link,                          link => $entry->link,
# Line 80  sub take_action { Line 162  sub take_action {
162                          summary => $entry->summary->body,                          summary => $entry->summary->body,
163                          category => $entry->category,                          category => $entry->category,
164                          author => $entry->author,                          author => $entry->author,
165                          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,
166                          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,
167                  );                  );
168    
169                  Jifty->log->debug("entry = ",dump( $entry ) );                  $msg ||= '';
170    
171                    if ( $ok ) {
172                            Jifty->log->debug("item ", $i->id, ": $msg");
173                            $items->add_record( $i );
174    
175                            # is new record?
176                            if ( $msg !~ m/^Found/ ) {
177                                    $new++;
178                                    Grep::Search->add( $i );
179                            }
180                    } else {
181                            warn "can't add entry ", dump( $entry ), "\n";
182                    }
183          }          }
184    
185      $self->result->message('Success') if not $self->result->failure;          return ($items, $new);
       
     return 1;  
186  }  }
187    
188  1;  1;

Legend:
Removed from v.5  
changed lines
  Added in v.71

  ViewVC Help
Powered by ViewVC 1.1.26