1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#!/bin/sh
# Copyright (C) 1999 Nathaniel Smith <njs@uclink4.berkeley.edu>
# Copyright (C) 1999, 2000, 2001 Robert Woodcock <rcw@debian.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.
#
# You should have recieved a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright for this work is to expire January 1, 2010, after which it
# shall be public domain.
# TODO:
# - Add more error checking
# KNOWN BUGS:
# - Not much error checking, esp. of arguments
# - Submitted via: line is created by template, when it really should be in send.
# Oh well.
VERSION=0.4.1
NAME=cddb-tool
#return codes
BAD_SYNTAX_ERR=10 # invalid CDDB file
NO_TMP_ERR=11 # can't create a temp file
NO_MATCH_ERR=12 # try submitting one
LOOKUP_ERR=13 # problem connecting to cddb server
EMPTY_QUERY_RESPONSE=14 # query response = "", (probably no net connection)
# assume a reasonable default if $WGET is undefined
if [ "$WGET" = "" ]; then
WGET=wget
fi
usage() {
cat << EOF
$NAME version $VERSION
usage: one of:
$0 parse file
$0 template disc-id tracks
$0 send file address
$0 read server user host disc-id genre
$0 query server user host disc-id tracks
$0 help
EOF
}
help() {
cat << EOF
$NAME version $VERSION
A toolbox for doing cddb related stuff
Usage: $0 command [command_options]
Commands:
parse file
Get data out of a cddb file - dumps to stdout in a form
source'able by the shell
send file address
Mails a CDDB file to a specified address, using correct format.
Category should be one of blues, classical, country, data, folk,
jazz, newage, reggae, rock, soundtrack, or misc.
template disc-id tracks
Generates a template (empty) cddb file to stdout. The command
line should be essentially the output of cd-discid.
query server user host disc-id tracks
Looks up disc on server (should be of form "http://host/~cddb/cddb.cgi")
remainder of command line is in the same form as that returned
by the cd-discid program.
read server user host disc-id genre
CDDB file is dumped to stdout. File will contain an extra
#CATEGORY= line, which leaves it a valid CDDB file but which will
be recognized by parse and send commands. Uses wget, so if you
need to use a proxy then just configure wget to do so. user and
host will be used for identifying ourselves to the CDDB server.
help
Display this.
EOF
}
COMMAND=$1
shift
case $COMMAND in
parse) # takes 1 argument, a filename, and dumps out a sh parseable version
CDDBFILE="$1"
set -e
# names chosen to match usage in abcde code
DISCID=$(grep ^DISCID= "$CDDBFILE" | cut -f2 -d= | tr -d \[:cntrl:\])
DARTISTALBUM=$(grep ^DTITLE= "$CDDBFILE" | cut -f2- -d= | sed 's- / -~-g')
DARTIST=$(echo $DARTISTALBUM | cut -f1 -d~ | sed 's,\\,\\\\,g;s,\([\"\$\`]\),\\\1,g' | tr -d \[:cntrl:\])
DALBUM=$(echo $DARTISTALBUM | cut -f2 -d~ | sed 's,\\,\\\\,g;s,\([\"\$\`]\),\\\1,g' | tr -d \[:cntrl:\])
CDDBGENRE=$(grep '^#CATEGORY=' "$CDDBFILE" | cut -f2- -d=)
set +e
echo DISCID=\"$DISCID\"
echo DALBUM=\"$DALBUM\"
echo DARTIST=\"$DARTIST\"
echo CDDBGENRE=\"$CDDBGENRE\"
NUMTRACKS=$(grep -E '^TTITLE[0-9]+=' "$CDDBFILE" | wc -l)
CURRTRACK=0
while [ "$CURRTRACK" -lt $NUMTRACKS ]; do
CURRTRACKM1=$CURRTRACK # Track minus 1 (cddb numbers from 0)
CURRTRACK=$(expr $CURRTRACK + 1)
echo -n "TRACK${CURRTRACK}=\""
grep ^TTITLE${CURRTRACKM1}= "$CDDBFILE" | cut -f2 -d= | sed 's,\\,\\\\,g;s,\([\"\$\`]\),\\\1,g' | tr -d \[:cntrl:\]
echo \"
done
;;
template)
DISCID="$@"
DISCNUM=$1
echo '# xmcd CD database file'
echo '#'
echo '# Track frame offsets:'
NUMTRACKS=$2
for x in $(seq 3 $(expr $NUMTRACKS + 2))
do
printf "#\t$(echo "$DISCID" | cut -f$x -d' ')\n"
done
x=$(expr $x + 1)
LENGTH=$(echo "$DISCID" | cut -f$x -d' ')
echo "#"
echo "# Disc length: $LENGTH seconds"
echo "#"
echo "# Submitted via: $NAME $VERSION"
echo "#"
echo "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc"
echo "#CATEGORY=misc"
echo DISCID="$DISCNUM"
echo "DTITLE=Unknown Artist / Unknown Album"
# TTITLE0 -- TTITLEn
for x in $(seq 1 $NUMTRACKS)
do
echo "TTITLE$(expr $x - 1)=Track $x"
done
echo "EXTD="
# EXTT0 -- EXTTn
for x in $(seq 1 $NUMTRACKS)
do
echo "EXTT$(expr $x - 1)="
done
echo "PLAYORDER="
;;
send) # cddb-tool send filename email@address
FILE="$1"
ADDRESS="$2"
DISCID=$(grep ^DISCID= "$FILE" | cut -f2 -d= | tr -d \[:cntrl:\])
CDDBGENRE=$(grep '^#CATEGORY=' "$FILE" | cut -f2- -d=)
grep -v "^#CATEGORY=" "$FILE" | mail "$ADDRESS" -s "cddb $CDDBGENRE $DISCID"
;;
query) # cddb-tool query serverurl user host discid...
SERVER="$1"
USER="$2"
HOST="$3"
HELLOINFO="$USER+$HOST+$NAME+$VERSION"
shift 3
TRACKINFO="$@"
TRACKINFOPLUS=$(echo $TRACKINFO | tr ' ' '+')
RESULTS=$($WGET -q -O - "$SERVER?cmd=cddb+query+$TRACKINFOPLUS\&hello=$HELLOINFO\&proto=3") || exit $LOOKUP_ERR
echo $RESULTS | tr '\r' '\n' | tr -s '\n' | sed 's/^ //g'
;;
read) # cddb-tool read serverurl user host genre discnumber
SERVER="$1"
USER="$2"
HOST="$3"
CATEGORY="$4"
DISCID="$5"
HELLOINFO="$USER+$HOST+$NAME+$VERSION"
$WGET -q -O - $CDDBDATA "$SERVER?cmd=cddb+read+$CATEGORY+$DISCID\&hello=$HELLOINFO\&proto=3" 2>/dev/null
;;
help) help ;;
*) usage ;;
esac
|