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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 47 - (show annotations)
Wed Feb 21 03:04:48 2007 UTC (17 years, 2 months 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 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 use LWP::UserAgent;
15
16 use Grep::Search;
17
18 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 render as 'select',
31 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 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
53
54 Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
55 also be stored in local cache.
56
57 =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 my $q = $self->argument_value('q');
67 my $user_id = Jifty->web->current_user->id;
68
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 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 $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 return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1;
105
106
107 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) {
115 my $i = Grep::Model::Item->new();
116
117 my ($ok,$msg) = $i->load_or_create(
118 in_feed => $feed,
119 title => $entry->title,
120 link => $entry->link,
121 content => $entry->content->body,
122 summary => $entry->summary->body,
123 category => $entry->category,
124 author => $entry->author,
125 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 );
128
129 $msg ||= '';
130
131 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
144 }
145
146 if ( my $count = $items->count ) {
147
148 # 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 $self->result->message( $message );
157
158 $self->result->content( items => $items );
159 $self->result->content( count => $items->count );
160
161 Grep::Search->finish if $new;
162
163 } else {
164 return $self->result->error( "No results for '$q' in " . $feed->title );
165 }
166
167 return $items;
168 }
169
170 1;
171

  ViewVC Help
Powered by ViewVC 1.1.26