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 |
|
15 |
use Data::Dump qw/dump/; |
16 |
|
17 |
use Jifty::Param::Schema; |
18 |
use Jifty::Action schema { |
19 |
|
20 |
param q => |
21 |
type is 'text', |
22 |
label is 'Search for', |
23 |
hint is 'enter few words to search for'; |
24 |
|
25 |
param feed => |
26 |
label is 'From feed', |
27 |
render as 'select', |
28 |
available are defer { |
29 |
my $feeds = Grep::Model::FeedCollection->new; |
30 |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
31 |
$feeds->unlimit; |
32 |
warn "feeds ", $feeds->build_select_query; |
33 |
[{ |
34 |
display_from => 'title', |
35 |
value_from => 'id', |
36 |
collection => $feeds, |
37 |
}]; |
38 |
}; |
39 |
|
40 |
}; |
41 |
|
42 |
=head2 take_action |
43 |
|
44 |
=cut |
45 |
|
46 |
sub take_action { |
47 |
my $self = shift; |
48 |
|
49 |
# Custom action code |
50 |
|
51 |
my $feed = Grep::Model::Feed->new(); |
52 |
my $feed_id = $self->argument_value('feed'); |
53 |
|
54 |
if (! $feed_id) { |
55 |
$self->result->message("Need feed ID"); |
56 |
return 0; |
57 |
} |
58 |
|
59 |
$feed->load_by_cols( id => $feed_id ); |
60 |
|
61 |
if (! $feed->id) { |
62 |
$self->result->message("Can't fetch feed $feed_id"); |
63 |
return 0; |
64 |
} |
65 |
|
66 |
my $uri = $feed->uri; |
67 |
if ($uri =~ m/%s/) { |
68 |
$uri = sprintf( $uri, $self->argument_value('q') ); |
69 |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
70 |
} else { |
71 |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
72 |
} |
73 |
|
74 |
my $xml_feed = XML::Feed->parse( URI->new($uri) ) |
75 |
or die XML::Feed->errstr; |
76 |
|
77 |
warn "fetching ", $xml_feed->title, "\n"; |
78 |
|
79 |
my @results; |
80 |
|
81 |
for my $entry ($xml_feed->entries) { |
82 |
my $i = Grep::Model::Item->new(); |
83 |
|
84 |
$i->load_or_create( |
85 |
in_feed => $feed, |
86 |
title => $entry->title, |
87 |
link => $entry->link, |
88 |
content => $entry->content->body, |
89 |
summary => $entry->summary->body, |
90 |
category => $entry->category, |
91 |
author => $entry->author, |
92 |
issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef, |
93 |
modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef, |
94 |
); |
95 |
|
96 |
die "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
97 |
|
98 |
push @results, $i->id; |
99 |
|
100 |
Jifty->log->debug("item ", $i->id, " = ",dump( $entry ) ); |
101 |
} |
102 |
|
103 |
if ( @results ) { |
104 |
|
105 |
$self->result->message( $self->argument_value('q') . ' => ' . |
106 |
$xml_feed->entries . ' items: ' . join(",", @results) |
107 |
); |
108 |
|
109 |
$self->result->content( results => \@results ); |
110 |
return 1; |
111 |
|
112 |
} else { |
113 |
|
114 |
$self->result->message( 'No results for ' . $self->argument_value('q') ); |
115 |
return 0; |
116 |
} |
117 |
} |
118 |
|
119 |
1; |
120 |
|