diff get_cdinfo.tcl @ 3:74031379d3cb CDDB-STUFF_1_0

Initial revision
author darius
date Wed, 09 Aug 2000 02:18:47 +0000
parents
children f3f2657296d2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/get_cdinfo.tcl	Wed Aug 09 02:18:47 2000 +0000
@@ -0,0 +1,169 @@
+#!/bin/sh
+# tcl magic \
+exec tclsh8.0 $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 ];
+
+    # Greeting from server
+    #puts [ gets $fh ];
+    gets $fh;
+
+    puts $fh "CDDB HELLO [ exec id -u -n ] [ exec hostname ] TclMangler 0.1";
+    flush $fh
+
+    # Hello message
+    #puts [ gets $fh ];
+    gets $fh;
+
+    puts $fh "CDDB QUERY $discid [ llength $trackoffs ] $trackoffs $disclen"
+    flush $fh 
+
+    set line [ gets $fh ];
+    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";
+		puts $wfh "# xmcd CD database file
+# Copyright (C) 1993-1999 CDDB, Inc.
+#
+# Track frame offsets:";
+		foreach t $trackoffs {
+		    puts $wfh "#\t$t";
+		}
+		puts $wfh "#
+# 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 $wfh "TTITLE$i=";
+		    incr i
+		}
+
+		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 } {
+		    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;
+}
+
+main;
\ No newline at end of file