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 |
|
|
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"); |
81 |
|
|
82 |
my $uri = $feed->uri; |
my $uri = $feed->uri; |
83 |
if ($uri =~ m/%s/) { |
if ($uri =~ m/%s/) { |
84 |
$uri = $feed->search_uri( $self->argument_value('q') ); |
$uri = $feed->search_uri( $q ); |
85 |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
86 |
} else { |
} else { |
87 |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
88 |
} |
} |
89 |
|
|
90 |
my $xml_feed = XML::Feed->parse( URI->new($uri) ) |
my $items = Grep::Model::ItemCollection->new(); |
91 |
or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n"; |
|
92 |
|
sub abort { |
93 |
|
my $message = join(" ", @_); |
94 |
|
if ( $publish ) { |
95 |
|
Grep::Event::Result->new({ |
96 |
|
q => $q, coll => $items, |
97 |
|
message => $message, class => 'error', |
98 |
|
})->publish; |
99 |
|
return; |
100 |
|
} else { |
101 |
|
$self->result->error( $message ); |
102 |
|
return; |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
|
my $ua = LWP::UserAgent->new; |
107 |
|
$ua->default_header( 'Cookie' => $feed->cookie ); |
108 |
|
my $r = $ua->get( $uri ); |
109 |
|
return abort( |
110 |
|
$feed->title . " returned " . $r->status_line . " for $uri\n" |
111 |
|
) unless ( $r->is_success ); |
112 |
|
|
113 |
|
my $content = $r->content; |
114 |
|
|
115 |
|
return abort( "No content returned from $uri" ) unless length( $content ) > 1; |
116 |
|
|
|
warn "fetching ", $xml_feed->title, "\n"; |
|
117 |
|
|
118 |
my @items; |
my $xml_feed = XML::Feed->parse( \$content ) |
119 |
|
or return abort( $feed->title, " returned ", XML::Feed->errstr, " for $uri" ); |
120 |
|
|
121 |
|
warn "getting entries from ", $xml_feed->title, "\n"; |
122 |
|
|
123 |
for my $entry ($xml_feed->entries) { |
for my $entry ($xml_feed->entries) { |
124 |
my $i = Grep::Model::Item->new(); |
my $i = Grep::Model::Item->new(); |
131 |
summary => $entry->summary->body, |
summary => $entry->summary->body, |
132 |
category => $entry->category, |
category => $entry->category, |
133 |
author => $entry->author, |
author => $entry->author, |
134 |
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, |
135 |
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, |
136 |
); |
); |
137 |
|
|
138 |
die "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
if ( $i->id ) { |
139 |
|
$items->add_record( $i ); |
|
push @items, $i; |
|
140 |
|
|
141 |
# Grep::Event::Result->new( $i )->publish; |
Jifty->log->debug("added ", $i->id, " to collection"); |
142 |
|
} else { |
143 |
|
warn "can't add entry ", dump( $entry ) unless ( $i->id ); |
144 |
|
} |
145 |
|
|
|
Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) ); |
|
146 |
} |
} |
147 |
|
|
148 |
if ( @items ) { |
if ( my $count = $items->count ) { |
149 |
|
|
150 |
$self->result->message( $self->argument_value('q') . ' => ' . |
my $message = "$count results for '$q' in " . $feed->title; |
|
$xml_feed->entries . ' items: ' . join(",", map { $_->id } @items) |
|
|
); |
|
151 |
|
|
152 |
$self->result->content( items => \@items ); |
$self->result->message( $message ); |
|
return 1; |
|
|
|
|
|
} else { |
|
153 |
|
|
154 |
$self->result->message( 'No results found' ); |
if ( $publish ) { |
155 |
|
Grep::Event::Result->new({ |
156 |
|
q => $q, coll => $items, |
157 |
|
item_fragment => $self->argument_value('item_fragment'), |
158 |
|
message => $message, class => 'message', |
159 |
|
})->publish; |
160 |
|
Jifty->log->debug( $items->count, " for '$q' published" ); |
161 |
|
} else { |
162 |
|
$self->result->content( items => $items ); |
163 |
|
Jifty->log->debug( $items->count, " for '$q' found" ); |
164 |
|
} |
165 |
|
|
166 |
# with default sticky_on_failure, this will keep form data |
} else { |
167 |
$self->result->failure( 1 ); |
return abort( "No results for '$q' in " . $feed->title ); |
|
|
|
|
return 0; |
|
168 |
} |
} |
169 |
|
|
170 |
|
return $items; |
171 |
} |
} |
172 |
|
|
173 |
1; |
1; |