/[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 3 by dpavlin, Sat Feb 17 17:10:27 2007 UTC revision 33 by dpavlin, Mon Feb 19 21:26:30 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            param publish =>
49                    label is 'In which queue?';
50    
51  };  };
52    
53  =head2 take_action  =head2 take_action
54    
55    Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
56    also be stored in local cache.
57    
58  =cut  =cut
59    
60  sub take_action {  sub take_action {
# Line 50  sub take_action { Line 64  sub take_action {
64    
65          my $feed = Grep::Model::Feed->new();          my $feed = Grep::Model::Feed->new();
66          my $feed_id = $self->argument_value('feed');          my $feed_id = $self->argument_value('feed');
67            my $publish = $self->argument_value('publish');
68            my $q = $self->argument_value('q');
69    
70          if (! $feed_id) {          if (! $feed_id) {
71                  $self->result->message("Need feed ID");                  $self->result->message("Need feed ID");
# Line 63  sub take_action { Line 79  sub take_action {
79                  return 0;                  return 0;
80          }          }
81    
82          Jifty->log->info("Fetching ", $feed->title, " from ", $feed->uri);          my $uri = $feed->uri;
83            if ($uri =~ m/%s/) {
84                    $uri = $feed->search_uri( $q );
85                    Jifty->log->info("Searching ", $feed->title, " at $uri");
86            } else {
87                    Jifty->log->info("Fetching ", $feed->title, " at $uri");
88            }
89    
90            my $items = Grep::Model::ItemCollection->new();
91    
92            sub abort {
93                    my $message = join(" ", @_);
94                    if ( $publish ) {
95                            Grep::Event::Result->new({
96                                    q => $q, coll => $items,
97                                    message => $message, class => 'error',
98                            })->publish;
99                            return 0;
100                    } else {
101                            $self->result->error( $message );
102                            return 0;
103                    }
104            }
105            my $ua = LWP::UserAgent->new;
106            $ua->default_header( 'Cookie' => $feed->cookie );
107            my $r = $ua->get( $uri );
108            return abort(
109                    $feed->title . " returned " . $r->status_line . " for $uri\n"
110            ) unless ( $r->is_success );
111    
112            my $content = $r->content;
113    
114            return abort( "No content returned from $uri" ) unless length( $content ) > 1;
115    
116    
117            my $xml_feed = XML::Feed->parse( \$content )
118                    or return abort( $feed->title, " returned ", XML::Feed->errstr, " for $uri" );
119    
120          my $xml_feed = XML::Feed->parse( URI->new( $feed->uri ) )          warn "getting entries from ", $xml_feed->title, "\n";
                 or die XML::Feed->errstr;  
121    
         warn "fetching ", $xml_feed->title, "\n";  
122          for my $entry ($xml_feed->entries) {          for my $entry ($xml_feed->entries) {
123                  my $i = Grep::Model::Item->new();                  my $i = Grep::Model::Item->new();
124    
# Line 76  sub take_action { Line 126  sub take_action {
126                          in_feed => $feed,                          in_feed => $feed,
127                          title => $entry->title,                          title => $entry->title,
128                          link => $entry->link,                          link => $entry->link,
129                          content => $entry->content,                          content => $entry->content->body,
130                          summary => $entry->summary,                          summary => $entry->summary->body,
131                          category => $entry->category,                          category => $entry->category,
132                          author => $entry->author,                          author => $entry->author,
133                          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,
134                          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,
135                  );                  );
136    
137                    if ( $i->id ) {
138                            $items->add_record( $i );
139    
140                            Jifty->log->debug("added ", $i->id, " to collection");
141                    } else {
142                            warn "can't add entry ", dump( $entry ) unless ( $i->id );
143                    }
144    
145          }          }
146    
147      $self->result->message('Success') if not $self->result->failure;          if ( my $count = $items->count ) {
148        
149      return 1;                  my $message = "$count results for '$q' in " . $feed->title;
150    
151                    $self->result->message( $message );
152    
153                    if ( $publish ) {
154                            Grep::Event::Result->new({
155                                    q => $q, coll => $items,
156                                    item_fragment => $self->argument_value('item_fragment'),
157                                    message => $message, class => 'message',
158                            })->publish;
159                            Jifty->log->debug( $items->count, " for '$q' published" );
160                    } else {
161                            $self->result->content( items => $items );
162                            Jifty->log->debug( $items->count, " for '$q' found" );
163                    }
164    
165            } else {
166                    return abort( "No results for '$q' in ' . $feed->title );
167            }
168    
169            return $items;
170  }  }
171    
172  1;  1;

Legend:
Removed from v.3  
changed lines
  Added in v.33

  ViewVC Help
Powered by ViewVC 1.1.26