Mercurial > ~darius > hgwebdir.cgi > cddb-stuff
view cddb2.tcl @ 6:f5daa3cdeb58
- Clear up old variable usage.
- Grok Artist/Title in TTITLE fields.
- Add the ability to append to track title fields.
author | darius |
---|---|
date | Sat, 12 Aug 2000 07:12:12 +0000 |
parents | dd6e30f7eb42 |
children | 2c01c09bdc0f |
line wrap: on
line source
#!/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; # 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} { set tracks(albumname) $value; } else { puts "Atrist - $tracks(albumartist)"; } puts "Title - $tracks(albumname)"; } "TTITLE([0-9]+)" { regexp {TTITLE([0-9]+)} $key a t_no; set t_no [ expr $t_no + 1 ]; if {[info exists tracks(title:$t_no)]} { append tracks(title:$t_no) $value; } else { 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} { if {![regexp {^(.*) / (.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} { set tracks(title:$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!"; set tracks(title:$i) ""; } if {[info exists tracks(artist:$i)]} { # puts "artist exists for $i"; continue; } if {$tracks(albumartist) != ""} { set tracks(artist:$i) $tracks(albumartist); 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"; } if {$tracks(albumartist) == ""} { set tracks(albumartist) "Various Artists"; } 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(albumartist) - $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 quoter tracks(albumname); quoter tracks(albumartist); set dirname "$tracks(albumartist) - $tracks(albumname)"; puts $out "mkdir \"$dirname\""; for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } { # Eat illegal characters quoter tracks(artist:$t_no); quoter 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 quoter {str_var} { upvar $str_var str; regsub -all -- {/} $str {\\} str; regsub -all -- {\"} $str {\"} str; # regsub -all -- {\'} $str {\'} str; } 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;