Index: trunk/flactrack
===================================================================
--- trunk/flactrack	(revision 48)
+++ trunk/flactrack	(revision 1)
@@ -7,17 +7,5 @@
 # TODO: separate sox filter for each track!
 
-use FindBin;
-use lib "$FindBin::RealBin/lib";
-
-use Getopt::Long qw{:config no_ignore_case};
-use File::Spec::Functions qw{catfile splitpath};
-use File::Path;
-use Audio::FLAC::Header;
-use MusicBrainz;
-use Text::Unidecode;
-use Cwd;
-
-# default to using tags
-my $TAGS = 1;
+use Getopt::Long;
 
 GetOptions(
@@ -25,9 +13,4 @@
     't=s' => \my $TYPE,
     'x=s' => \my $SOX_FILTER,
-    'all|a' => \my $ALL,
-    'dir|d=s' => \my $DIRECTORY,
-    'ascii-tags' => \my $ASCII_TAGS,
-    'tags!' => \$TAGS,
-    'force' => \my $FORCE,
 );
 
@@ -37,66 +20,21 @@
 $TYPE ||= 'mp3';
 
-# default to the current directory
-$DIRECTORY ||= cwd;
+# for getting track metadata from MusicBrainz
+my %info;
+if ($TYPE eq 'mp3') {
+    require Audio::FLAC::Header;
+    require LWP;
+    require XML::XPath;
+    require XML::XPath::XMLParser;
 
-my $flac = Audio::FLAC::Header->new($FLAC_FILE) or die "Can't read FLAC header from $FLAC_FILE\n";
+    my $flac = Audio::FLAC::Header->new($FLAC_FILE) or warn "Can't read FLAC header from $FLAC_FILE\n";
+    my $discid = $flac->tags('MBZ_DISCID') or warn "No MBZ_DISCID tag in $FLAC_FILE\n" if $flac;
+    #TODO: calculate TOC and DISCID from cuesheet if there is no MBZ_DISCID tag present
 
-# for getting track metadata from MusicBrainz
-my $info;
-if ($ALL || ($TYPE eq 'mp3' && $TAGS)) {
-    (my $properties_file = $FLAC_FILE) =~ s/\.flac$/.properties/;
-    if (-e $properties_file) {
-        require Config::Properties;
-
-        # the properties are in UTF-8; mark them as such so unidecode works correctly later
-        my $properties = Config::Properties->new(file => $properties_file, encoding => 'utf8');
-        $info = $properties->getProperties;
-    } else {
-
-        my $discid = $flac->tags('MUSICBRAINZ_DISCID') or warn "No MUSICBRAINZ_DISCID tag in $FLAC_FILE\n" if $flac;
-        #TODO: calculate TOC and DISCID from cuesheet if there is no MUSICBRAINZ_DISCID tag present
-
-        $info = get_musicbrainz_info($discid);
-    }
-    exit unless $info;
+    #TODO: use the functions in mbz instead of repeating them here)
+    %info = get_musicbrainz_info($discid);
 }
 
-if ($ALL) {
-    # if we are converting an entire album file, create a directory to hold the mp3s
-    # name the directory ARTIST.DATE.ALBUM, so it will sort nicely
-    my $base_dir = $DIRECTORY;
-    my $album_dir = join('.', map { to_filename($_) } @{$info}{qw{ALBUMARTISTSORT ORIGINALDATE ALBUM}});
-    # need to append "disc#" if this is a multidisc album
-    if ($info->{DISCTOTAL} > 1) {
-        $album_dir .= '.disc_' . $info->{DISCNUMBER};
-    }
-    $DIRECTORY = catfile($base_dir, $album_dir);
-    if (-e $DIRECTORY and not $FORCE) {
-        die "$DIRECTORY already exists, skipping. (Use --force to overwrite)\n";
-    }
-    #die $DIRECTORY;
-}
-
-if ($ALL) {
-    die "Use of --all requires a --directory\n" unless $DIRECTORY;
-    die "No track info found on MusicBrainz for $FLAC_FILE\n" unless $info;
-    use YAML;
-    my $cuesheet = $flac->cuesheet;
-    my $count = scalar grep { /TRACK \d\d/ } @{ $flac->cuesheet };
-    print "Found $count tracks\n";
-    #TODO: default to just 01, 02, etc. if there is no $info
-    %TRACKS = map {
-        $_ => catfile($DIRECTORY, sprintf('%02d.%s', $_, to_filename($info->{sprintf 'TRACK%02d.TITLE', $_})))
-    } (1 .. $count);
-    #print Dump(\%TRACKS);
-    for my $tracknum (sort { $a <=> $b } keys %TRACKS) {
-        printf "%2d: %s\n", $tracknum, $TRACKS{$tracknum};
-    }
-    mkpath($DIRECTORY);
-}
-
-#TODO: all the option of sorting by tracknum or not
-#while (my ($tracknum, $title) = each %TRACKS) {
-for my $tracknum (sort { $a <=> $b } keys %TRACKS) {
+while (my ($tracknum, $title) = each %TRACKS) {
     if ($tracknum !~ /^\d+$/) {
 	warn "Don't know what to do with track number '$tracknum'";
@@ -111,16 +49,15 @@
     }
 
-    my $title = quotemeta($TRACKS{$tracknum});
+    $title = quotemeta($title);
     if ($TYPE eq 'mp3') {
         # bitrate of 192
         $cmd .= qq{| lame -b 192};
         # if there is track info, add it as ID3 tags
-        if ($info) {
-            my $track_key = sprintf 'TRACK%02d', $tracknum;
-            $cmd .= sprintf q{ --tt %s --ta %s --tl %s --ty %d --tn %d},
-                quote($ASCII_TAGS ? unidecode($info->{"$track_key.TITLE"}) : $info->{"$track_key.TITLE"}),
-                quote($ASCII_TAGS ? unidecode($info->{"$track_key.ARTIST"}) : $info->{"$track_key.ARTIST"}),
-                quote($ASCII_TAGS ? unidecode($info->{ALBUM}) : $info->{ALBUM}),
-                ($info->{ORIGINALDATE} =~ /^(\d\d\d\d)/)[0],
+        if (%info) {
+            my $track = $info{TRACKS}[$tracknum];
+            $cmd .= sprintf q{ --tt %s --ta %s --tl %s --tn %d},
+                quote($$track{TITLE}),
+                quote($$track{ARTIST}),
+                quote($info{ALBUM}),
                 $tracknum;
         }
@@ -133,5 +70,4 @@
     #die $cmd;
     system $cmd;
-    die "\nFLAC decoding canceled\n" if ($? & 127);
 
     print "\n" if $SOX_FILTER;
@@ -144,12 +80,50 @@
 }
 
-sub to_filename {
-    my @strings = @_;
-    return map {
-        s/&/ and /g;
-        unidecode($_);
-        s/[^a-z0-9-_ ]+//gi;
-        s/ +/_/g;
-        lc;
-    } @strings;
+
+# make the output terminal handle UTF-8 characters
+#binmode STDOUT, ':utf8';
+#my $info = get_musicbrainz_info($discid);
+#for my $key (sort keys %{ $info }) {
+#    print "$key=$$info{$key}\n";
+#}
+
+sub lookup_release {
+    my ($discid) = @_;
+    my $ua = LWP::UserAgent->new;
+
+    my $uri = URI->new('http://musicbrainz.org/ws/1/release/');
+    $uri->query_form(type => 'xml', discid => $discid);
+
+    my $res = $ua->get($uri);
+    return $res->decoded_content;
 }
+
+sub get_musicbrainz_info {
+    my ($discid) = @_;
+    my %info;
+
+    $info{MBZ_DISCID} = $discid;
+
+    my $xpath = XML::XPath->new();
+
+    $xpath->set_xml(lookup_release($discid));
+
+    # TODO: check for more than 1 release?
+
+    $info{MB_RELEASE_ID} = $xpath->findvalue('//release/@id');
+    $info{ALBUM}         = $xpath->findvalue('//release/title');
+    $info{ARTIST}        = $xpath->findvalue('//release/artist/name');
+    $info{TRACKS}        = [];
+
+    # TODO: get release date
+
+    my $tracknum = 1;
+    for my $track_node ($xpath->findnodes('//track-list/track')) {
+        $info{TRACKS}[$tracknum]{MB_TRACKID} = $xpath->findvalue('@id', $track_node);
+        $info{TRACKS}[$tracknum]{TITLE}      = $xpath->findvalue('title', $track_node);
+        $info{TRACKS}[$tracknum]{ARTIST}     = $xpath->findvalue('artist/name', $track_node) || $info{ARTIST};
+        $tracknum++;
+    }
+
+    return %info;
+}
