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 |
}; |
49 |
|
50 |
=head2 take_action |
51 |
|
52 |
Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will |
53 |
also be stored in local cache. |
54 |
|
55 |
=cut |
56 |
|
57 |
sub take_action { |
58 |
my $self = shift; |
59 |
|
60 |
# Custom action code |
61 |
|
62 |
my $feed = Grep::Model::Feed->new(); |
63 |
my $feed_id = $self->argument_value('feed'); |
64 |
my $q = $self->argument_value('q'); |
65 |
my $user_id = Jifty->web->current_user->id; |
66 |
|
67 |
if (! $feed_id) { |
68 |
$self->result->message("Need feed ID"); |
69 |
return 0; |
70 |
} |
71 |
|
72 |
$feed->load_by_cols( id => $feed_id ); |
73 |
|
74 |
if (! $feed->id) { |
75 |
$self->result->message("Can't fetch feed $feed_id"); |
76 |
return 0; |
77 |
} |
78 |
|
79 |
my $message; |
80 |
my $uri = $feed->uri; |
81 |
if ($uri =~ m/%s/) { |
82 |
$uri = $feed->search_uri( $q ); |
83 |
$message = 'Searching'; |
84 |
} else { |
85 |
$message = 'Fetching'; |
86 |
} |
87 |
$message .= ' ' . $feed->title . " at $uri"; |
88 |
|
89 |
Jifty->log->info( $message ); |
90 |
|
91 |
my $items = Grep::Model::ItemCollection->new(); |
92 |
|
93 |
my $ua = LWP::UserAgent->new; |
94 |
$ua->default_header( 'Cookie' => $feed->cookie ); |
95 |
my $r = $ua->get( $uri ); |
96 |
return $self->result->error( |
97 |
$feed->title . " returned " . $r->status_line . " for $uri\n" |
98 |
) unless ( $r->is_success ); |
99 |
|
100 |
my $content = $r->content; |
101 |
|
102 |
return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1; |
103 |
|
104 |
|
105 |
my $xml_feed = XML::Feed->parse( \$content ) |
106 |
or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr, " for $uri" ); |
107 |
|
108 |
warn "getting entries from ", $xml_feed->title, "\n"; |
109 |
|
110 |
for my $entry ($xml_feed->entries) { |
111 |
my $i = Grep::Model::Item->new(); |
112 |
|
113 |
$i->load_or_create( |
114 |
in_feed => $feed, |
115 |
title => $entry->title, |
116 |
link => $entry->link, |
117 |
content => $entry->content->body, |
118 |
summary => $entry->summary->body, |
119 |
category => $entry->category, |
120 |
author => $entry->author, |
121 |
issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S") : undef, |
122 |
modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S") : undef, |
123 |
); |
124 |
|
125 |
if ( $i->id ) { |
126 |
$items->add_record( $i ); |
127 |
|
128 |
Jifty->log->debug("added ", $i->id, " to collection"); |
129 |
} else { |
130 |
warn "can't add entry ", dump( $entry ) unless ( $i->id ); |
131 |
} |
132 |
|
133 |
} |
134 |
|
135 |
if ( my $count = $items->count ) { |
136 |
|
137 |
my $message = "$count results for '$q' in " . $feed->title; |
138 |
|
139 |
$self->result->message( $message ); |
140 |
|
141 |
$self->result->content( items => $items ); |
142 |
$self->result->content( count => $items->count ); |
143 |
|
144 |
} else { |
145 |
return $self->result->error( "No results for '$q' in " . $feed->title ); |
146 |
} |
147 |
|
148 |
return $items; |
149 |
} |
150 |
|
151 |
1; |
152 |
|