| 1 | package Plagger::Plugin::Notify::Audio; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use MP3::Info; | ||
| 6 | |||
| 7 | sub init { | ||
| 8 | my $self = shift; | ||
| 9 | $self->SUPER::init(@_); | ||
| 10 | |||
| 11 | my $player = $self->conf->{player} || $^O; | ||
| 12 | my $class = 'Plagger::Plugin::Notify::Audio::' . $player; | ||
| 13 | eval "require $class;"; | ||
| 14 | if ($@) { | ||
| 15 | Plagger->context->error("Notify plugin doesn't run on your platform $player: $@"); | ||
| 16 | } | ||
| 17 | bless $self, $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub register { | ||
| 21 | my($self, $context) = @_; | ||
| 22 | $context->register_hook( | ||
| 23 | $self, | ||
| 24 | 'publish.entry' => \&update, | ||
| 25 | 'publish.finalize' => \&finalize, | ||
| 26 | ); | ||
| 27 | $self->{enclosures} = [ ]; | ||
| 28 | $self->{count} = 0; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub update { | ||
| 32 | my($self, $context, $args) = @_; | ||
| 33 | |||
| 34 | if ($self->conf->{play_enclosures}) { | ||
| 35 | push @{$self->{enclosures}}, grep $_->local_path, $args->{entry}->enclosures; | ||
| 36 | } else { | ||
| 37 | $self->{count}++; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | sub finalize { | ||
| 42 | my($self, $context, $args) = @_; | ||
| 43 | |||
| 44 | if ($self->{count}) { | ||
| 45 | $self->log(info => "Play " . $self->conf->{filename}); | ||
| 46 | return $self->play($self->conf->{filename}); | ||
| 47 | } | ||
| 48 | |||
| 49 | for my $enclosure (@{$self->{enclosures}}) { | ||
| 50 | # XXX this should be a separate plugin to handle MP4/WAV/ogg as well! | ||
| 51 | my $info = eval { MP3::Info->new($enclosure->local_path) }; | ||
| 52 | my $length = $info ? $info->secs : undef; | ||
| 53 | $self->log(info => "Play " . $enclosure->local_path . ($length ? " for $length seconds" : "")); | ||
| 54 | $self->play($enclosure->local_path, $length); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub play { | ||
| 59 | my($self, $filename) = @_; | ||
| 60 | $self->log(warn => "Subclass should override this"); | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 | __END__ |