/[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 7 by dpavlin, Sat Feb 17 21:22:17 2007 UTC revision 47 by dpavlin, Wed Feb 21 03:04:48 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 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            my $message;
82          my $uri = $feed->uri;          my $uri = $feed->uri;
83          if ($uri =~ m/%s/) {          if ($uri =~ m/%s/) {
84                  $uri = sprintf( $uri, $self->argument_value('q') );                  $uri = $feed->search_uri( $q );
85                  Jifty->log->info("Searching ", $feed->title, " at $uri");                  $message = 'Searching';
86          } else {          } else {
87                  Jifty->log->info("Fetching ", $feed->title, " at $uri");                  $message = 'Fetching';
88          }          }
89            $message .= ' ' . $feed->title . " at $uri";
90    
91            Jifty->log->info( $message );
92    
93            my $items = Grep::Model::ItemCollection->new();
94    
95            my $ua = LWP::UserAgent->new;
96            $ua->default_header( 'Cookie' => $feed->cookie );
97            my $r = $ua->get( $uri );
98            return $self->result->error(
99                    $feed->title . " returned " . $r->status_line . " for $uri\n"
100            ) unless ( $r->is_success );
101    
102            my $content = $r->content;
103    
104          my $xml_feed = XML::Feed->parse( URI->new($uri) )          return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1;
                 or die XML::Feed->errstr;  
105    
         warn "fetching ", $xml_feed->title, "\n";  
106    
107          my @results;          my $xml_feed = XML::Feed->parse( \$content )
108                    or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr, " for $uri" );
109    
110            warn "getting entries from ", $xml_feed->title, "\n";
111    
112            my $new;
113    
114          for my $entry ($xml_feed->entries) {          for my $entry ($xml_feed->entries) {
115                  my $i = Grep::Model::Item->new();                  my $i = Grep::Model::Item->new();
116    
117                  $i->load_or_create(                  my ($ok,$msg) = $i->load_or_create(
118                          in_feed => $feed,                          in_feed => $feed,
119                          title => $entry->title,                          title => $entry->title,
120                          link => $entry->link,                          link => $entry->link,
# Line 89  sub take_action { Line 122  sub take_action {
122                          summary => $entry->summary->body,                          summary => $entry->summary->body,
123                          category => $entry->category,                          category => $entry->category,
124                          author => $entry->author,                          author => $entry->author,
125                          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,
126                          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,
127                  );                  );
128    
129                  die "can't create item from entry ", dump( $entry ) unless ( $i->id );                  $msg ||= '';
130    
131                  push @results, $i->id;                  if ( $ok ) {
132                            Jifty->log->debug("item ", $i->id, ": $msg");
133                            $items->add_record( $i );
134    
135                            # is new record?
136                            if ( $msg !~ m/^Found/ ) {
137                                    $new++;
138                                    Grep::Search->add( $i );
139                            }
140                    } else {
141                            warn "can't add entry ", dump( $entry ), "\n";
142                    }
143    
                 Jifty->log->debug("item ", $i->id, " = ",dump( $entry ) );  
144          }          }
145    
146          if ( @results ) {          if ( my $count = $items->count ) {
147    
148                  $self->result->message( $self->argument_value('q') . ' => ' .                  # construct a proper sentence :-)
149                          $xml_feed->entries . ' items: ' . join(",", @results)                  my $message = $count
150                  );                          . ( $new == $count ? ' new' : '' )
151                            . ( $new == 0 ? ' old' : '' )
152                            . ' results '  
153                            . ( $new && $new < $count ? "of which $new new " : '' )
154                            . "for '$q' in " . $feed->title;
155            
156                    $self->result->message( $message );
157    
158                  $self->result->content( results => \@results );                  $self->result->content( items => $items );
159                  return 1;                  $self->result->content( count => $items->count );
160    
161          } else {                  Grep::Search->finish if $new;
162    
163                  $self->result->message( 'No results for ' . $self->argument_value('q') );          } else {
164                  return 0;                  return $self->result->error( "No results for '$q' in " . $feed->title );
165          }          }
166    
167            return $items;
168  }  }
169    
170  1;  1;

Legend:
Removed from v.7  
changed lines
  Added in v.47

  ViewVC Help
Powered by ViewVC 1.1.26