Plagger/Plugin/CustomFeed/Script.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 72 CC 5 SCAR 20.4
CC
5
Cov
0%
aggregate
20.0
handle
6.0
register
2.0
CRAP 7.7
1 package Plagger::Plugin::CustomFeed::Script;
2 use strict;
3 use base qw( Plagger::Plugin::Aggregator::Simple );
4  
5 use URI;
6 use URI::Escape;
7 use YAML::Syck;
8  
9 use Plagger::Plugin::Aggregator::Simple;
10 use Plagger::Plugin::CustomFeed::Debug;
11  
12 sub register {
13 my($self, $context) = @_;
14 $context->register_hook(
15 $self,
16 'customfeed.handle' => \&handle,
17 );
18 }
19  
20 sub handle {
21 my($self, $context, $args) = @_;
22  
23 if (URI->new($args->{feed}->url)->scheme eq 'script') {
24 $self->aggregate($context, $args);
25 return 1;
26 }
27  
28 return;
29 }
30  
31 sub aggregate {
32 my($self, $context, $args) = @_;
33  
34 my $script = URI->new($args->{feed}->url)->opaque;
35 $script =~ s!^//!!;
36 $script = URI::Escape::uri_unescape($script); # to support script://python.exe foo.py
37  
38 $context->log(debug => "Executing '$script'");
39 my $output = qx($script);
40 if ($?) {
41 $context->log(error => "Error happend while executing '$script': $?");
42 return;
43 }
44  
45 # TODO: check BOM?
46 if ($output =~ /^<\?xml/) {
47 $context->log(debug => "Looks like output is RSS/Atom");
48 $self->SUPER::handle_feed($args->{feed}->url, \$output, $args->{feed});
49 } else {
50 eval {
51 local $YAML::Syck::ImplicitUnicode = 1;
52 my $feed = YAML::Syck::Load($output);
53 $context->log(debug => "Looks like output is YAML");
54 local $self->{conf} = $feed;
55 $self->Plagger::Plugin::CustomFeed::Debug::aggregate($context, $args);
56 };
57 if ($@) {
58 $context->log(error => "Failed to parse as YAML. Can't determine output format of $script");
59 return;
60 }
61 }
62  
63 return 1;
64 }
65  
66 1;
67 __END__