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 Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
17 |
|
|
25 |
|
|
26 |
param feed => |
param feed => |
27 |
label is 'From feed', |
label is 'From feed', |
28 |
render as 'combobox', |
render as 'select', |
29 |
available are defer { |
available are defer { |
30 |
my $feeds = Grep::Model::FeedCollection->new; |
my $feeds = Grep::Model::FeedCollection->new; |
31 |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
38 |
}]; |
}]; |
39 |
}; |
}; |
40 |
|
|
41 |
|
param item_fragment => |
42 |
|
label is 'Show', |
43 |
|
render as 'select', |
44 |
|
# valid are qw/result result_short/, |
45 |
|
# available are qw/result result_short/; |
46 |
|
available are qw/long short title/; |
47 |
|
|
48 |
|
param publish => |
49 |
|
label is 'In which queue?'; |
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 { |
64 |
|
|
65 |
my $feed = Grep::Model::Feed->new(); |
my $feed = Grep::Model::Feed->new(); |
66 |
my $feed_id = $self->argument_value('feed'); |
my $feed_id = $self->argument_value('feed'); |
67 |
|
my $publish = $self->argument_value('publish'); |
68 |
|
my $q = $self->argument_value('q'); |
69 |
|
|
70 |
if (! $feed_id) { |
if (! $feed_id) { |
71 |
$self->result->message("Need feed ID"); |
$self->result->message("Need feed ID"); |
79 |
return 0; |
return 0; |
80 |
} |
} |
81 |
|
|
82 |
Jifty->log->info("Fetching ", $feed->title, " from ", $feed->uri); |
my $uri = $feed->uri; |
83 |
|
if ($uri =~ m/%s/) { |
84 |
|
$uri = $feed->search_uri( $q ); |
85 |
|
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
86 |
|
} else { |
87 |
|
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
88 |
|
} |
89 |
|
|
90 |
|
my $ua = LWP::UserAgent->new; |
91 |
|
$ua->default_header( 'Cookie' => $feed->cookie ); |
92 |
|
my $r = $ua->get( $uri ); |
93 |
|
die $feed->title . " returned " . $r->status_line . " for $uri\n" unless ( $r->is_success ); |
94 |
|
|
95 |
my $xml_feed = XML::Feed->parse( URI->new( $feed->uri ) ) |
my $content = $r->content; |
96 |
or die XML::Feed->errstr; |
|
97 |
|
die "No content returned from $uri" unless length( $content ) > 1; |
98 |
|
|
99 |
|
my $xml_feed = XML::Feed->parse( \$content ) |
100 |
|
or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n"; |
101 |
|
|
102 |
|
warn "getting entries from ", $xml_feed->title, "\n"; |
103 |
|
|
104 |
|
my $items = Grep::Model::ItemCollection->new(); |
105 |
|
|
|
warn "fetching ", $xml_feed->title, "\n"; |
|
106 |
for my $entry ($xml_feed->entries) { |
for my $entry ($xml_feed->entries) { |
107 |
my $i = Grep::Model::Item->new(); |
my $i = Grep::Model::Item->new(); |
108 |
|
|
110 |
in_feed => $feed, |
in_feed => $feed, |
111 |
title => $entry->title, |
title => $entry->title, |
112 |
link => $entry->link, |
link => $entry->link, |
113 |
content => $entry->content, |
content => $entry->content->body, |
114 |
summary => $entry->summary, |
summary => $entry->summary->body, |
115 |
category => $entry->category, |
category => $entry->category, |
116 |
author => $entry->author, |
author => $entry->author, |
117 |
issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef, |
issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S") : undef, |
118 |
modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef, |
modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S") : undef, |
119 |
); |
); |
120 |
|
|
121 |
|
if ( $i->id ) { |
122 |
|
$items->add_record( $i ); |
123 |
|
|
124 |
|
Jifty->log->debug("added ", $i->id, " to collection"); |
125 |
|
} else { |
126 |
|
warn "can't add entry ", dump( $entry ) unless ( $i->id ); |
127 |
|
} |
128 |
|
|
129 |
} |
} |
130 |
|
|
131 |
$self->result->message('Success') if not $self->result->failure; |
if ( $items ) { |
132 |
|
|
133 |
return 1; |
$self->result->message( $q . ' => ' . $xml_feed->entries . ' results' ); |
134 |
|
|
135 |
|
if ( $publish ) { |
136 |
|
Grep::Event::Result->new({ q => $q, coll => $items, item_fragment => 'title' })->publish; |
137 |
|
Jifty->log->debug( $items->count, " for '$q' published" ); |
138 |
|
} else { |
139 |
|
$self->result->content( items => $items ); |
140 |
|
Jifty->log->debug( $items->count, " for '$q' found" ); |
141 |
|
} |
142 |
|
|
143 |
|
} else { |
144 |
|
|
145 |
|
$self->result->error( 'No results found' ); |
146 |
|
|
147 |
|
} |
148 |
|
|
149 |
|
return $items; |
150 |
} |
} |
151 |
|
|
152 |
1; |
1; |