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 |
}; |
}; |
49 |
|
|
50 |
=head2 take_action |
=head2 take_action |
73 |
|
|
74 |
my $uri = $feed->uri; |
my $uri = $feed->uri; |
75 |
if ($uri =~ m/%s/) { |
if ($uri =~ m/%s/) { |
76 |
$uri = sprintf( $uri, $self->argument_value('q') ); |
$uri = $feed->search_uri( $self->argument_value('q') ); |
77 |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
78 |
} else { |
} else { |
79 |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
80 |
} |
} |
81 |
|
|
82 |
my $xml_feed = XML::Feed->parse( URI->new($uri) ) |
my $ua = LWP::UserAgent->new; |
83 |
or die XML::Feed->errstr; |
$ua->default_header( 'Cookie' => $feed->cookie ); |
84 |
|
my $r = $ua->get( $uri ); |
85 |
|
die $feed->title . " returned " . $r->status_line . " for $uri\n" unless ( $r->is_success ); |
86 |
|
|
87 |
|
my $content = $r->content; |
88 |
|
|
89 |
|
die "No content returned from $uri" unless length( $content ) > 1; |
90 |
|
|
91 |
warn "fetching ", $xml_feed->title, "\n"; |
my $xml_feed = XML::Feed->parse( \$content ) |
92 |
|
or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n"; |
93 |
|
|
94 |
my @results; |
warn "getting entries from ", $xml_feed->title, "\n"; |
95 |
|
|
96 |
|
my @items; |
97 |
|
|
98 |
for my $entry ($xml_feed->entries) { |
for my $entry ($xml_feed->entries) { |
99 |
my $i = Grep::Model::Item->new(); |
my $i = Grep::Model::Item->new(); |
110 |
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 %z") : undef, |
111 |
); |
); |
112 |
|
|
113 |
die "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
if ( $i->id ) { |
114 |
|
push @items, $i; |
115 |
|
|
116 |
|
# Grep::Event::Result->new( $i )->publish; |
117 |
|
|
118 |
push @results, $i->id; |
Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) ); |
119 |
|
} else { |
120 |
|
warn "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
121 |
|
} |
122 |
|
|
|
Jifty->log->debug("item ", $i->id, " = ",dump( $entry ) ); |
|
123 |
} |
} |
124 |
|
|
125 |
if ( @results ) { |
if ( @items ) { |
126 |
|
|
127 |
$self->result->message( $self->argument_value('q') . ' => ' . |
$self->result->message( $self->argument_value('q') . ' => ' . |
128 |
$xml_feed->entries . ' items: ' . join(",", @results) |
$xml_feed->entries . ' items: ' . join(",", map { $_->id } @items) |
129 |
); |
); |
130 |
|
|
131 |
$self->result->content( results => \@results ); |
$self->result->content( items => \@items ); |
132 |
return 1; |
return 1; |
133 |
|
|
134 |
} else { |
} else { |
135 |
|
|
136 |
$self->result->message( 'No results for ' . $self->argument_value('q') ); |
$self->result->error( 'No results found' ); |
137 |
return 0; |
return 0; |
138 |
|
|
139 |
} |
} |
140 |
} |
} |
141 |
|
|