Mercurial > ~darius > hgwebdir.cgi > cddb-stuff
view get_cdinfo.tcl @ 11:c122160e07da
Use Tcl 8.2
author | darius |
---|---|
date | Thu, 18 Jul 2002 06:38:42 +0000 |
parents | f3f2657296d2 |
children |
line wrap: on
line source
#!/bin/sh # tcl magic \ exec tclsh8.2 $0 $* # # 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 "."; if { [ llength $argv ] < 4 } { puts stderr "Bad usage"; puts stderr "$argv0 <outfile> <discid> \[ <tracks> ... \] <disclen>"; exit 1; } set outfile [ lindex $argv 0 ]; set discid [ lindex $argv 1 ]; set trackoffs [ lrange $argv 2 [ expr [ llength $argv ] - 2 ] ]; set disclen [ lindex $argv end ]; if { $outfile == "-" } { set wfh stdout; } else { set wfh [ open $outfile "w" ]; } # set fh [ socket cddb.cddb.com 8880 ]; if {[catch {set fh [socket freedb.freedb.org 888]} msg]} { puts stderr "Unable to connect to CDDB - $msg"; puts stderr "Generating dummy file"; generate_dummy $wfh $discid $disclen $trackoffs ; exit 0; } # Greeting from server puts [ gets $fh ]; #gets $fh; puts $fh "CDDB HELLO [ exec id -u -n ] [ exec hostname ] TclMangler 0.1"; puts "CDDB HELLO [ exec id -u -n ] [ exec hostname ] TclMangler 0.1"; flush $fh puts "Said hello" # Hello message # puts [ gets $fh ]; gets $fh; puts $fh "CDDB QUERY $discid [ llength $trackoffs ] $trackoffs $disclen" puts "CDDB QUERY $discid [ llength $trackoffs ] $trackoffs $disclen" flush $fh puts "Queried" set line [ gets $fh ]; puts "Got - $line" if { [ regexp {([0-9][0-9][0-9]) (.*)} $line a rtn rest ] } { switch -- $rtn { "200" { if { [ regexp {([a-z]*) ([0-9a-f]*) (.*)} $rest a cat discid name ] } { puts stderr "Matched CD called $name in category $cat"; } else { puts stderr "Couldn't parse line with code 200 '$rest'"; exit 1; } } "202" { puts stderr "No such CD found.. generating dummy file"; generate_dummy $wfh $discid $disclen $trackoffs ; puts "Wrote dummy"; exit; } "211" { set tot 0; set matches ""; while { 1 } { set line [ gets $fh ]; if { $line == "." } { break; } if { [ regexp {^([a-z]*) ([0-9a-f]*) (.*)} $line a cat discid name ] } { puts stderr "[ expr $tot + 1 ]) Matched CD called $name in category $cat"; lappend matches [ list $cat $discid $name ]; incr tot; } else { puts stderr "Couldn't parse line after code 210 - '$line'"; exit 1; } } if { $tot == 0 } { puts stderr "No matches found for 210?"; exit 1; } if { $tot == 1 } { puts "Only 1 partial match"; set num 0; } else { puts stderr "Please enter the number which corresponds to the correct entry"; while { 1 } { set num [ gets stdin ]; if { ($num >= 1) && ($num <= $tot ) } { break; } puts stderr "Sorry, that number is invalid, please try again"; } set cat [ lindex [ lindex $matches [ expr $num - 1 ] ] 0 ]; set discid [ lindex [ lindex $matches [ expr $num - 1 ] ] 1 ]; set name [ lindex [ lindex $matches [ expr $num - 1 ] ] 2 ]; } } "501" { puts stderr"Invalid disc ID $discid"; exit 1; } default { puts stderr "Couldn't parse '$line'"; exit 1; } } } puts $fh "CDDB READ $cat $discid" flush $fh gets $fh line; if { [ regexp {([0-9][0-9][0-9]) (.*)} $line a rtn rest ] } { if { $rtn != "210" } { puts stderr "Bad error from CDDB READ command ($line)" exit 1; } } else { puts stderr "Couldn't parse $line"; exit 1; } while { 1 } { set line [ gets $fh ]; if { $line == "." } { break; } puts $wfh $line; } close $fh; } proc generate_dummy {fh discid disclen trackoffs} { puts $fh "# xmcd CD database file # Copyright (C) 1993-1999 CDDB, Inc. # # Track frame offsets:"; foreach t $trackoffs { puts $fh "#\t$t"; } puts $fh "# # Disc length: $disclen seconds # # Revision: 1 # Submitted via: Tcl Mangler 0.1 - Copyright (c) 1999 Daniel O'Connor # DISCID=$discid DTITLE="; set i 0; foreach t $trackoffs { puts $fh "TTITLE$i="; incr i } } main;