1 |
use strict; |
2 |
use warnings; |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
Grep::Action::Fetch |
7 |
|
8 |
=cut |
9 |
|
10 |
package Grep::Action::Fetch; |
11 |
use base qw/Grep::Action Jifty::Action/; |
12 |
|
13 |
use XML::Feed; |
14 |
use LWP::UserAgent; |
15 |
|
16 |
use Data::Dump qw/dump/; |
17 |
|
18 |
use Jifty::Param::Schema; |
19 |
use Jifty::Action schema { |
20 |
|
21 |
param q => |
22 |
type is 'text', |
23 |
label is 'Search for', |
24 |
hint is 'enter few words to search for'; |
25 |
|
26 |
param feed => |
27 |
label is 'From feed', |
28 |
render as 'select', |
29 |
available are defer { |
30 |
my $feeds = Grep::Model::FeedCollection->new; |
31 |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
32 |
$feeds->unlimit; |
33 |
warn "feeds ", $feeds->build_select_query; |
34 |
[{ |
35 |
display_from => 'title', |
36 |
value_from => 'id', |
37 |
collection => $feeds, |
38 |
}]; |
39 |
}; |
40 |
|
41 |
}; |
42 |
|
43 |
=head2 take_action |
44 |
|
45 |
=cut |
46 |
|
47 |
sub take_action { |
48 |
my $self = shift; |
49 |
|
50 |
# Custom action code |
51 |
|
52 |
my $feed = Grep::Model::Feed->new(); |
53 |
my $feed_id = $self->argument_value('feed'); |
54 |
|
55 |
if (! $feed_id) { |
56 |
$self->result->message("Need feed ID"); |
57 |
return 0; |
58 |
} |
59 |
|
60 |
$feed->load_by_cols( id => $feed_id ); |
61 |
|
62 |
if (! $feed->id) { |
63 |
$self->result->message("Can't fetch feed $feed_id"); |
64 |
return 0; |
65 |
} |
66 |
|
67 |
my $uri = $feed->uri; |
68 |
if ($uri =~ m/%s/) { |
69 |
$uri = $feed->search_uri( $self->argument_value('q') ); |
70 |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
71 |
} else { |
72 |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
73 |
} |
74 |
|
75 |
my $ua = LWP::UserAgent->new; |
76 |
$ua->default_header( 'Cookie' => $feed->cookie ); |
77 |
my $r = $ua->get( $uri ); |
78 |
die "Can't fetch $uri: " . $r->status_line unless ( $r->is_success ); |
79 |
|
80 |
my $content = $r->content; |
81 |
|
82 |
die "No content returned from $uri" unless length( $content ) > 1; |
83 |
|
84 |
my $xml_feed = XML::Feed->parse( \$content ) |
85 |
or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n"; |
86 |
|
87 |
warn "getting entries from ", $xml_feed->title, "\n"; |
88 |
|
89 |
my @items; |
90 |
|
91 |
for my $entry ($xml_feed->entries) { |
92 |
my $i = Grep::Model::Item->new(); |
93 |
|
94 |
$i->load_or_create( |
95 |
in_feed => $feed, |
96 |
title => $entry->title, |
97 |
link => $entry->link, |
98 |
content => $entry->content->body, |
99 |
summary => $entry->summary->body, |
100 |
category => $entry->category, |
101 |
author => $entry->author, |
102 |
issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef, |
103 |
modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef, |
104 |
); |
105 |
|
106 |
die "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
107 |
|
108 |
push @items, $i; |
109 |
|
110 |
# Grep::Event::Result->new( $i )->publish; |
111 |
|
112 |
Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) ); |
113 |
} |
114 |
|
115 |
if ( @items ) { |
116 |
|
117 |
$self->result->message( $self->argument_value('q') . ' => ' . |
118 |
$xml_feed->entries . ' items: ' . join(",", map { $_->id } @items) |
119 |
); |
120 |
|
121 |
$self->result->content( items => \@items ); |
122 |
return 1; |
123 |
|
124 |
} else { |
125 |
|
126 |
$self->result->error( 'No results found' ); |
127 |
return 0; |
128 |
|
129 |
} |
130 |
} |
131 |
|
132 |
1; |
133 |
|