Changeset 262
- Timestamp:
- 09/12/06 23:00:56 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plagger/trunk/lib/Plagger/Plugin/CustomFeed/DAAP.pm
r261 r262 24 24 my($self, $context, $args) = @_; 25 25 26 $self->{songs} = {};27 $self->{daap_entry} = {};28 29 26 $self->{feed} = Plagger::Feed->new; 30 27 $self->{feed}->type('DAAP'); … … 34 31 $res->discover; 35 32 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); 63 36 } 64 37 … … 66 39 } 67 40 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 86 41 sub seek_songs { 87 my ( $self, $ daap_entry ) = @_;42 my ( $self, $bonjour_entry ) = @_; 88 43 89 44 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, 92 47 ); 93 48 94 49 $daap->connect or return; 95 50 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 77 sub 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); 99 96 } 100 97 101 98 sub match { 102 my($self, $song, $ query) = @_;99 my($self, $song, $keyword) = @_; 103 100 104 101 my $search = join " ", $song->{"daap.songartist"}, $song->{"dmap.itemname"}, $song->{"daap.songalbum"}; 105 $search =~ /$ query/i;102 $search =~ /$keyword/i; 106 103 } 107 104 plagger/trunk/lib/Plagger/Plugin/Filter/FetchDAAP.pm
r261 r262 3 3 use base qw( Plagger::Plugin ); 4 4 5 use Encode; 5 6 use Net::DAAP::Client; 6 7 … … 23 24 24 25 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'; 29 28 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://!; 31 32 32 open my $out, '>', $path; 33 $| = 1; 34 binmode $out; 33 my ( $host, $port, $id ) = ( $url =~ m!^daap://([^/:]+):(\d+)/database/\d+/items/(\d+)\..+$! ); 35 34 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 } 43 63 } 44 64
