13 |
use XML::Feed; |
use XML::Feed; |
14 |
use LWP::UserAgent; |
use LWP::UserAgent; |
15 |
|
|
16 |
|
use Grep::Source; |
17 |
|
|
18 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
19 |
|
|
20 |
use Jifty::Param::Schema; |
use Jifty::Param::Schema; |
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', |
47 |
# available are qw/result result_short/; |
# available are qw/result result_short/; |
48 |
available are qw/long short title/; |
available are qw/long short title/; |
49 |
|
|
|
param publish => |
|
|
label is 'In which queue?'; |
|
|
|
|
50 |
}; |
}; |
51 |
|
|
52 |
=head2 take_action |
=head2 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?"; |
|
my $publish = $self->argument_value('publish'); |
|
|
my $q = $self->argument_value('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/) { |
# construct a proper sentence :-) |
78 |
$uri = $feed->search_uri( $q ); |
my $message = $count |
79 |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
. ( $new == $count ? ' new' : '' ) |
80 |
} else { |
. ( $new == 0 ? ' old' : '' ) |
81 |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
. ' results ' |
82 |
} |
. ( $new && $new < $count ? "of which $new new " : '' ) |
83 |
|
. "for '$q' in " . $source->feed->title; |
84 |
my $items = Grep::Model::ItemCollection->new(); |
|
|
|
|
|
sub abort { |
|
|
my $message = join(" ", @_); |
|
|
if ( $publish ) { |
|
|
Grep::Event::Result->new({ |
|
|
q => $q, coll => $items, |
|
|
message => $message, class => 'error', |
|
|
})->publish; |
|
|
return 0; |
|
|
} else { |
|
|
$self->result->error( $message ); |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
my $ua = LWP::UserAgent->new; |
|
|
$ua->default_header( 'Cookie' => $feed->cookie ); |
|
|
my $r = $ua->get( $uri ); |
|
|
return abort( |
|
|
$feed->title . " returned " . $r->status_line . " for $uri\n" |
|
|
) unless ( $r->is_success ); |
|
|
|
|
|
my $content = $r->content; |
|
|
|
|
|
return abort( "No content returned from $uri" ) unless length( $content ) > 1; |
|
|
|
|
|
|
|
|
my $xml_feed = XML::Feed->parse( \$content ) |
|
|
or return abort( $feed->title, " returned ", XML::Feed->errstr, " for $uri" ); |
|
|
|
|
|
warn "getting entries from ", $xml_feed->title, "\n"; |
|
|
|
|
|
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") : undef, |
|
|
modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S") : undef, |
|
|
); |
|
|
|
|
|
if ( $i->id ) { |
|
|
$items->add_record( $i ); |
|
|
|
|
|
Jifty->log->debug("added ", $i->id, " to collection"); |
|
|
} else { |
|
|
warn "can't add entry ", dump( $entry ) unless ( $i->id ); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if ( $items ) { |
|
|
|
|
|
my $message = $q . ' => ' . $xml_feed->entries . ' results in ' . $feed->title; |
|
|
|
|
85 |
$self->result->message( $message ); |
$self->result->message( $message ); |
86 |
|
|
87 |
if ( $publish ) { |
$self->result->content( items => $items ); |
88 |
Grep::Event::Result->new({ |
$self->result->content( count => $items->count ); |
89 |
q => $q, coll => $items, |
|
90 |
item_fragment => $self->argument_value('item_fragment'), |
return $items; |
|
message => $message, class => 'messages', |
|
|
})->publish; |
|
|
Jifty->log->debug( $items->count, " for '$q' published" ); |
|
|
} else { |
|
|
$self->result->content( items => $items ); |
|
|
Jifty->log->debug( $items->count, " for '$q' found" ); |
|
|
} |
|
91 |
|
|
92 |
} else { |
} else { |
93 |
return abort( 'No results found' ); |
return $self->result->error( "No results for '$q' in " . $feed->title ); |
94 |
} |
} |
|
|
|
|
return $items; |
|
95 |
} |
} |
96 |
|
|
97 |
|
|
98 |
1; |
1; |
99 |
|
|