| 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__ |