Changeset 7
- Timestamp:
- 10/06/06 08:27:12
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plagger/trunk/my-plugins/Filter-FetchEnclosure-Mplayer/Mplayer.pm
r1 r7 2 2 use strict; 3 3 use base qw(Plagger::Plugin::Filter::FetchEnclosure); 4 use File::Path; 4 5 5 6 sub register { … … 7 8 $context->register_hook( 8 9 $self, 9 'update.entry.fixup' => \& enqueue,10 'update.entry.fixup' => \&fetch, 10 11 ); 11 12 } 12 13 13 sub enqueue { 14 sub init { 15 my $self = shift; 16 $self->SUPER::init(@_); 17 18 $self->{ua} = Plagger::UserAgent->new; 19 } 20 21 sub fetch { 14 22 my($self, $context, $args) = @_; 15 23 16 24 for my $enclosure ($args->{entry}->enclosures) { 25 17 26 my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe); 18 27 unless (-e $feed_dir && -d _) { … … 21 30 } 22 31 23 my $digest = File::Spec->catfile($feed_dir, $args->{entry}->digest); 24 my $path = "$digest.wav"; 25 my $extpath = "$digest." . $self->conf->{fetchfor} if $self->conf->{fetchfor}; 32 my $path_base = File::Spec->catfile($feed_dir, $args->{entry}->digest); 33 my $path = "$path_base.wav"; 34 my $fetched = $self->conf->{consider_fetched} || 'mp3'; 35 my $extpath = "$path_base." . $fetched; 26 36 27 37 if (-e $extpath || -e $path) { … … 34 44 } 35 45 36 $context->log(info => "fetch " . $enclosure->url . " to " . $path); 46 my $new_enclosure_url = $self->rewrite_enclosure_url($enclosure->url); 47 48 $context->log(info => "fetch " . $new_enclosure_url . " to " . $path); 37 49 38 50 my @options = split(/ /,$self->conf->{option}) or (); 39 my @command = ('mplayer', $ enclosure->url,51 my @command = ('mplayer', $new_enclosure_url, 40 52 '-vc', 'dummy', 41 53 '-vo', 'null', … … 48 60 if (!$? && -s $path ) { 49 61 my $length = -s _; 62 $enclosure->url($new_enclosure_url); 50 63 $enclosure->length($length); 51 64 $enclosure->type('audio/x-wav'); … … 55 68 $context->log(error => "Error dumping to $path"); 56 69 $context->log(info => "Deleting $path"); 57 unlink $path or $ self->log(error => "Cannot delete $path. ");70 unlink $path or $context->log(error => "Cannot delete $path. "); 58 71 } 59 72 } 73 } 74 75 sub rewrite_enclosure_url { 76 my ($self, $url) = @_; 77 78 $url =~ m!.*\.(.+)!; 79 my $ext = $1; 80 Plagger->context->log(error => "Cannot determine file type.") unless $ext; 81 82 my $new_url; 83 # TODO support multi stream addresses 84 if ($ext =~ /^asx$/) { 85 my $res = $self->{ua}->fetch($url) 86 or Plagger->context->log; 87 $res->content =~ m!((?:http|mms)://.*?\.(?:wsx|wma|asf))!; 88 $new_url = $1; 89 } 90 elsif ($ext =~ /^ram$/) { 91 my $res = $self->{ua}->fetch($url) 92 or Plagger->context->log; 93 $res->content =~ m!(rtsp://.*?\.(?:rm|smi))!; 94 $new_url = $1; 95 } 96 97 Plagger->context->log(debug => "Rewrite url from $url to $new_url"); 98 99 return $new_url or $url; 60 100 } 61 101
