/[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

Annotation of /lib/Grep/Action/Fetch.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 47 - (hide annotations)
Wed Feb 21 03:04:48 2007 UTC (17 years ago) by dpavlin
File size: 3704 byte(s)
use real full-text search engine (Lucene in this case) for Search action,
added Grep::Search helper object
1 dpavlin 3 use strict;
2     use warnings;
3    
4     =head1 NAME
5    
6     Grep::Action::Fetch
7    
8     =cut
9    
10     package Grep::Action::Fetch;
11     use base qw/Grep::Action Jifty::Action/;
12    
13     use XML::Feed;
14 dpavlin 21 use LWP::UserAgent;
15 dpavlin 3
16 dpavlin 47 use Grep::Search;
17    
18 dpavlin 3 use Data::Dump qw/dump/;
19    
20     use Jifty::Param::Schema;
21     use Jifty::Action schema {
22    
23     param q =>
24     type is 'text',
25     label is 'Search for',
26     hint is 'enter few words to search for';
27    
28     param feed =>
29     label is 'From feed',
30 dpavlin 7 render as 'select',
31 dpavlin 3 available are defer {
32     my $feeds = Grep::Model::FeedCollection->new;
33     $feeds->order_by({ column => 'title', order => 'ASC' });
34     $feeds->unlimit;
35     warn "feeds ", $feeds->build_select_query;
36     [{
37     display_from => 'title',
38     value_from => 'id',
39     collection => $feeds,
40     }];
41     };
42    
43 dpavlin 25 param item_fragment =>
44     label is 'Show',
45     render as 'select',
46 dpavlin 28 # valid are qw/result result_short/,
47     # available are qw/result result_short/;
48     available are qw/long short title/;
49 dpavlin 25
50 dpavlin 3 };
51    
52     =head2 take_action
53    
54 dpavlin 29 Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
55     also be stored in local cache.
56    
57 dpavlin 3 =cut
58    
59     sub take_action {
60     my $self = shift;
61    
62     # Custom action code
63    
64     my $feed = Grep::Model::Feed->new();
65     my $feed_id = $self->argument_value('feed');
66 dpavlin 29 my $q = $self->argument_value('q');
67 dpavlin 43 my $user_id = Jifty->web->current_user->id;
68 dpavlin 3
69     if (! $feed_id) {
70     $self->result->message("Need feed ID");
71     return 0;
72     }
73    
74     $feed->load_by_cols( id => $feed_id );
75    
76     if (! $feed->id) {
77     $self->result->message("Can't fetch feed $feed_id");
78     return 0;
79     }
80    
81 dpavlin 43 my $message;
82 dpavlin 7 my $uri = $feed->uri;
83     if ($uri =~ m/%s/) {
84 dpavlin 29 $uri = $feed->search_uri( $q );
85 dpavlin 43 $message = 'Searching';
86 dpavlin 7 } else {
87 dpavlin 43 $message = 'Fetching';
88 dpavlin 7 }
89 dpavlin 44 $message .= ' ' . $feed->title . " at $uri";
90 dpavlin 3
91 dpavlin 43 Jifty->log->info( $message );
92    
93 dpavlin 31 my $items = Grep::Model::ItemCollection->new();
94    
95 dpavlin 21 my $ua = LWP::UserAgent->new;
96     $ua->default_header( 'Cookie' => $feed->cookie );
97     my $r = $ua->get( $uri );
98 dpavlin 44 return $self->result->error(
99 dpavlin 31 $feed->title . " returned " . $r->status_line . " for $uri\n"
100     ) unless ( $r->is_success );
101 dpavlin 21
102     my $content = $r->content;
103    
104 dpavlin 44 return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1;
105 dpavlin 21
106 dpavlin 31
107 dpavlin 21 my $xml_feed = XML::Feed->parse( \$content )
108 dpavlin 44 or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr, " for $uri" );
109 dpavlin 3
110 dpavlin 21 warn "getting entries from ", $xml_feed->title, "\n";
111 dpavlin 7
112 dpavlin 46 my $new;
113    
114 dpavlin 3 for my $entry ($xml_feed->entries) {
115     my $i = Grep::Model::Item->new();
116    
117 dpavlin 46 my ($ok,$msg) = $i->load_or_create(
118 dpavlin 3 in_feed => $feed,
119     title => $entry->title,
120     link => $entry->link,
121 dpavlin 5 content => $entry->content->body,
122     summary => $entry->summary->body,
123 dpavlin 3 category => $entry->category,
124     author => $entry->author,
125 dpavlin 29 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") : undef,
127 dpavlin 3 );
128 dpavlin 5
129 dpavlin 47 $msg ||= '';
130    
131 dpavlin 46 if ( $ok ) {
132     Jifty->log->debug("item ", $i->id, ": $msg");
133 dpavlin 29 $items->add_record( $i );
134 dpavlin 47
135     # is new record?
136     if ( $msg !~ m/^Found/ ) {
137     $new++;
138     Grep::Search->add( $i );
139     }
140 dpavlin 25 } else {
141 dpavlin 46 warn "can't add entry ", dump( $entry ), "\n";
142 dpavlin 25 }
143 dpavlin 9
144 dpavlin 3 }
145    
146 dpavlin 33 if ( my $count = $items->count ) {
147 dpavlin 7
148 dpavlin 46 # construct a proper sentence :-)
149     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 dpavlin 31 $self->result->message( $message );
157    
158 dpavlin 44 $self->result->content( items => $items );
159 dpavlin 42 $self->result->content( count => $items->count );
160 dpavlin 7
161 dpavlin 47 Grep::Search->finish if $new;
162    
163 dpavlin 7 } else {
164 dpavlin 44 return $self->result->error( "No results for '$q' in " . $feed->title );
165 dpavlin 7 }
166 dpavlin 29
167     return $items;
168 dpavlin 3 }
169    
170     1;
171    

  ViewVC Help
Powered by ViewVC 1.1.26