Changeset 7

Show
Ignore:
Timestamp:
10/06/06 08:27:12
Author:
yohei
Message:

Initial change for rewriting enclosure url

Files:

Legend:

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

    r1 r7  
    22use strict; 
    33use base qw(Plagger::Plugin::Filter::FetchEnclosure); 
     4use File::Path; 
    45 
    56sub register { 
     
    78    $context->register_hook( 
    89        $self, 
    9         'update.entry.fixup' => \&enqueue
     10        'update.entry.fixup' => \&fetch
    1011    ); 
    1112} 
    1213 
    13 sub enqueue { 
     14sub init { 
     15    my $self = shift; 
     16    $self->SUPER::init(@_); 
     17 
     18    $self->{ua} = Plagger::UserAgent->new; 
     19
     20 
     21sub fetch { 
    1422    my($self, $context, $args) = @_; 
    1523 
    1624    for my $enclosure ($args->{entry}->enclosures) { 
     25         
    1726        my $feed_dir = File::Spec->catfile($self->conf->{dir}, $args->{feed}->id_safe); 
    1827        unless (-e $feed_dir && -d _) { 
     
    2130        } 
    2231 
    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; 
    2636 
    2737        if (-e $extpath || -e $path) { 
     
    3444        } 
    3545 
    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); 
    3749 
    3850        my @options = split(/ /,$self->conf->{option}) or (); 
    39         my @command = ('mplayer', $enclosure->url,  
     51        my @command = ('mplayer', $new_enclosure_url,  
    4052                    '-vc', 'dummy',  
    4153                    '-vo', 'null',  
     
    4860        if (!$? && -s $path ) { 
    4961            my $length = -s _; 
     62            $enclosure->url($new_enclosure_url); 
    5063            $enclosure->length($length); 
    5164            $enclosure->type('audio/x-wav'); 
     
    5568            $context->log(error => "Error dumping to $path"); 
    5669            $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. "); 
    5871        } 
    5972    } 
     73} 
     74 
     75sub 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; 
    60100} 
    61101