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