annotate get_cdinfo.tcl @ 11:c122160e07da

Use Tcl 8.2
author darius
date Thu, 18 Jul 2002 06:38:42 +0000
parents f3f2657296d2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
74031379d3cb Initial revision
darius
parents:
diff changeset
1 #!/bin/sh
74031379d3cb Initial revision
darius
parents:
diff changeset
2 # tcl magic \
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
3 exec tclsh8.2 $0 $*
3
74031379d3cb Initial revision
darius
parents:
diff changeset
4
74031379d3cb Initial revision
darius
parents:
diff changeset
5 #
74031379d3cb Initial revision
darius
parents:
diff changeset
6 # This software is copyright Daniel O'Connor (darius@dons.net.au) 1998, 1999
74031379d3cb Initial revision
darius
parents:
diff changeset
7 #
74031379d3cb Initial revision
darius
parents:
diff changeset
8 # This software is release under the GNU Public License Version 2.
74031379d3cb Initial revision
darius
parents:
diff changeset
9 # A copy of this licence must be distributed with this software.
74031379d3cb Initial revision
darius
parents:
diff changeset
10 #
74031379d3cb Initial revision
darius
parents:
diff changeset
11
74031379d3cb Initial revision
darius
parents:
diff changeset
12 proc main {} {
74031379d3cb Initial revision
darius
parents:
diff changeset
13 global argv0 argv tracks auto_path;
74031379d3cb Initial revision
darius
parents:
diff changeset
14
74031379d3cb Initial revision
darius
parents:
diff changeset
15 lappend auto_path ".";
74031379d3cb Initial revision
darius
parents:
diff changeset
16
74031379d3cb Initial revision
darius
parents:
diff changeset
17 if { [ llength $argv ] < 4 } {
74031379d3cb Initial revision
darius
parents:
diff changeset
18 puts stderr "Bad usage";
74031379d3cb Initial revision
darius
parents:
diff changeset
19 puts stderr "$argv0 <outfile> <discid> \[ <tracks> ... \] <disclen>";
74031379d3cb Initial revision
darius
parents:
diff changeset
20 exit 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
21 }
74031379d3cb Initial revision
darius
parents:
diff changeset
22 set outfile [ lindex $argv 0 ];
74031379d3cb Initial revision
darius
parents:
diff changeset
23 set discid [ lindex $argv 1 ];
74031379d3cb Initial revision
darius
parents:
diff changeset
24 set trackoffs [ lrange $argv 2 [ expr [ llength $argv ] - 2 ] ];
74031379d3cb Initial revision
darius
parents:
diff changeset
25 set disclen [ lindex $argv end ];
74031379d3cb Initial revision
darius
parents:
diff changeset
26
74031379d3cb Initial revision
darius
parents:
diff changeset
27 if { $outfile == "-" } {
74031379d3cb Initial revision
darius
parents:
diff changeset
28 set wfh stdout;
74031379d3cb Initial revision
darius
parents:
diff changeset
29 } else {
74031379d3cb Initial revision
darius
parents:
diff changeset
30 set wfh [ open $outfile "w" ];
74031379d3cb Initial revision
darius
parents:
diff changeset
31 }
74031379d3cb Initial revision
darius
parents:
diff changeset
32
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
33 # set fh [ socket cddb.cddb.com 8880 ];
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
34 if {[catch {set fh [socket freedb.freedb.org 888]} msg]} {
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
35 puts stderr "Unable to connect to CDDB - $msg";
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
36 puts stderr "Generating dummy file";
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
37 generate_dummy $wfh $discid $disclen $trackoffs ;
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
38 exit 0;
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
39 }
3
74031379d3cb Initial revision
darius
parents:
diff changeset
40
74031379d3cb Initial revision
darius
parents:
diff changeset
41 # Greeting from server
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
42 puts [ gets $fh ];
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
43 #gets $fh;
3
74031379d3cb Initial revision
darius
parents:
diff changeset
44
74031379d3cb Initial revision
darius
parents:
diff changeset
45 puts $fh "CDDB HELLO [ exec id -u -n ] [ exec hostname ] TclMangler 0.1";
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
46 puts "CDDB HELLO [ exec id -u -n ] [ exec hostname ] TclMangler 0.1";
3
74031379d3cb Initial revision
darius
parents:
diff changeset
47 flush $fh
74031379d3cb Initial revision
darius
parents:
diff changeset
48
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
49 puts "Said hello"
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
50
3
74031379d3cb Initial revision
darius
parents:
diff changeset
51 # Hello message
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
52 # puts [ gets $fh ];
3
74031379d3cb Initial revision
darius
parents:
diff changeset
53 gets $fh;
74031379d3cb Initial revision
darius
parents:
diff changeset
54
74031379d3cb Initial revision
darius
parents:
diff changeset
55 puts $fh "CDDB QUERY $discid [ llength $trackoffs ] $trackoffs $disclen"
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
56 puts "CDDB QUERY $discid [ llength $trackoffs ] $trackoffs $disclen"
3
74031379d3cb Initial revision
darius
parents:
diff changeset
57 flush $fh
74031379d3cb Initial revision
darius
parents:
diff changeset
58
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
59 puts "Queried"
3
74031379d3cb Initial revision
darius
parents:
diff changeset
60 set line [ gets $fh ];
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
61 puts "Got - $line"
3
74031379d3cb Initial revision
darius
parents:
diff changeset
62 if { [ regexp {([0-9][0-9][0-9]) (.*)} $line a rtn rest ] } {
74031379d3cb Initial revision
darius
parents:
diff changeset
63 switch -- $rtn {
74031379d3cb Initial revision
darius
parents:
diff changeset
64 "200" {
74031379d3cb Initial revision
darius
parents:
diff changeset
65 if { [ regexp {([a-z]*) ([0-9a-f]*) (.*)} $rest a cat discid name ] } {
74031379d3cb Initial revision
darius
parents:
diff changeset
66 puts stderr "Matched CD called $name in category $cat";
74031379d3cb Initial revision
darius
parents:
diff changeset
67 } else {
74031379d3cb Initial revision
darius
parents:
diff changeset
68 puts stderr "Couldn't parse line with code 200 '$rest'";
74031379d3cb Initial revision
darius
parents:
diff changeset
69 exit 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
70 }
74031379d3cb Initial revision
darius
parents:
diff changeset
71 }
74031379d3cb Initial revision
darius
parents:
diff changeset
72
74031379d3cb Initial revision
darius
parents:
diff changeset
73 "202" {
74031379d3cb Initial revision
darius
parents:
diff changeset
74 puts stderr "No such CD found.. generating dummy file";
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
75 generate_dummy $wfh $discid $disclen $trackoffs ;
3
74031379d3cb Initial revision
darius
parents:
diff changeset
76
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
77 puts "Wrote dummy";
3
74031379d3cb Initial revision
darius
parents:
diff changeset
78 exit;
74031379d3cb Initial revision
darius
parents:
diff changeset
79 }
74031379d3cb Initial revision
darius
parents:
diff changeset
80
74031379d3cb Initial revision
darius
parents:
diff changeset
81 "211" {
74031379d3cb Initial revision
darius
parents:
diff changeset
82 set tot 0;
74031379d3cb Initial revision
darius
parents:
diff changeset
83 set matches "";
74031379d3cb Initial revision
darius
parents:
diff changeset
84
74031379d3cb Initial revision
darius
parents:
diff changeset
85 while { 1 } {
74031379d3cb Initial revision
darius
parents:
diff changeset
86 set line [ gets $fh ];
74031379d3cb Initial revision
darius
parents:
diff changeset
87 if { $line == "." } {
74031379d3cb Initial revision
darius
parents:
diff changeset
88 break;
74031379d3cb Initial revision
darius
parents:
diff changeset
89 }
74031379d3cb Initial revision
darius
parents:
diff changeset
90
74031379d3cb Initial revision
darius
parents:
diff changeset
91 if { [ regexp {^([a-z]*) ([0-9a-f]*) (.*)} $line a cat discid name ] } {
74031379d3cb Initial revision
darius
parents:
diff changeset
92 puts stderr "[ expr $tot + 1 ]) Matched CD called $name in category $cat";
74031379d3cb Initial revision
darius
parents:
diff changeset
93 lappend matches [ list $cat $discid $name ];
74031379d3cb Initial revision
darius
parents:
diff changeset
94 incr tot;
74031379d3cb Initial revision
darius
parents:
diff changeset
95 } else {
74031379d3cb Initial revision
darius
parents:
diff changeset
96 puts stderr "Couldn't parse line after code 210 - '$line'";
74031379d3cb Initial revision
darius
parents:
diff changeset
97 exit 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
98 }
74031379d3cb Initial revision
darius
parents:
diff changeset
99 }
74031379d3cb Initial revision
darius
parents:
diff changeset
100
74031379d3cb Initial revision
darius
parents:
diff changeset
101 if { $tot == 0 } {
74031379d3cb Initial revision
darius
parents:
diff changeset
102 puts stderr "No matches found for 210?";
74031379d3cb Initial revision
darius
parents:
diff changeset
103 exit 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
104 }
74031379d3cb Initial revision
darius
parents:
diff changeset
105
74031379d3cb Initial revision
darius
parents:
diff changeset
106 if { $tot == 1 } {
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
107 puts "Only 1 partial match";
3
74031379d3cb Initial revision
darius
parents:
diff changeset
108 set num 0;
74031379d3cb Initial revision
darius
parents:
diff changeset
109 } else {
74031379d3cb Initial revision
darius
parents:
diff changeset
110 puts stderr "Please enter the number which corresponds to the correct entry";
74031379d3cb Initial revision
darius
parents:
diff changeset
111 while { 1 } {
74031379d3cb Initial revision
darius
parents:
diff changeset
112 set num [ gets stdin ];
74031379d3cb Initial revision
darius
parents:
diff changeset
113 if { ($num >= 1) && ($num <= $tot ) } {
74031379d3cb Initial revision
darius
parents:
diff changeset
114 break;
74031379d3cb Initial revision
darius
parents:
diff changeset
115 }
74031379d3cb Initial revision
darius
parents:
diff changeset
116 puts stderr "Sorry, that number is invalid, please try again";
74031379d3cb Initial revision
darius
parents:
diff changeset
117 }
74031379d3cb Initial revision
darius
parents:
diff changeset
118
74031379d3cb Initial revision
darius
parents:
diff changeset
119 set cat [ lindex [ lindex $matches [ expr $num - 1 ] ] 0 ];
74031379d3cb Initial revision
darius
parents:
diff changeset
120 set discid [ lindex [ lindex $matches [ expr $num - 1 ] ] 1 ];
74031379d3cb Initial revision
darius
parents:
diff changeset
121 set name [ lindex [ lindex $matches [ expr $num - 1 ] ] 2 ];
74031379d3cb Initial revision
darius
parents:
diff changeset
122 }
74031379d3cb Initial revision
darius
parents:
diff changeset
123 }
74031379d3cb Initial revision
darius
parents:
diff changeset
124
74031379d3cb Initial revision
darius
parents:
diff changeset
125 "501" {
74031379d3cb Initial revision
darius
parents:
diff changeset
126 puts stderr"Invalid disc ID $discid";
74031379d3cb Initial revision
darius
parents:
diff changeset
127 exit 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
128 }
74031379d3cb Initial revision
darius
parents:
diff changeset
129
74031379d3cb Initial revision
darius
parents:
diff changeset
130 default {
74031379d3cb Initial revision
darius
parents:
diff changeset
131 puts stderr "Couldn't parse '$line'";
74031379d3cb Initial revision
darius
parents:
diff changeset
132 exit 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
133 }
74031379d3cb Initial revision
darius
parents:
diff changeset
134 }
74031379d3cb Initial revision
darius
parents:
diff changeset
135 }
74031379d3cb Initial revision
darius
parents:
diff changeset
136
74031379d3cb Initial revision
darius
parents:
diff changeset
137 puts $fh "CDDB READ $cat $discid"
74031379d3cb Initial revision
darius
parents:
diff changeset
138 flush $fh
74031379d3cb Initial revision
darius
parents:
diff changeset
139
74031379d3cb Initial revision
darius
parents:
diff changeset
140 gets $fh line;
74031379d3cb Initial revision
darius
parents:
diff changeset
141
74031379d3cb Initial revision
darius
parents:
diff changeset
142 if { [ regexp {([0-9][0-9][0-9]) (.*)} $line a rtn rest ] } {
74031379d3cb Initial revision
darius
parents:
diff changeset
143 if { $rtn != "210" } {
74031379d3cb Initial revision
darius
parents:
diff changeset
144 puts stderr "Bad error from CDDB READ command ($line)"
74031379d3cb Initial revision
darius
parents:
diff changeset
145 exit 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
146 }
74031379d3cb Initial revision
darius
parents:
diff changeset
147 } else {
74031379d3cb Initial revision
darius
parents:
diff changeset
148 puts stderr "Couldn't parse $line";
74031379d3cb Initial revision
darius
parents:
diff changeset
149 exit 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
150 }
74031379d3cb Initial revision
darius
parents:
diff changeset
151
74031379d3cb Initial revision
darius
parents:
diff changeset
152 while { 1 } {
74031379d3cb Initial revision
darius
parents:
diff changeset
153 set line [ gets $fh ];
74031379d3cb Initial revision
darius
parents:
diff changeset
154 if { $line == "." } {
74031379d3cb Initial revision
darius
parents:
diff changeset
155 break;
74031379d3cb Initial revision
darius
parents:
diff changeset
156 }
74031379d3cb Initial revision
darius
parents:
diff changeset
157
74031379d3cb Initial revision
darius
parents:
diff changeset
158 puts $wfh $line;
74031379d3cb Initial revision
darius
parents:
diff changeset
159 }
74031379d3cb Initial revision
darius
parents:
diff changeset
160
74031379d3cb Initial revision
darius
parents:
diff changeset
161 close $fh;
74031379d3cb Initial revision
darius
parents:
diff changeset
162 }
74031379d3cb Initial revision
darius
parents:
diff changeset
163
9
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
164 proc generate_dummy {fh discid disclen trackoffs} {
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
165
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
166 puts $fh "# xmcd CD database file
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
167 # Copyright (C) 1993-1999 CDDB, Inc.
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
168 #
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
169 # Track frame offsets:";
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
170 foreach t $trackoffs {
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
171 puts $fh "#\t$t";
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
172 }
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
173
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
174 puts $fh "#
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
175 # Disc length: $disclen seconds
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
176 #
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
177 # Revision: 1
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
178 # Submitted via: Tcl Mangler 0.1 - Copyright (c) 1999 Daniel O'Connor
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
179 #
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
180 DISCID=$discid
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
181 DTITLE=";
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
182 set i 0;
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
183 foreach t $trackoffs {
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
184 puts $fh "TTITLE$i=";
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
185 incr i
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
186 }
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
187 }
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
188
f3f2657296d2 Use Tcl 8.2
darius
parents: 3
diff changeset
189 main;