Fix bug in parsing the output from freedb.
authorSteve Youngs <steve@steveyoungs.com>
Sun, 19 Jun 2011 07:26:41 +0000 (17:26 +1000)
committerSteve Youngs <steve@steveyoungs.com>
Sun, 19 Jun 2011 07:26:41 +0000 (17:26 +1000)
Occasionally track titles are so long that they put them on multiple lines
in the freedb database.  This patch handles that case.

It also adds the option to write a playlist file on the fly.  This is good
for those times when order is important.

* zcdrip.in (_parse_cddb): Handle multi-line track title entries.

* zcdrip.in (args): Add `-p' `--playlist' arguments to write a
playlist file on the fly.

* zcdrip.in (mp3rip,oggrip): Optionally write a playlist file.

Signed-off-by: Steve Youngs <steve@steveyoungs.com>
zcdrip.in

index ce096b7..ea57c23 100644 (file)
--- a/zcdrip.in
+++ b/zcdrip.in
@@ -5,7 +5,7 @@
 ## Author:        Steve Youngs <steve@steveyoungs.com>
 ## Maintainer:    Steve Youngs <steve@steveyoungs.com>
 ## Created:       <2006-08-08>
-## Time-stamp:    <Sunday Jun 19, 2011 12:21:40 steve>
+## Time-stamp:    <Sunday Jun 19, 2011 16:47:17 steve>
 
 ## Redistribution and use in source and binary forms, with or without
 ## modification, are permitted provided that the following conditions
@@ -93,6 +93,7 @@ Synopsis:
         [ -a ALBUM ] [ --album=ALBUM ] [ -s ARTIST ] [ --sameartist=ARTIST ]
        [ -t ENCODING_TYPE ] [ --type=ENCODING_TYPE ] [ -f | --flac_copy ]
        [ -e FILE | --edit_rcfile=FILE ] [ -O | --online ] [ -d DIR | --dir=DIR ]
+        [ -p PLAYLISTFILE | --playlist=PLAYLISTFILE ]
   $ourname 
         [ -h | --help ]
   $ourname
@@ -160,6 +161,12 @@ Options:
   --online
         Attempt to grab the track info from freedb.freedb.org.
 
+  -p PLAYLISTFILE
+  --playlist=PLAYLISTFILE
+        Write a playlist file to PLAYLISTFILE.  Argument PLAYLISTFILE
+        is _NOT_ optional, and should be given as the full path to the 
+        file.
+
   -r FILE
   --rcfile=FILE
         Get track titles and artist names from FILE.  Argument FILE
@@ -393,10 +400,21 @@ _parse_cddb ()
     fi
 
     # create an rcfile
+    :>$rcfile                  # make sure it is empty
+
+    for (( i=0; i<$tracks; i++ )); do
+       TRACK=""
+       for TITLE in $(grep TTITLE${i}= ${zlog}|cut -d= -f2); do
+           TRACK="${TRACK} ${TITLE}"
+       done
+       TRACK=${TRACK//[[:cntrl:]]/}
+       TRACK=${TRACK/[[:space:]]/}
+       echo ${TRACK} >> ${rcfile}
+    done
+
     if [[ $various == yes || -n $(grep TTITLE $zlog|grep $art) ]]; then
-       grep TTITLE $zlog|cut -d= -f2|sed 's@ [-/] @|@' > $rcfile
+       sed -i 's@ [-/] @|@g' $rcfile
     else
-       grep TTITLE $zlog|cut -d= -f2 > $rcfile
        sed -i "s@^@${art}|@g" $rcfile
     fi
     
@@ -708,6 +726,7 @@ mp3rip ()
                    --ty $year --tg $genre --tc $zcomment $wavfile \
                    --tn $i ${dir}/$filename[$i]
            fi
+           [[ $write_playlist == yes ]] && echo ${dir}$filename[$i] >> $plfile
        fi
        [[ -f $wavfile ]] && rm -f $wavfile
     done
@@ -731,6 +750,7 @@ oggrip ()
                    -N $i $wavfile
                rm $wavfile
            fi
+           [[ $write_playlist == yes ]] && echo ${dir}/$filename[$i] >> $plfile
        fi
     done
 }
@@ -779,7 +799,7 @@ chkreqs ()
        
 
 # Command line parsing
-args=mofihVO-:r:a:s:t:e:d:
+args=mofihVO-:r:a:s:t:e:d:p:
 flavour=0
 rc=0
 interactive=no
@@ -805,6 +825,7 @@ while getopts $args opts; do
                (album?*) album=${OPTARG/album=/} ;;
                (sameartist?*) art=${OPTARG/sameartist=/} ;;
                (dir?*) dir=${OPTARG/dir=/} ;;
+               (playlist?*) write_playlist=yes; plfile=${OPTARG/playlist=/} ;;
                (version) _version; exit 0 ;;
                (help) cmd=usage; rv=0 ;;
                (*) 
@@ -824,6 +845,7 @@ while getopts $args opts; do
        (s) art=$OPTARG ;;
        (t) atype=$OPTARG; (( ++flavour )) ;;
        (d) dir=$OPTARG ;;
+       (p) write_playlist=yes; plfile=$OPTARG ;;
        (V) _version; exit 0 ;;
        (h) cmd=usage; rv=0 ;;
        (*) rv=2 ;;