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', |
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 $q = $self->argument_value('q'); |
|
|
my $user_id = Jifty->web->current_user->id; |
|
|
|
|
|
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; |
|
} |
|
|
|
|
|
my $message; |
|
|
my $uri = $feed->uri; |
|
|
if ($uri =~ m/%s/) { |
|
|
$uri = $feed->search_uri( $q ); |
|
|
$message = 'Searching'; |
|
|
} else { |
|
|
$message = 'Fetching'; |
|
|
} |
|
|
$message .= ' ' . $feed->title . " at $uri"; |
|
|
|
|
|
Jifty->log->info( $message ); |
|
|
|
|
|
my $items = Grep::Model::ItemCollection->new(); |
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
|
|
$ua->default_header( 'Cookie' => $feed->cookie ); |
|
|
my $r = $ua->get( $uri ); |
|
|
return $self->result->error( |
|
|
$feed->title . " returned " . $r->status_line . " for $uri\n" |
|
|
) unless ( $r->is_success ); |
|
|
|
|
|
my $content = $r->content; |
|
|
|
|
|
return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1; |
|
|
|
|
|
|
|
|
my $xml_feed = XML::Feed->parse( \$content ) |
|
|
or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr, " for $uri" ); |
|
|
|
|
|
warn "getting entries from ", $xml_feed->title, "\n"; |
|
|
|
|
|
my $new; |
|
|
|
|
|
for my $entry ($xml_feed->entries) { |
|
|
my $i = Grep::Model::Item->new(); |
|
|
|
|
|
my ($ok,$msg) = $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 ( $ok ) { |
|
|
Jifty->log->debug("item ", $i->id, ": $msg"); |
|
|
$items->add_record( $i ); |
|
|
# count new objects |
|
|
$new++ if ($msg !~ m/^Found/); |
|
|
} else { |
|
|
warn "can't add entry ", dump( $entry ), "\n"; |
|
|
} |
|
|
|
|
|
} |
|
74 |
|
|
75 |
if ( my $count = $items->count ) { |
if ( my $count = $items->count ) { |
76 |
|
|
77 |
|
$count ||= 0; |
78 |
|
|
79 |
# construct a proper sentence :-) |
# construct a proper sentence :-) |
80 |
my $message = $count |
my $message = $count |
81 |
. ( $new == $count ? ' new' : '' ) |
. ( $new == $count ? ' new' : '' ) |
82 |
. ( $new == 0 ? ' old' : '' ) |
. ( $new == 0 ? ' old' : '' ) |
83 |
. ' results ' |
. ' results ' |
84 |
. ( $new && $new < $count ? "of which $new new " : '' ) |
. ( $new && $new < $count ? "of which $new new " : '' ) |
85 |
. "for '$q' in " . $feed->title; |
. "for '$q' in " . $source->feed->title; |
86 |
|
|
87 |
$self->result->message( $message ); |
$self->result->message( $message ); |
88 |
|
|
89 |
$self->result->content( items => $items ); |
$self->result->content( items => $items ); |
90 |
$self->result->content( count => $items->count ); |
$self->result->content( count => $items->count ); |
91 |
|
|
92 |
|
return $items; |
93 |
|
|
94 |
} else { |
} else { |
95 |
return $self->result->error( "No results for '$q' in " . $feed->title ); |
return $self->result->error( "No results for '$q' in " . $feed->title ); |
96 |
} |
} |
|
|
|
|
return $items; |
|
97 |
} |
} |
98 |
|
|
99 |
|
|
100 |
1; |
1; |
101 |
|
|