From efb1aa41c60da50d7a8960c819eb73894f736d3a Mon Sep 17 00:00:00 2001 From: Ian Beckwith Date: Mon, 20 Sep 2010 00:09:44 +0100 Subject: [PATCH] implement id3 tag lookup --- lib/ID3FS/DB.pm | 31 ++++++++++++++++++--- lib/ID3FS/File/Mp3.pm | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/lib/ID3FS/DB.pm b/lib/ID3FS/DB.pm index 9da8069..25ef83b 100644 --- a/lib/ID3FS/DB.pm +++ b/lib/ID3FS/DB.pm @@ -85,6 +85,23 @@ sub add { my($self,$path)=@_; my $file=ID3FS::File->new($path); + return unless(defined($file)); + my $artist=$file->artist(); + my $album=$file->album(); + my $v1genre=$file->v1genre(); + my $year=$file->year(); + my $audiotype=$file->album(); +#FIXME +# my $haspic=$file->haspic(); + my $tags=$file->tags(); + print "$path:\n"; + for my $thing (qw(artist album v1genre year audiotype)) + { + no strict 'refs'; + my $thingval=$file->$thing(); + use strict 'refs'; + print("\t$thing: ", $thingval, "\n") if(defined($thingval)); + } } sub cmd @@ -117,12 +134,18 @@ CREATE TABLE tags ( name ); -CREATE TABLE tags_x_files ( +CREATE TABLE tagvalues ( + id INTEGER PRIMARY KEY, + name +); + +CREATE TABLE files_x_tags ( files_id, tags_id ); -CREATE TABLE v1genres ( - id, - name +CREATE TABLE tags_x_tagvalues ( + tags_id, + tagvalues_id ); + diff --git a/lib/ID3FS/File/Mp3.pm b/lib/ID3FS/File/Mp3.pm index 3f8964e..86a7c4b 100644 --- a/lib/ID3FS/File/Mp3.pm +++ b/lib/ID3FS/File/Mp3.pm @@ -2,6 +2,7 @@ package ID3FS::File::Mp3; use strict; use warnings; +use MP3::Tag; sub new { @@ -11,8 +12,81 @@ sub new bless($self,$class); $self->{path}=shift; + $self->{mp3}=MP3::Tag->new($self->{path}); + $self->get_tags(); + $self->{v1}=undef; + $self->{v1}=$self->{mp3}->{ID3v1} if(exists($self->{mp3}->{ID3v1})); + $self->{v2}=undef; + $self->{v2}=$self->{mp3}->{ID3v2} if(exists($self->{mp3}->{ID3v2})); + + # FIXME + $self->{tags}={}; + return $self; } +sub choose +{ + my ($self, $func, $verbose)=@_; + my $thing=undef; + if(defined($self->{v2})) + { + $thing=$self->{v2}->$func(); + } + if(defined($self->{v1}) && (!defined($thing) || !length($thing))) + { + $thing=$self->{v1}->$func(); + } + if(!defined($thing) || !length($thing)) + { + warn("$self->{path}: no $func defined in ID3 tags\n") if($verbose); + return undef; + } + return $thing; +} + +sub artist { shift->choose("artist", 1); } +sub album { shift->choose("album", 1); } +# We don't care if year is not set +sub year { shift->choose("year", 0); } +sub audiotype { return "mp3"; } + +sub v1genre +{ + my($self)=@_; + if(defined($self->{v1})) + { + return $self->{v1}->genre(); + } +} + +# FIXME: haspic; + +sub tags +{ + my $self=shift; + return $self->{tags}; +} + +sub get_tags +{ + my ($self)=@_; + # MP3::Tag->get_tags shows cryptic debug info via print when it finds + # an unhandled id3v2 version, in addition to the warning, so use + # select to send prints to /dev/null + my $oldout=undef; + if(open(NULL,">/dev/null")) + { + $oldout=select(NULL); + } + eval { $self->{mp3}->get_tags; }; + warn("$self->{path}: $@\n") if($@); + if(defined($oldout)) + { + select($oldout); + close(NULL); + } +} + 1; -- 2.11.0