11 |
use base qw/Grep::Action Jifty::Action/; |
use base qw/Grep::Action Jifty::Action/; |
12 |
|
|
13 |
use XML::Feed; |
use XML::Feed; |
14 |
|
use LWP::UserAgent; |
15 |
|
|
16 |
|
use Grep::Search; |
17 |
|
use Grep::Source; |
18 |
|
|
19 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
20 |
|
|
28 |
|
|
29 |
param feed => |
param feed => |
30 |
label is 'From feed', |
label is 'From feed', |
31 |
render as 'combobox', |
render as 'select', |
32 |
available are defer { |
available are defer { |
33 |
my $feeds = Grep::Model::FeedCollection->new; |
my $feeds = Grep::Model::FeedCollection->new; |
34 |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
41 |
}]; |
}]; |
42 |
}; |
}; |
43 |
|
|
44 |
|
param item_fragment => |
45 |
|
label is 'Show', |
46 |
|
render as 'select', |
47 |
|
# valid are qw/result result_short/, |
48 |
|
# available are qw/result result_short/; |
49 |
|
available are qw/long short title/; |
50 |
|
|
51 |
}; |
}; |
52 |
|
|
53 |
=head2 take_action |
=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 |
=cut |
59 |
|
|
60 |
sub take_action { |
sub take_action { |
62 |
|
|
63 |
# Custom action code |
# Custom action code |
64 |
|
|
65 |
my $feed = Grep::Model::Feed->new(); |
my $feed_id = $self->argument_value('feed') or die "no feed?"; |
66 |
my $feed_id = $self->argument_value('feed'); |
my $q = $self->argument_value('q') or die "no q?"; |
|
|
|
|
if (! $feed_id) { |
|
|
$self->result->message("Need feed ID"); |
|
|
return 0; |
|
|
} |
|
67 |
|
|
68 |
|
my $feed = Grep::Model::Feed->new(); |
69 |
$feed->load_by_cols( id => $feed_id ); |
$feed->load_by_cols( id => $feed_id ); |
70 |
|
die "can't load feed ", $feed_id unless ($feed->id); |
71 |
|
|
72 |
if (! $feed->id) { |
my $source = Grep::Source->new({ feed => $feed }); |
73 |
$self->result->message("Can't fetch feed $feed_id"); |
my $items = $source->search( $q ); |
74 |
return 0; |
my $new = $source->new_items; |
75 |
} |
|
76 |
|
if ( my $count = $items->count ) { |
77 |
Jifty->log->info("Fetching ", $feed->title, " from ", $feed->uri); |
|
78 |
|
# construct a proper sentence :-) |
79 |
|
my $message = $count |
80 |
|
. ( $new == $count ? ' new' : '' ) |
81 |
|
. ( $new == 0 ? ' old' : '' ) |
82 |
|
. ' results ' |
83 |
|
. ( $new && $new < $count ? "of which $new new " : '' ) |
84 |
|
. "for '$q' in " . $source->feed->title; |
85 |
|
|
86 |
|
$self->result->message( $message ); |
87 |
|
|
88 |
my $xml_feed = XML::Feed->parse( URI->new( $feed->uri ) ) |
$self->result->content( items => $items ); |
89 |
or die XML::Feed->errstr; |
$self->result->content( count => $items->count ); |
90 |
|
|
91 |
warn "fetching ", $xml_feed->title, "\n"; |
return $items; |
|
for my $entry ($xml_feed->entries) { |
|
|
my $i = Grep::Model::Item->new(); |
|
|
|
|
|
$i->load_or_create( |
|
|
in_feed => $feed, |
|
|
title => $entry->title, |
|
|
link => $entry->link, |
|
|
content => $entry->content->body, |
|
|
summary => $entry->summary->body, |
|
|
category => $entry->category, |
|
|
author => $entry->author, |
|
|
issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef, |
|
|
modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef, |
|
|
); |
|
92 |
|
|
93 |
Jifty->log->debug("entry = ",dump( $entry ) ); |
} else { |
94 |
|
return $self->result->error( "No results for '$q'" ); |
95 |
} |
} |
|
|
|
|
$self->result->message('Success') if not $self->result->failure; |
|
|
|
|
|
return 1; |
|
96 |
} |
} |
97 |
|
|
98 |
|
|
99 |
1; |
1; |
100 |
|
|