Mercurial > ~darius > hgwebdir.cgi > cddb-stuff
diff cddb2.tcl @ 1:dd6e30f7eb42
Initial revision
author | darius |
---|---|
date | Wed, 09 Aug 2000 02:18:46 +0000 |
parents | |
children | f5daa3cdeb58 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cddb2.tcl Wed Aug 09 02:18:46 2000 +0000 @@ -0,0 +1,286 @@ +#!/usr/local/bin/tclsh8.2 + +# +# This software is copyright Daniel O'Connor (darius@dons.net.au) 1998, 1999 +# +# This software is release under the GNU Public License Version 2. +# A copy of this licence must be distributed with this software. +# + +proc main {} { + global argv0 argv tracks auto_path; + + lappend auto_path "[file dirname $argv0]"; + lappend auto_path "."; + + if { [ catch {loadlibs [ list tk82 ]} msg] } { + log "Failed to load library files - $msg"; + exit 1; + } +# puts $auto_path + + if { [ llength $argv ] != 4 } { + puts "Bad usage"; + puts "$argv0 <shellfile> <oldbasename> <newbasename> <infile>"; + exit; + } + + set shellfile [ lindex $argv 0 ]; + set oldbasename [ lindex $argv 1 ]; + set newbasename [ lindex $argv 2 ]; + set infile [ lindex $argv 3 ] + + if {[file exists $shellfile] && ![file writable $shellfile]} { + puts "$shellfile not writeable, aborting"; + exit 1; + } + + set tracks(albumname) ""; + set tracks(albumartist) ""; + set tracks(number) 0; + + set dtitle ""; + set dartist ""; + + # If the file doesn't exist we assume the user wants to enter new data + if {![file exists $infile] && $infile != "-"} { + puts "Please enter the number of tracks on the CD..." + while { 1 } { + puts -nonewline "-> "; + flush stdout; + set tracks(number) [ gets stdin ]; + if { ($tracks(number) >= 1) && ($tracks(number) <= 100) } { + break; + } + puts "Please enter a number between 1 and 100"; + } + + set tracks(disclen) 1; + set tracks(discid) 1; + + for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } { + set tracks(artist:$t_no) ""; + set tracks(title:$t_no) ""; + set tracks(offset:$t_no) 1; + } + + } else { + if { $infile == "-" } { + set fh stdin; + } else { + set fh [ open $infile "r" ]; + } + + set offset_seek 0; + set offset_list ""; + + while {![eof $fh]} { + set line [gets $fh]; +# puts "Line = $line"; + if {$offset_seek > 0} { + if {$line == "\#"} { +# puts "Comment skip" + continue; + } + + if {[ regexp {^\#[ ]+([0-9]+)} $line a offset] == 1} { + puts "Adding $offset"; + lappend offset_list $offset; + continue; + } + + # Change back to default state + puts "Not looking for offsets"; + set offset_seek 0; + } + + if {[regexp {^\# Track frame offsets:} $line] == 1} { + # Change to looking for offsets state + puts "Looking for offsets"; + set offset_seek 1; + } + + if {[regexp {^\# Disc length: +([0-9]+)} $line a tracks(disclen)] == 1} { + continue; + } + + if {[regexp {([A-Z0-9]*)=(.*)} $line a key value] == 0} { + continue; + } + + switch -regexp -- $key { + "DISCID" { + set tracks(discid) $value; +# puts "Got ID as $value"; + } + + "DTITLE" { + if { [ regexp {^(.*) / (.*)$} $value a tracks(albumartist) tracks(albumname)] == 1 } { +# puts "Got disc artist as $dartist, title as $dtitle"; + ; + } else { + set tracks(albumname) $value; +# puts "Got title as $value"; + } + } + + "TTITLE([0-9]+)" { + regexp {TTITLE([0-9]+)} $key a t_no; + set t_no [ expr $t_no + 1 ]; + + incr tracks(number); + + if {![regexp {^(.*) - (.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} { + if {![regexp {^(.*)--(.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} { + set tracks(artist:$t_no) $value; + } + } + } + + "EXTT([0-9]+)" { + regexp {EXTT([0-9]+)} $key a t_no; + set t_no [ expr $t_no + 1 ]; + set ext($t_no) $value; + } + } + } + +# puts "Offset list - $offset_list"; +# puts "Disclength - $tracks(disclen)"; + + if {$tracks(number) < 1 || [llength $offset_list] != $tracks(number)} { + puts stderr "Invalid file, $tracks(number)"; + exit 1; + } + + set i 1; + foreach t $offset_list { + set tracks(offset:$i) $t; + incr i; + } + + for {set i 1} {$i <= $tracks(number)} {incr i} { + if {![info exists tracks(title:$i)]} { + error "track $i has no title!"; + } + + if {[info exists tracks(artist:$i)]} { + # puts "artist exists for $i"; + continue; + } + + if {$dartist != ""} { + set tracks(artist:$i) $tracks(albumartist); + # puts "setting artists to $dartist for $i (disc artist)"; + continue; + } + + if {[info exists ext($i)] && $ext($i) != ""} { + set tracks(artist:$i) $ext($i); + # puts "setting artist to $ext($i) for $i"; + continue; + } + + set t_artist($i) "Unknown"; + } + +# puts "$tracks(number) tracks"; + + } + + print_tracks "Before editing"; + set tracks(updated) 0; + edit_tracks; + print_tracks "After editing"; + + if {$tracks(updated) == 0} { + puts "Nothing saved aborting"; + puts ""; + exit 0; + } + + set out [ open $shellfile "w" ]; + + if { [ catch { + if {$infile == "-"} { + set fh stdout; + } else { + file rename -force "$infile" "$infile.old" + set fh [ open "$infile" "w" ]; + } + + puts $fh "# xmcd CD database file +# Copyright (C) 1993-1999 CDDB, Inc. +# +# Track frame offsets:"; + for { set i 1 } { $i <= $tracks(number) } { incr i } { + puts $fh "#\t$tracks(offset:$i)" + } + puts $fh "# +# Disc length: $tracks(disclen) seconds +# +# Revision: 1 +# Submitted via: Tcl Mangler 0.1 - Copyright (c) 1999 Daniel O'Connor +# +DISCID=$tracks(discid) +DTITLE=$tracks(albumname)"; + for { set i 1 } { $i <= $tracks(number) } { incr i } { + puts $fh "TTITLE[expr $i - 1]=$tracks(artist:$i) - $tracks(title:$i)"; + } + } msg ] } { + puts "Failed to write $infile, $msg"; + } + + # Eat illegal characters + regsub {\\} $tracks(albumname) / tracks(albumname) + regsub \" $tracks(albumname) \\\" tracks(albumname) + + if { $dartist != "" } { + set dirname "$dartist - $tracks(albumname)"; + } else { + set dirname "Various Artists - $tracks(albumname)"; + } + + puts $out "mkdir \"$dirname\""; + for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } { + # Eat illegal characters + regsub -- {\\} $tracks(artist:$t_no) / tracks(artist:$t_no); + regsub -- {\\} $tracks(title:$t_no) / tracks(title:$t_no); + regsub -- \" $tracks(artist:$t_no) \\\" tracks(artist:$t_no); + regsub -- \" $tracks(title:$t_no) \\\" tracks(title:$t_no); + set oldname [ eval format {$oldbasename} {$t_no} ]; + set newname [ eval format {$newbasename} {"$tracks(artist:$t_no) - $tracks(title:$t_no)"} ]; + + puts $out "mp3info -a \"$tracks(artist:$t_no)\" -t \"$tracks(title:$t_no)\" -l \"$tracks(albumname)\" -c \"Track $t_no\" -F 4 \"$oldname\""; + puts $out "mv \"$oldname\" \"$dirname/$newname\""; + } + + if {[catch {close $out} msg]} { + puts "Got $msg closing renamer"; + } + + if {[info exists fh]} { + if {[catch {close $fh} msg]} { + puts "Got $msg closing fh"; + } + } +} + +proc print_tracks {outtitle} { + global tracks; + + puts "================================================================================"; + puts "$outtitle"; + puts "================================================================================"; + puts "Title: $tracks(albumname)" + if { $tracks(albumartist) != "" } { + puts "Artist: $tracks(albumartist)"; + } + for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } { + puts [format "Track %d: %s - %s" $t_no $tracks(artist:$t_no) $tracks(title:$t_no)]; + } + + puts "================================================================================"; +} + +main;