--- gnupod_search.pl 2011-03-21 14:40:34.390287810 +0000 +++ gnupod_search.pl.patched 2011-03-21 14:45:43.205914391 +0000 @@ -27,6 +27,9 @@ use GNUpod::FooBar; use GNUpod::ArtworkDB; use Getopt::Long; +use File::Copy; +use File::Basename; +use Cwd; use vars qw(%opts @keeplist %rename_tags); @@ -47,7 +50,7 @@ "album|l=s", "title|t=s", "id|i=s", "rename=s@", "artwork=s", "playcount|c=s", "rating|s=s", "podcastrss|R=s", "podcastguid|U=s", "bitrate|b=s", - "view=s","genre|g=s", "match-once|o", "delete"); + "view=s","genre|g=s", "match-once|o", "delete", "copy", "path|p=s"); GNUpod::FooBar::GetConfig(\%opts, {view=>'s', mount=>'s', 'match-once'=>'b', 'automktunes'=>'b', model=>'s', bgcolor=>'s'}, "gnupod_search"); $opts{view} ||= 'ialt'; #Default view @@ -56,6 +59,8 @@ version() if $opts{version}; #Check if input makes sense: die "You can't use --delete and --rename together\n" if($opts{delete} && $opts{rename}); +die "Can only specify --path when using --copy\n" if($opts{path} && !$opts{copy}); +print "Copying all matching files\n" if($opts{copy}); # -> Connect the iPod my $connection = GNUpod::FooBar::connect(\%opts); @@ -104,9 +109,10 @@ sub newfile { my($el) = @_; # 2 = mount + view (both are ALWAYS set) - my $ntm = keys(%opts)-2-(defined $opts{'match-once'})-(defined $opts{'automktunes'})-(defined $opts{'delete'})-(defined $opts{'rename'})-(defined $opts{'artwork'})-(defined $opts{'model'}); + my $ntm = keys(%opts)-2-(defined $opts{'match-once'})-(defined $opts{'automktunes'})-(defined $opts{'delete'})-(defined $opts{'copy'})-(defined $opts{'rename'})-(defined $opts{'artwork'})-(defined $opts{'model'})-(defined $opts{path}); my $matched = 0; my $dounlink = 0; + my $docopy = 0; foreach my $opx (keys(%opts)) { next if $opx =~ /mount|match-once|delete|view|rename|artwork|model/; #Skip this @@ -143,6 +149,9 @@ if($opts{delete}) { $dounlink = 1; # Request deletion } + if($opts{copy}) { + $docopy = 1; # Request copy + } elsif(defined($opts{artwork})) { # -> Add/Set artwork $el->{file}->{has_artwork} = 1; @@ -157,6 +166,57 @@ unlink(GNUpod::XMLhelper::realpath($opts{mount},$el->{file}->{path})) or warn "[!!] Remove failed: $!\n"; $dirty++; } + if($docopy) { + # Note: May need ipod mounted with option 'shortname=lower' for copy to work + my $source = GNUpod::XMLhelper::realpath($opts{mount},$el->{file}->{path}); + + my $artist; + if (defined($el->{file}->{artist})) { + $artist = $el->{file}->{artist}; + } + else { + $artist = "Unknown_Artist"; + } + $artist =~ tr/ /_/; + $artist =~ tr/\/\\/--/; + my $album; + if (defined($el->{file}->{album})) { + $album = $el->{file}->{album}; + } + else { + $album = "Unknown_Album"; + } + $album =~ tr/ /_/; + $album =~ tr/\/\\/--/; + my $songnumber_prefix = ""; + if (defined($el->{file}->{songnum})) { + $songnumber_prefix = sprintf("%02d",$el->{file}->{songnum}) . "_"; + } + my $prefix; + if ($opts{path}) { + $prefix = $opts{path}; + } + else { + # Output to current directory by default + $prefix = cwd(); + } + my $title; + if (defined($el->{file}->{title})) { + $title = $el->{file}->{title}; + } + else { + $title = "Unknown_Album"; + } + $title =~ tr/ /_/; + $title =~ tr/\/\\/--/; + my $targetdir = $prefix . "/" . $artist . "::" . $album . "/"; + my $ext; + (undef,undef,$ext) = fileparse($el->{file}->{path},qr"\..*"); + my $targetfile = $songnumber_prefix . $title . $ext; + my $target = $targetdir . $targetfile; + mkdir $targetdir; + copy($source, $target) or die "File $source cannot be copied to $target."; + } else { # -> Keep file: add it to XML GNUpod::XMLhelper::mkfile($el); @@ -259,6 +319,8 @@ -b, --bitrate=BITRATE search songs by Bitrate -o, --match-once Search doesn't need to match multiple times (eg. -a & -l) --delete REMOVE (!) matched songs + --copy Copy matched songs to local disk + -p, --path=directory Output path to use when copying songs to local disk --view=ialt Modify output, default=ialt t = title a = artist r = rating p = iPod Path l = album g = genre c = playcount i = id @@ -493,6 +555,12 @@ # Delete all songs by the artist called 'Schlumminguch' gnupod_search.pl -m /mnt/ipod --artist="Schlummiguch" --delete + # Copy album 'Gnu' by the artist called 'Schlumminguch' to /tmp + gnupod_search.pl -m /mnt/ipod --artist="Schlummiguch" --album="Gnu" --copy --path=/tmp + + # Backup all songs on iPod to current directory + gnupod_search.pl -m /mnt/ipod --copy + # Record the changes to the iTunes database (this is essential) mktunes.pl -m /mnt/ipod