From: Ian Beckwith Date: Sun, 7 Nov 2010 22:02:51 +0000 (+0000) Subject: id3fs-tag -V: per-dir summaries X-Git-Url: http://erislabs.net/gitweb/?p=id3fs.git;a=commitdiff_plain;h=a88e7ff8c0d20c928dbe6ea52c779454779d2366 id3fs-tag -V: per-dir summaries --- diff --git a/bin/id3fs-tag b/bin/id3fs-tag index 9566d2c..9ed8aac 100755 --- a/bin/id3fs-tag +++ b/bin/id3fs-tag @@ -25,8 +25,7 @@ use vars qw($me); $me=($0=~/(?:.*\/)?(.*)/)[0]; my @extensions=qw(mp3); # FIXME: flac ogg -my (%argv_tags, %dir_tags, %file_tags); -my $current_argv; +my (%file_tags); my $verbose=0; my $help=0; my ($artist, $album, $track, $tracknum, $year, $v1genre, $comment, @@ -72,7 +71,6 @@ while(my $path=shift @ARGV) warn("$me: $path: not found\n"); next; } - $current_argv=$path; # ick, global nastiness File::Find::find( {wanted => \&wanted, follow => 1, no_chdir => 1}, $path); } @@ -210,39 +208,83 @@ sub gather_tags @tags=map { join('/', grep { defined; } @$_); } @tags; @tags=ID3FS::AudioFile::uniq(@tags); $file_tags{$path}=\@tags; - my @argv_tags=(); - @argv_tags=@{$argv_tags{$current_argv}} if($argv_tags{$current_argv}); - $argv_tags{$current_argv}=[ ID3FS::AudioFile::uniq(@tags, @argv_tags) ]; } sub summarize_tags { - my @all_tags=ID3FS::AudioFile::uniq(map { @$_; } values(%argv_tags)); + my @all_tags=ID3FS::AudioFile::uniq(map { @$_; } values(%file_tags)); + + # group tags by directories + my %dir_tags=(); + my @dirs=map { s/(.*)\/.*/$1/; $_; } keys %file_tags; + @dirs=ID3FS::AudioFile::uniq(@dirs); + for my $tag (@all_tags) + { + DIR: for my $dir (@dirs) + { + for my $file (keys %file_tags) + { + next unless(@{$file_tags{$file}}); + next unless($file =~ /^$dir/); + next DIR unless(grep { $_ eq $tag; } @{$file_tags{$file}}); + } + push(@{$dir_tags{$dir}}, $tag); + } + } + + # remove dir tags from files + for my $path (keys %dir_tags) + { + for my $file (keys %file_tags) + { + next unless($file =~ /^$path/); + $file_tags{$file} = [ ID3FS::AudioFile::list_remove( + $dir_tags{$path}, + $file_tags{$file}) ]; + } + } + # find common tags my @common_tags=(); -OUTER: for my $tag (@all_tags) + OUTER: for my $tag (@all_tags) { - for my $taglist (values(%argv_tags)) + for my $taglist (values(%dir_tags)) { next OUTER unless(grep { $_ eq $tag; } @$taglist); } push(@common_tags, $tag); } - print "ALL: ", join(', ', @all_tags), "\n"; - print "COMMON: ", join(', ', @common_tags), "\n"; - use Data::Dumper; - # remove common tags from %argv_tags - for my $argv (keys(%argv_tags)) + # remove common tags from %file_tags + for my $filename (keys(%file_tags)) + { + next unless(@{$file_tags{$filename}}); + $file_tags{$filename}= [ ID3FS::AudioFile::list_remove(\@common_tags, + $file_tags{$filename}) ]; + } + + # remove common tags from %dir_tags + for my $path (keys(%dir_tags)) + { + next unless(@{$dir_tags{$path}}); + $dir_tags{$path}= [ ID3FS::AudioFile::list_remove(\@common_tags, + $dir_tags{$path}) ]; + } + +# print "ALL: ", join(', ', @all_tags), "\n"; + print "Common tags: ", join(', ', @common_tags), "\n"; +# print "PER-DIR:\n"; + for my $path (sort keys %dir_tags) { - next unless(@{$argv_tags{$argv}}); - $argv_tags{$argv}= [ ID3FS::AudioFile::list_remove(\@common_tags, $argv_tags{$argv}) ]; + next unless(@{$dir_tags{$path}}); + print "$path: ", join(', ', @{$dir_tags{$path}}), "\n"; } - print "PER-DIR: \n"; - for my $argv (keys(%argv_tags)) +# print "PER-FILE: \n"; + for my $filename (keys(%file_tags)) { - print "$argv: ", join(', ', @{$argv_tags{$argv}}), "\n"; + next unless(@{$file_tags{$filename}}); + print "$filename: ", join(', ', @{$file_tags{$filename}}), "\n"; } } diff --git a/lib/ID3FS/AudioFile/Mp3.pm b/lib/ID3FS/AudioFile/Mp3.pm index 8ec87ef..f060dc9 100644 --- a/lib/ID3FS/AudioFile/Mp3.pm +++ b/lib/ID3FS/AudioFile/Mp3.pm @@ -207,7 +207,8 @@ sub delete_tags { my($self, $tags, $delvals)=@_; my $current=$self->tags(); - my @current=split(/\s*,\s*/, $current); + my @current=(); + @current=split(/\s*,\s*/, $current) if($current); my @tags=split(/\s*,\s*/, $tags); my %hash=(); @hash{@current}=();