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 |
|
|
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 |
my $uri = $feed->uri; |
|
78 |
if ($uri =~ m/%s/) { |
# construct a proper sentence :-) |
79 |
$uri = sprintf( $uri, $self->argument_value('q') ); |
my $message = $count |
80 |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
. ( $new == $count ? ' new' : '' ) |
81 |
} else { |
. ( $new == 0 ? ' old' : '' ) |
82 |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
. ' results ' |
83 |
} |
. ( $new && $new < $count ? "of which $new new " : '' ) |
84 |
|
. "for '$q' in " . $source->feed->title; |
85 |
my $xml_feed = XML::Feed->parse( URI->new($uri) ) |
|
86 |
or die XML::Feed->errstr; |
$self->result->message( $message ); |
87 |
|
|
88 |
warn "fetching ", $xml_feed->title, "\n"; |
$self->result->content( items => $items ); |
89 |
|
$self->result->content( count => $items->count ); |
90 |
|
|
91 |
my @results; |
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, |
|
|
); |
|
|
|
|
|
die "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
|
|
|
|
|
push @results, $i->id; |
|
|
|
|
|
Jifty->log->debug("item ", $i->id, " = ",dump( $entry ) ); |
|
|
} |
|
|
|
|
|
if ( @results ) { |
|
|
|
|
|
$self->result->message( $self->argument_value('q') . ' => ' . |
|
|
$xml_feed->entries . ' items: ' . join(",", @results) |
|
|
); |
|
|
|
|
|
$self->result->content( results => \@results ); |
|
|
return 1; |
|
92 |
|
|
93 |
} else { |
} else { |
94 |
|
return $self->result->error( "No results for '$q' in " . $feed->title ); |
|
$self->result->message( 'No results found' ); |
|
|
|
|
|
# with default sticky_on_failure, this will keep form data |
|
|
$self->result->failure( 1 ); |
|
|
|
|
|
return 0; |
|
95 |
} |
} |
96 |
} |
} |
97 |
|
|
98 |
|
|
99 |
1; |
1; |
100 |
|
|