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 |
|
70 |
if (! $feed_id) { |
71 |
$self->result->message("Need feed ID"); |
72 |
return 0; |
73 |
} |
74 |
|
75 |
$feed->load_by_cols( id => $feed_id ); |
76 |
|
77 |
if (! $feed->id) { |
78 |
$self->result->message("Can't fetch feed $feed_id"); |
79 |
return 0; |
80 |
} |
81 |
|
82 |
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 |
warn "getting entries from ", $xml_feed->title, "\n"; |
121 |
|
122 |
for my $entry ($xml_feed->entries) { |
123 |
my $i = Grep::Model::Item->new(); |
124 |
|
125 |
$i->load_or_create( |
126 |
in_feed => $feed, |
127 |
title => $entry->title, |
128 |
link => $entry->link, |
129 |
content => $entry->content->body, |
130 |
summary => $entry->summary->body, |
131 |
category => $entry->category, |
132 |
author => $entry->author, |
133 |
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") : 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 |
if ( $items ) { |
148 |
|
149 |
my $message = $q . ' => ' . $xml_feed->entries . ' results 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 => 'messages', |
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 found' ); |
167 |
} |
168 |
|
169 |
return $items; |
170 |
} |
171 |
|
172 |
1; |
173 |
|