/[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 43 - (show annotations)
Tue Feb 20 16:26:56 2007 UTC (17 years, 3 months ago) by dpavlin
File size: 4477 byte(s)
small refactoring for better debugging messages while exploring Jifty::Event
way of match(ing) events (while my use is more filter-like) and de-crufting code
in places
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 Data::Dump qw/dump/;
17
18 use Jifty::Param::Schema;
19 use Jifty::Action schema {
20
21 param q =>
22 type is 'text',
23 label is 'Search for',
24 hint is 'enter few words to search for';
25
26 param feed =>
27 label is 'From feed',
28 render as 'select',
29 available are defer {
30 my $feeds = Grep::Model::FeedCollection->new;
31 $feeds->order_by({ column => 'title', order => 'ASC' });
32 $feeds->unlimit;
33 warn "feeds ", $feeds->build_select_query;
34 [{
35 display_from => 'title',
36 value_from => 'id',
37 collection => $feeds,
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
54
55 Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
56 also be stored in local cache.
57
58 =cut
59
60 sub take_action {
61 my $self = shift;
62
63 # Custom action code
64
65 my $feed = Grep::Model::Feed->new();
66 my $feed_id = $self->argument_value('feed');
67 my $publish = $self->argument_value('publish');
68 my $q = $self->argument_value('q');
69 my $user_id = Jifty->web->current_user->id;
70
71 if (! $feed_id) {
72 $self->result->message("Need feed ID");
73 return 0;
74 }
75
76 $feed->load_by_cols( id => $feed_id );
77
78 if (! $feed->id) {
79 $self->result->message("Can't fetch feed $feed_id");
80 return 0;
81 }
82
83 my $message;
84 my $uri = $feed->uri;
85 if ($uri =~ m/%s/) {
86 $uri = $feed->search_uri( $q );
87 $message = 'Searching';
88 } else {
89 $message = 'Fetching';
90 }
91 $message .= ' ' . ( $publish ? "and publishing for $user_id" : '' ) . ' ' . $feed->title . " at $uri";
92
93 Jifty->log->info( $message );
94
95 my $items = Grep::Model::ItemCollection->new();
96
97 my $ua = LWP::UserAgent->new;
98 $ua->default_header( 'Cookie' => $feed->cookie );
99 my $r = $ua->get( $uri );
100 return $self->abort(
101 $feed->title . " returned " . $r->status_line . " for $uri\n"
102 ) unless ( $r->is_success );
103
104 my $content = $r->content;
105
106 return $self->abort( "No content returned from $uri" ) unless length( $content ) > 1;
107
108
109 my $xml_feed = XML::Feed->parse( \$content )
110 or return $self->abort( $feed->title, " returned ", XML::Feed->errstr, " for $uri" );
111
112 warn "getting entries from ", $xml_feed->title, "\n";
113
114 for my $entry ($xml_feed->entries) {
115 my $i = Grep::Model::Item->new();
116
117 $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 if ( $i->id ) {
130 $items->add_record( $i );
131
132 Jifty->log->debug("added ", $i->id, " to collection");
133 } else {
134 warn "can't add entry ", dump( $entry ) unless ( $i->id );
135 }
136
137 }
138
139 if ( my $count = $items->count ) {
140
141 my $message = "$count results for '$q' in " . $feed->title;
142
143 $self->result->message( $message );
144
145 if ( $publish ) {
146 Grep::Event::Result->new({
147 q => $q, coll => $items,
148 item_fragment => $self->argument_value('item_fragment'),
149 message => $message, class => 'message',
150 user_id => $user_id,
151 })->publish;
152 Jifty->log->debug( "$count published for user $user_id for '$q'" );
153 } else {
154 $self->result->content( items => $items );
155 Jifty->log->debug( "$count found for user $user_id for '$q'" );
156 }
157 $self->result->content( count => $items->count );
158
159 } else {
160 return $self->abort( "No results for '$q' in " . $feed->title );
161 }
162
163 return $items;
164 }
165
166 =head2 abort
167
168 Abort action with error message which support publish or normal messages
169
170 =cut
171
172 sub abort {
173 my $self = shift;
174
175 my $message = join(" ", @_);
176
177 my $publish = $self->argument_value('publish');
178 my $q = $self->argument_value('q');
179 my $user_id = Jifty->web->current_user->id;
180
181 Jifty->log->debug("ABORT: $message", $publish ? " for $user_id" : '');
182
183 if ( $publish ) {
184 Grep::Event::Result->new({
185 q => $q, coll => Grep::Model::ItemCollection->new(),
186 message => $message, class => 'error',
187 user_id => $user_id,
188 })->publish;
189 return;
190 } else {
191 $self->result->error( $message );
192 return;
193 }
194 }
195
196 1;
197

  ViewVC Help
Powered by ViewVC 1.1.26