]> diplodocus.org Git - flac-archive/blobdiff - fa-tags
Reformat requirements and add MusicBrainz::Client.
[flac-archive] / fa-tags
diff --git a/fa-tags b/fa-tags
index 34eafb85821ecdc7876a880bb8b385c397dd3fa7..d399287bf71805ff276d4f072f38f6efd8895a2e 100755 (executable)
--- a/fa-tags
+++ b/fa-tags
-#! /usr/bin/env zsh
+#! /usr/bin/env perl
 
 # $Id$
 
-# Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
-# Copyright (c) 2003-2004 Jesus Climent <jesus.climent@hispalinux.es>
-# Copyright (c) 2004 Eric Gillespie <epg@pretzelnet.org>
-# This code is hereby licensed for public consumption under either the
-# GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
+# See fa-flacd for documentation.
 
-set -e
+use strict;
+use warnings;
 
-if [[ -z ${CDDBURL} ]]; then
-    CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
-fi
+use Getopt::Long;
+use Pod::Usage;
 
-get_cddb_protocol () {
-    for CDDBPROTO in 5 4 3; do
-        if [[ \
-            $(cddb-tool stat $CDDBURL $USER $HOST $CDDBPROTO \
-            | sed 's/^\([0-9][0-9]*\).*/\1/;q') = '210' ]]; then
-            return 0
-        fi
-    done
+use MusicBrainz::Client::Simple;
 
-    echo "${CDDBURL} does not work"
-    exit 2
-}
-
-handle_read=$(<<EOF
-function rstrip_print(_s) {
-    sub(/[[:space:]]*\$/, "", _s)
-    print(_s)
-}
-
-BEGIN {
-    FS="="
-}
-
-/^DTITLE=/ {
-    split(\$2, a, /[[:blank:]]*\/[[:blank:]]*/)
-    rstrip_print("ARTIST=" a[1])
-    rstrip_print("ALBUM=" a[2])
-}
+my $help;
+GetOptions(
+           'help|h|?' => \$help,
+          ) or pod2usage();
+$help and pod2usage(-exitstatus=>0, -verbose=>1);
 
-/^DYEAR=/ {
-    rstrip_print("DATE=" \$2)
-}
-
-/^TTITLE[[:digit:]]+/ {
-    rstrip_print("TITLE=" \$2)
-}
-EOF
-)
+my $trackcount = shift or pod2usage();
 
-perl_handle_read=$(<<EOF
-(\$tag, \$value) = split(/=/, \$_, 2);
+my $mb = new MusicBrainz::Client::Simple;
+my @result = $mb->lookup_cd;
+die($mb->get_error) unless $mb->success;
 
-if (/^DTITLE=/) {
-    (\$artist, \$album) = split(/\\s*\\/\\s*/, \$value);
-    print("ARTIST=\$artist\\n");
-    print("ALBUM=\$album\\n");
+open(F, '>candidate-tags-0') or die("open('>candidate-tags-0'): $!");
+print(F "$_=\n") for ('ARTIST', 'ALBUM', 'DATE');
+print(F "TITLE=\n") for 1 .. $trackcount;
+close(F) or die("close('>candidate-tags-0'): $!");
 
-} elsif (/^DYEAR=/) {
-    print("DATE=\$value\\n");
+my $i;
+for my $album (@result) {
+    $i++;
+    open(F, '>', "candidate-tags-$i") or die("open('>candidate-tags-$i'): $!");
 
-} elsif (/^TTITLE\\d+/) {
-    print("TITLE=\$value\\n");
-}
-EOF
-)
+    print(F 'ARTIST=', $album->get_artist->get_name, "\n");
+    print(F 'ALBUM=', $album->get_name, "\n");
+    print(F "DATE=\n");
 
-handle_queries () {
-    local F
-    local i=1
+    my @tracks = $album->get_tracks;
+    my $name;
+    my $track;
+    for my $j (1 .. $trackcount) {
+        if ($track = shift(@tracks)) {
+            $name = $track->get_name;
+        } else {
+            $name = '';
+        }
+        print(F "TITLE=$name\n");
+    }
 
-    # First eat the status line.
-    read F
-
-    while read -A F; do
-        [[ $F = '.' ]] && break
-        cddb-tool read $CDDBURL $CDDBPROTO $USER $HOST $F[1] $F[2] \
-            | nawk $handle_read > candidate-tags-$i
-# XXX The awk script has at least one problem; since we can't split to
-# exactly two components, we break on track names with equal signs.
-# Use Perl instead:
-#        cddb-tool read $CDDBURL $CDDBPROTO $USER $HOST $F[1] $F[2] \
-#            | perl -lne $handle_read > candidate-tags-$i
-        i=$(( $i + 1 ))
-    done
+    close(F) or die("close('>candidate-tags-$i'): $!");
 }
-
-get_cddb_protocol
-(cddb-tool query $CDDBURL $CDDBPROTO $USER $HOST "$@" | handle_queries) &
-
-cat > candidate-tags-0 <<EOF
-ARTIST=
-ALBUM=
-DATE=
-EOF
-for i in $(jot $2 1); do
-    echo 'TITLE=' >> candidate-tags-0
-done