/[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 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 24  use Jifty::Action schema { Line 25  use Jifty::Action schema {
25    
26          param feed =>          param feed =>
27                  label is 'From feed',                  label is 'From feed',
28                  render as 'combobox',                  render as 'select',
29                  available are defer {                  available are defer {
30                          my $feeds = Grep::Model::FeedCollection->new;                          my $feeds = Grep::Model::FeedCollection->new;
31                          $feeds->order_by({ column => 'title', order => 'ASC' });                          $feeds->order_by({ column => 'title', order => 'ASC' });
# 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          Jifty->log->info("Fetching ", $feed->title, " from ", $feed->uri);          my $message;
80            my $uri = $feed->uri;
81            if ($uri =~ m/%s/) {
82                    $uri = $feed->search_uri( $q );
83                    $message = 'Searching';
84            } else {
85                    $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( $feed->uri ) )          my $xml_feed = XML::Feed->parse( \$content )
106                  or die XML::Feed->errstr;                  or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr, " for $uri" );
107    
108            warn "getting entries from ", $xml_feed->title, "\n";
109    
110            my $new;
111    
         warn "fetching ", $xml_feed->title, "\n";  
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 80  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                  Jifty->log->debug("entry = ",dump( $entry ) );                  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    
136          }          }
137    
138      $self->result->message('Success') if not $self->result->failure;          if ( my $count = $items->count ) {
139        
140      return 1;                  # construct a proper sentence :-)
141                    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 );
151                    $self->result->content( count => $items->count );
152    
153            } else {
154                    return $self->result->error( "No results for '$q' in " . $feed->title );
155            }
156    
157            return $items;
158  }  }
159    
160  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26