Changeset 8

Show
Ignore:
Timestamp:
10/13/06 04:10:47
Author:
yohei
Message:

Find a stream url from a container, and make some changes to use this with
FindEnclosureURL

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plagger/trunk/my-plugins/Filter-FetchEnclosure-Mplayer/Mplayer.pm

    r7 r8  
    2222    my($self, $context, $args) = @_; 
    2323 
     24    unless (defined $self->conf->{type}) { 
     25        $context->log(error => q{config 'type' is not set.}); 
     26    } 
     27 
    2428    for my $enclosure ($args->{entry}->enclosures) { 
     29 
     30        unless ($enclosure->type =~ $self->conf->{type}) { 
     31            $enclosure->local_path('dummy') unless $enclosure->local_path; 
     32            next; 
     33        } 
    2534         
    26         my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe); 
     35        my $feed_dir 
     36            = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe); 
    2737        unless (-e $feed_dir && -d _) { 
    2838            $context->log(info => "mkdir $feed_dir"); 
     
    3040        } 
    3141 
    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; 
    36  
    37         if (-e $extpath || -e $path) { 
     42        my $path_base   = File::Spec->catfile($feed_dir, $args->{entry}->digest); 
     43        my $output_path = "$path_base.wav"; 
     44         
     45        # Check existence of fetched data 
     46        my $fetched_extension = $self->conf->{consider_fetched} || 'mp3'; 
     47        my $fetched_data_path = "$path_base.$fetched_extension"; 
     48        if (-e $fetched_data_path || -e $output_path) { 
    3849            my $length = -s _; 
    3950            $enclosure->length($length); 
    4051            $enclosure->type('audio/x-wav'); 
    41             $enclosure->local_path($path); 
    42             $context->log(debug => $enclosure->url . "is already stored in $path"); 
     52            $enclosure->local_path($output_path); 
     53            $context->log(debug => $enclosure->url . "is already stored in $output_path"); 
    4354            next; 
    4455        } 
    4556 
    46         my $new_enclosure_url = $self->rewrite_enclosure_url($enclosure->url); 
     57        my $stream_url = $self->_get_stream_url_for($enclosure); 
    4758 
    48         $context->log(info => "fetch " . $new_enclosure_url . " to " . $path); 
     59        $context->log(info => "fetch $stream_url to $output_path"); 
    4960 
    5061        my @options = split(/ /,$self->conf->{option}) or (); 
    51         my @command = ('mplayer', $new_enclosure_url,  
    52                     '-vc', 'dummy',  
    53                     '-vo', 'null',  
    54                     '-ao', "pcm:file=$path",  
    55                     '-quiet',  
    56                     @options,); 
     62        my @command = ( 
     63            'mplayer', 
     64            $stream_url,  
     65            '-vc', 'dummy',  
     66            '-vo', 'null',  
     67            '-ao', "pcm:file=$output_path",  
     68            '-quiet',  
     69            @options, 
     70        ); 
    5771 
    5872        system (@command); 
    5973 
    60         if (!$? && -s $path ) { 
     74        if (!$? && -s $output_path ) { 
    6175            my $length = -s _; 
    62             $enclosure->url($new_enclosure_url); 
     76            $enclosure->url($stream_url); 
    6377            $enclosure->length($length); 
    6478            $enclosure->type('audio/x-wav'); 
    65             $enclosure->local_path($path); 
    66             $context->log(info => "Dumping to $path is done [$length]"); 
     79            $enclosure->local_path($output_path); 
     80            $context->log(info => "Dumping to $output_path is done [$length]"); 
    6781        } else { 
    68             $context->log(error => "Error dumping to $path"); 
    69             $context->log(info => "Deleting $path"); 
    70             unlink $path or $context->log(error => "Cannot delete $path. "); 
     82            $context->log(warn => "Can not dump to $output_path"); 
     83            $context->log(info => "Deleting $output_path"); 
     84            unlink $output_path or $context->log(warn => "Can not delete $output_path."); 
    7185        } 
    7286    } 
    7387} 
    7488 
    75 sub rewrite_enclosure_url
    76     my ($self, $url) = @_; 
     89sub _get_stream_url_for
     90    my ($self, $enclosure) = @_; 
    7791 
    78     $url =~ m!.*\.(.+)!; 
    79     my $ext = $1; 
    80     Plagger->context->log(error => "Cannot determine file type.") unless $ext; 
    81  
    82     my $new_url; 
     92    my $stream_url; 
     93     
    8394    # TODO support multi stream addresses 
    84     if ($ext =~ /^asx$/) { 
    85         my $res = $self->{ua}->fetch($url) 
     95    if ($enclosure->type =~ /asf/) { 
     96        my $res = $self->{ua}->fetch($enclosure->url) 
    8697            or Plagger->context->log; 
    8798        $res->content =~ m!((?:http|mms)://.*?\.(?:wsx|wma|asf))!; 
    88         $new_url = $1; 
     99        $stream_url = $1; 
    89100    } 
    90     elsif ($ext =~ /^ram$/) { 
    91         my $res = $self->{ua}->fetch($url) 
     101    elsif ($enclosure->type =~ /realaudio/) { 
     102        my $res = $self->{ua}->fetch($enclosure->url) 
    92103            or Plagger->context->log; 
    93104        $res->content =~ m!(rtsp://.*?\.(?:rm|smi))!; 
    94         $new_url = $1; 
     105        $stream_url = $1; 
    95106    } 
    96107 
    97     Plagger->context->log(debug => "Rewrite url from $url to $new_url"); 
     108    Plagger->context->log(warn => "Cannot get stream url for " .  $enclosure->url) 
     109        unless $stream_url; 
    98110 
    99     return $new_url or $url; 
     111    return $stream_url; 
    100112} 
    101  
    102113 
    1031141;