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::Source; |
17 |
|
|
18 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
19 |
|
|
32 |
my $feeds = Grep::Model::FeedCollection->new; |
my $feeds = Grep::Model::FeedCollection->new; |
33 |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
34 |
$feeds->unlimit; |
$feeds->unlimit; |
35 |
warn "feeds ", $feeds->build_select_query; |
#warn "feeds ", $feeds->build_select_query; |
36 |
[{ |
[{ |
37 |
display_from => 'title', |
display_from => 'title', |
38 |
value_from => 'id', |
value_from => 'id', |
40 |
}]; |
}]; |
41 |
}; |
}; |
42 |
|
|
43 |
|
param item_fragment => |
44 |
|
label is 'Show', |
45 |
|
render as 'select', |
46 |
|
# valid are qw/result result_short/, |
47 |
|
# available are qw/result result_short/; |
48 |
|
available are qw/long short title/; |
49 |
|
|
50 |
}; |
}; |
51 |
|
|
52 |
=head2 take_action |
=head2 take_action |
53 |
|
|
54 |
|
Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will |
55 |
|
also be stored in local cache. |
56 |
|
|
57 |
=cut |
=cut |
58 |
|
|
59 |
sub take_action { |
sub take_action { |
61 |
|
|
62 |
# Custom action code |
# Custom action code |
63 |
|
|
64 |
my $feed = Grep::Model::Feed->new(); |
my $feed_id = $self->argument_value('feed') or die "no feed?"; |
65 |
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; |
|
|
} |
|
66 |
|
|
67 |
|
my $feed = Grep::Model::Feed->new(); |
68 |
$feed->load_by_cols( id => $feed_id ); |
$feed->load_by_cols( id => $feed_id ); |
69 |
|
die "can't load feed ", $feed_id unless ($feed->id); |
70 |
|
|
71 |
if (! $feed->id) { |
my $source = Grep::Source->new({ feed => $feed }); |
72 |
$self->result->message("Can't fetch feed $feed_id"); |
my $items = $source->search( $q ); |
73 |
return 0; |
my $new = $source->new_items; |
74 |
} |
|
75 |
|
if ( my $count = $items->count ) { |
76 |
my $uri = $feed->uri; |
|
77 |
if ($uri =~ m/%s/) { |
$count ||= 0; |
78 |
$uri = sprintf( $uri, $self->argument_value('q') ); |
|
79 |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
# construct a proper sentence :-) |
80 |
} else { |
my $message = $count |
81 |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
. ( $new == $count ? ' new' : '' ) |
82 |
} |
. ( $new == 0 ? ' old' : '' ) |
83 |
|
. ' results ' |
84 |
my $xml_feed = XML::Feed->parse( URI->new($uri) ) |
. ( $new && $new < $count ? "of which $new new " : '' ) |
85 |
or die XML::Feed->errstr; |
. "for '$q' in " . $source->feed->title; |
86 |
|
|
87 |
warn "fetching ", $xml_feed->title, "\n"; |
$self->result->message( $message ); |
|
|
|
|
my @results; |
|
|
|
|
|
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, |
|
|
); |
|
|
|
|
|
die "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
|
88 |
|
|
89 |
push @results, $i->id; |
$self->result->content( items => $items ); |
90 |
|
$self->result->content( count => $items->count ); |
91 |
|
|
92 |
Jifty->log->debug("item ", $i->id, " = ",dump( $entry ) ); |
return $items; |
|
} |
|
|
|
|
|
if ( @results ) { |
|
|
|
|
|
$self->result->message( $self->argument_value('q') . ' => ' . |
|
|
$xml_feed->entries . ' items: ' . join(",", @results) |
|
|
); |
|
|
|
|
|
$self->result->content( results => \@results ); |
|
|
return 1; |
|
93 |
|
|
94 |
} else { |
} else { |
95 |
|
return $self->result->error( "No results for '$q' in " . $feed->title ); |
|
$self->result->message( 'No results for ' . $self->argument_value('q') ); |
|
|
return 0; |
|
96 |
} |
} |
97 |
} |
} |
98 |
|
|
99 |
|
|
100 |
1; |
1; |
101 |
|
|