Changeset 262

Show
Ignore:
Timestamp:
09/12/06 23:00:56 (2 years ago)
Author:
miya
Message:

CustomFeed::DAAP: support enclosure.
Filter::FetchDAAP: support enclosure.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plagger/trunk/lib/Plagger/Plugin/CustomFeed/DAAP.pm

    r261 r262  
    2424    my($self, $context, $args) = @_; 
    2525 
    26     $self->{songs} = {}; 
    27     $self->{daap_entry} = {}; 
    28  
    2926    $self->{feed} = Plagger::Feed->new; 
    3027    $self->{feed}->type('DAAP'); 
     
    3431    $res->discover; 
    3532 
    36     for my $daap_entry ( $res->entries ){ 
    37         $context->log( 'info' => 'seeking ' . $daap_entry->name ); 
    38         $self->seek_songs($daap_entry); 
    39     } 
    40  
    41     my $keywords = $self->conf->{keywords}; 
    42     for my $id ( keys %{ $self->{songs} } ) { 
    43       SONGS: 
    44         for my $song ( values %{ $self->{songs}->{$id} } ) { 
    45             if ( ref $keywords eq 'ARRAY' ) { 
    46                 for my $keyword ( @{ $keywords } ){ 
    47                     if ( $self->match($song, $keyword) ){ 
    48                         $self->add_entry($song); 
    49                         next SONGS; 
    50                     } 
    51                 } 
    52             } 
    53             elsif ( $keywords ) { 
    54                 if ( $self->match($song, $keywords) ){ 
    55                     $self->add_entry($song); 
    56                     next SONGS; 
    57                 } 
    58             } 
    59             else { 
    60                 $self->add_entry($song); 
    61             } 
    62         } 
     33    for my $bonjour_entry ( $res->entries ){ 
     34        $context->log( 'info' => 'seeking ' . $bonjour_entry->name ); 
     35        $self->seek_songs($bonjour_entry); 
    6336    } 
    6437 
     
    6639} 
    6740 
    68 sub add_entry { 
    69     my ( $self, $song ) = @_; 
    70  
    71     my $entry = Plagger::Entry->new; 
    72     $entry->title($song->{'dmap.itemname'}); 
    73     $entry->meta->{daap_song} = $song; 
    74     for my $entry_id ( keys %{ $self->{songs} } ) { 
    75         for my $s ( values %{ $self->{songs}->{$entry_id} } ) { 
    76             if ( $s->{'dmap.persistentid'} eq $song->{'dmap.persistentid'} ) { 
    77                 $entry->meta->{daap_entry} = $self->{daap_entry}->{$entry_id}; 
    78                 last; 
    79             } 
    80         } 
    81     } 
    82  
    83     $self->{feed}->add_entry($entry); 
    84 } 
    85  
    8641sub seek_songs { 
    87     my ( $self, $daap_entry ) = @_; 
     42    my ( $self, $bonjour_entry ) = @_; 
    8843 
    8944    my $daap = Net::DAAP::Client->new( 
    90         SERVER_HOST => $daap_entry->address, 
    91         SERVER_PORT => $daap_entry->port, 
     45        SERVER_HOST => $bonjour_entry->address, 
     46        SERVER_PORT => $bonjour_entry->port, 
    9247    ); 
    9348 
    9449    $daap->connect or return; 
    9550 
    96     my $id = join "-", $daap_entry->address, $daap_entry->port; 
    97     $self->{songs}->{$id} = $daap->songs; 
    98     $self->{daap_entry}->{$id} = $daap_entry; 
     51    $self->{bonjour_entry} = $bonjour_entry; 
     52    $self->{daap} = $daap; 
     53 
     54    my $keywords = $self->conf->{keywords}; 
     55 
     56  SONGS: 
     57    for my $song ( values %{$daap->songs} ){ 
     58        if ( ref $keywords eq 'ARRAY' ) { 
     59            for my $keyword ( @{ $keywords } ){ 
     60                if ( $self->match($song, $keyword) ){ 
     61                    $self->add_entry($song); 
     62                    next SONGS; 
     63                } 
     64            } 
     65        } 
     66        elsif ( $keywords ) { 
     67            if ( $self->match($song, $keywords) ){ 
     68                $self->add_entry($song); 
     69            } 
     70        } 
     71        else { 
     72            $self->add_entry($song); 
     73        } 
     74    } 
     75
     76 
     77sub add_entry { 
     78    my ( $self, $song ) = @_; 
     79 
     80    my $address = $self->{bonjour_entry}->address; 
     81    my $port    = $self->{bonjour_entry}->port; 
     82    my $daap    = $self->{daap}; 
     83 
     84    my $entry = Plagger::Entry->new; 
     85    $entry->title($song->{'dmap.itemname'}); 
     86 
     87    my $enclosure = Plagger::Enclosure->new; 
     88    my $filename = "$song->{'dmap.itemid'}.$song->{'daap.songformat'}"; 
     89    $enclosure->filename($filename); 
     90    $enclosure->length($song->{'daap.songsize'}); 
     91    $enclosure->url("daap://$address:$port/database/" . $daap->db . "/items/$filename"); 
     92    $enclosure->auto_set_type; 
     93 
     94    $entry->add_enclosure($enclosure); 
     95    $self->{feed}->add_entry($entry); 
    9996} 
    10097 
    10198sub match { 
    102     my($self, $song, $query) = @_; 
     99    my($self, $song, $keyword) = @_; 
    103100 
    104101    my $search = join " ", $song->{"daap.songartist"}, $song->{"dmap.itemname"}, $song->{"daap.songalbum"}; 
    105     $search =~ /$query/i; 
     102    $search =~ /$keyword/i; 
    106103} 
    107104 
  • plagger/trunk/lib/Plagger/Plugin/Filter/FetchDAAP.pm

    r261 r262  
    33use base qw( Plagger::Plugin ); 
    44 
     5use Encode; 
    56use Net::DAAP::Client; 
    67 
     
    2324 
    2425    my $entry      = $args->{entry}; 
    25     my $song       = $entry->meta->{daap_song}; 
    26     my $daap_entry = $entry->meta->{daap_entry}; 
    27     my $filename   = Plagger::Util::filename_for($entry, $self->conf->{filename}); 
    28     my $path       = File::Spec->catfile($feed_dir, $filename); 
     26    my $enclosures = $entry->enclosures; 
     27    my $encoding   = $self->conf->{encoding} || 'cp932'; 
    2928 
    30     $context->log(info => "fetch " . $song->{'dmap.itemname'} . " to " . $path); 
     29    for my $enclosure ( @$enclosures ){ 
     30        my $url = $enclosure->url->as_string; 
     31        return if $url !~ m!^daap://!; 
    3132 
    32     open my $out, '>', $path; 
    33     $| = 1; 
    34     binmode $out; 
     33        my ( $host, $port, $id ) = ( $url =~ m!^daap://([^/:]+):(\d+)/database/\d+/items/(\d+)\..+$! ); 
    3534 
    36     my $daap = Net::DAAP::Client->new( 
    37         SERVER_HOST => $daap_entry->address, 
    38         SERVER_PORT => $daap_entry->port, 
    39     ); 
    40     $daap->connect; 
    41     print $out $daap->get($song->{'dmap.itemid'}); 
    42     close($out); 
     35        my $daap = Net::DAAP::Client->new( 
     36            SERVER_HOST => $host, 
     37            SERVER_PORT => $port, 
     38        ); 
     39        $daap->connect or do { 
     40            $context->log( error => "could not connect to $url" ); 
     41            return; 
     42        }; 
     43 
     44        my $filename = $enclosure->filename; 
     45        if ( $self->conf->{filename} ){ 
     46            $filename   = Plagger::Util::filename_for($entry, $self->conf->{filename}); 
     47        } 
     48        $filename = encode($encoding, $filename); 
     49 
     50        my $path = File::Spec->catfile($feed_dir, $filename); 
     51        $context->log(info => 'fetch ' . $entry->title . ' to ' . $path); 
     52        open my $out, '>', $path; 
     53        $| = 1; 
     54        binmode $out; 
     55 
     56        $daap->connect; 
     57        print $out $daap->get($id); 
     58        close($out); 
     59 
     60        $enclosure->local_path($path); 
     61        $enclosure->filename($filename); 
     62    } 
    4363} 
    4464