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 |
|
|
66 |
|
|
67 |
my $uri = $feed->uri; |
my $uri = $feed->uri; |
68 |
if ($uri =~ m/%s/) { |
if ($uri =~ m/%s/) { |
69 |
$uri = sprintf( $uri, $self->argument_value('q') ); |
$uri = $feed->search_uri( $self->argument_value('q') ); |
70 |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
Jifty->log->info("Searching ", $feed->title, " at $uri"); |
71 |
} else { |
} else { |
72 |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
Jifty->log->info("Fetching ", $feed->title, " at $uri"); |
73 |
} |
} |
74 |
|
|
75 |
my $xml_feed = XML::Feed->parse( URI->new($uri) ) |
my $ua = LWP::UserAgent->new; |
76 |
or die XML::Feed->errstr; |
$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 |
warn "fetching ", $xml_feed->title, "\n"; |
my $content = $r->content; |
81 |
|
|
82 |
my @results; |
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) { |
for my $entry ($xml_feed->entries) { |
92 |
my $i = Grep::Model::Item->new(); |
my $i = Grep::Model::Item->new(); |
105 |
|
|
106 |
die "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
die "can't create item from entry ", dump( $entry ) unless ( $i->id ); |
107 |
|
|
108 |
push @results, $i->id; |
push @items, $i; |
109 |
|
|
110 |
|
# Grep::Event::Result->new( $i )->publish; |
111 |
|
|
112 |
Jifty->log->debug("item ", $i->id, " = ",dump( $entry ) ); |
Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) ); |
113 |
} |
} |
114 |
|
|
115 |
if ( @results ) { |
if ( @items ) { |
116 |
|
|
117 |
$self->result->message( $self->argument_value('q') . ' => ' . |
$self->result->message( $self->argument_value('q') . ' => ' . |
118 |
$xml_feed->entries . ' items: ' . join(",", @results) |
$xml_feed->entries . ' items: ' . join(",", map { $_->id } @items) |
119 |
); |
); |
120 |
|
|
121 |
$self->result->content( results => \@results ); |
$self->result->content( items => \@items ); |
122 |
return 1; |
return 1; |
123 |
|
|
124 |
} else { |
} else { |
125 |
|
|
126 |
$self->result->message( 'No results found' ); |
$self->result->error( 'No results found' ); |
|
|
|
|
# with default sticky_on_failure, this will keep form data |
|
|
$self->result->failure( 1 ); |
|
|
|
|
127 |
return 0; |
return 0; |
128 |
|
|
129 |
} |
} |
130 |
} |
} |
131 |
|
|