flac/ogg: allow any case for comment names, files in the wild seem to have Genre...
authorIan Beckwith <ianb@erislabs.net>
Sun, 26 Sep 2010 02:23:39 +0000 (03:23 +0100)
committerIan Beckwith <ianb@erislabs.net>
Sun, 26 Sep 2010 02:23:39 +0000 (03:23 +0100)
lib/ID3FS/File/Flac.pm
lib/ID3FS/File/Ogg.pm

index 0f9999f..ebe4ea8 100644 (file)
@@ -19,21 +19,21 @@ sub new
 
 sub get
 {
-    my ($self, $tag, $verbose)=@_;
-    if(exists($self->{tags}->{$tag})   &&
-       defined($self->{tags}->{$tag})  &&
-       length($self->{tags}->{$tag})   &&
-       $self->{tags}->{$tag} =~ /\S+/)
+    my ($self, $tag, $complain)=@_;
+    for my $key (keys %{$self->{tags}})
     {
-       my $val=$self->{tags}->{$tag};
-       $val =~ s/\//-/g; # drop slashes
-       return $val;
-    }
-    else
-    {
-       warn("$self->{path}: no $tag defined in FLAC comments\n") if($verbose);
-       return undef;
+       if($key =~ /$tag/i &&
+          defined($self->{tags}->{$key})  &&
+          length($self->{tags}->{$key})   &&
+          $self->{tags}->{$key} =~ /\S+/)
+       {
+           my $val=$self->{tags}->{$key};
+           $val =~ s/\//-/g; # drop slashes
+           return $val;
+       }
     }
+    warn("$self->{path}: no $tag defined in FLAC comments\n") if($complain);
+    return undef;
 }
 
 sub artist    { shift->get("ARTIST", 1); }
@@ -58,9 +58,21 @@ sub year
 sub tags
 {
     my $self=shift;
-    my $genre=$self->get("GENRE");
-    return({}) unless($genre);
-    my @tags=split(/\s*,\s*/, $genre);
+    my @tags=();
+    my $tags={};
+    for my $key (keys %{$self->{tags}})
+    {
+       if($key =~ /genre/i &&
+          defined($self->{tags}->{$key})  &&
+          length($self->{tags}->{$key})   &&
+          $self->{tags}->{$key} =~ /\S+/)
+       {
+           push(@tags, $self->{tags}->{$key});
+       }
+    }
+    # combine then split on commas
+    # so multiple comma-delimited tags will work
+    @tags=split(/\s*,\s*/, join(', ', @tags));
     for my $tag (@tags)
     {
        if($tag=~/([^\/]+)\/(.*)/)
index f05d8c2..ed6c17a 100644 (file)
@@ -13,28 +13,35 @@ sub new
 
     $self->{path}=shift;
     $self->{ogg}=Ogg::Vorbis::Header->new($self->{path});
+    $self->{comments}=[ $self->{ogg}->comment_tags() ];
     return $self;
 }
 
 sub get
 {
-    my ($self, $tag, $verbose)=@_;
-    my @comments=$self->{ogg}->comment($tag);
-    if(@comments)
+    my ($self, $tag, $complain)=@_;
+    for my $commenttype (@{$self->{comments}})
     {
-       # take first comment with actual contents
-       while(my $comment=shift @comments)
+       if($commenttype =~ /$tag/i)
        {
-           if(defined($comment) &&
-              length($comment)  &&
-              $comment =~ /\S+/)
+           my @comments=$self->{ogg}->comment($commenttype);
+           if(@comments)
            {
-               $comment =~ s/\//-/g; # drop slashes
-               return $comment;
+               # take first comment with actual contents
+               while(my $comment=shift @comments)
+               {
+                   if(defined($comment) &&
+                      length($comment)  &&
+                      $comment =~ /\S+/)
+                   {
+                       $comment =~ s/\//-/g; # drop slashes
+                       return $comment;
+                   }
+               }
            }
        }
     }
-    warn("$self->{path}: no $tag defined in Ogg comments\n") if($verbose);
+    warn("$self->{path}: no $tag defined in Ogg comments\n") if($complain);
     return undef;
 }
 
@@ -60,7 +67,14 @@ sub year
 sub tags
 {
     my $self=shift;
-    my @comments=$self->{ogg}->comment("Genre");
+    my @comments;
+    for my $commenttype (@{$self->{comments}})
+    {
+       if($commenttype =~ /genre/i)
+       {
+           push(@comments,$self->{ogg}->comment($commenttype));
+       }
+    }
     my $tags={};
     if(@comments)
     {
@@ -68,6 +82,9 @@ sub tags
        @comments= grep { defined($_); } @comments;
        @comments= grep { length($_); }  @comments;
        @comments= grep { /\S+/; }       @comments;
+       # combine then split on commas
+       # so multiple comma-delimited tags will work
+       @comments=split(/\s*,\s*/, join(', ', @comments));
        for my $comment (@comments)
        {
            if($comment=~/([^\/]+)\/(.*)/)