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