comparison cddb2.tcl @ 1:dd6e30f7eb42

Initial revision
author darius
date Wed, 09 Aug 2000 02:18:46 +0000
parents
children f5daa3cdeb58
comparison
equal deleted inserted replaced
0:4a73fc4d9c78 1:dd6e30f7eb42
1 #!/usr/local/bin/tclsh8.2
2
3 #
4 # This software is copyright Daniel O'Connor (darius@dons.net.au) 1998, 1999
5 #
6 # This software is release under the GNU Public License Version 2.
7 # A copy of this licence must be distributed with this software.
8 #
9
10 proc main {} {
11 global argv0 argv tracks auto_path;
12
13 lappend auto_path "[file dirname $argv0]";
14 lappend auto_path ".";
15
16 if { [ catch {loadlibs [ list tk82 ]} msg] } {
17 log "Failed to load library files - $msg";
18 exit 1;
19 }
20 # puts $auto_path
21
22 if { [ llength $argv ] != 4 } {
23 puts "Bad usage";
24 puts "$argv0 <shellfile> <oldbasename> <newbasename> <infile>";
25 exit;
26 }
27
28 set shellfile [ lindex $argv 0 ];
29 set oldbasename [ lindex $argv 1 ];
30 set newbasename [ lindex $argv 2 ];
31 set infile [ lindex $argv 3 ]
32
33 if {[file exists $shellfile] && ![file writable $shellfile]} {
34 puts "$shellfile not writeable, aborting";
35 exit 1;
36 }
37
38 set tracks(albumname) "";
39 set tracks(albumartist) "";
40 set tracks(number) 0;
41
42 set dtitle "";
43 set dartist "";
44
45 # If the file doesn't exist we assume the user wants to enter new data
46 if {![file exists $infile] && $infile != "-"} {
47 puts "Please enter the number of tracks on the CD..."
48 while { 1 } {
49 puts -nonewline "-> ";
50 flush stdout;
51 set tracks(number) [ gets stdin ];
52 if { ($tracks(number) >= 1) && ($tracks(number) <= 100) } {
53 break;
54 }
55 puts "Please enter a number between 1 and 100";
56 }
57
58 set tracks(disclen) 1;
59 set tracks(discid) 1;
60
61 for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } {
62 set tracks(artist:$t_no) "";
63 set tracks(title:$t_no) "";
64 set tracks(offset:$t_no) 1;
65 }
66
67 } else {
68 if { $infile == "-" } {
69 set fh stdin;
70 } else {
71 set fh [ open $infile "r" ];
72 }
73
74 set offset_seek 0;
75 set offset_list "";
76
77 while {![eof $fh]} {
78 set line [gets $fh];
79 # puts "Line = $line";
80 if {$offset_seek > 0} {
81 if {$line == "\#"} {
82 # puts "Comment skip"
83 continue;
84 }
85
86 if {[ regexp {^\#[ ]+([0-9]+)} $line a offset] == 1} {
87 puts "Adding $offset";
88 lappend offset_list $offset;
89 continue;
90 }
91
92 # Change back to default state
93 puts "Not looking for offsets";
94 set offset_seek 0;
95 }
96
97 if {[regexp {^\# Track frame offsets:} $line] == 1} {
98 # Change to looking for offsets state
99 puts "Looking for offsets";
100 set offset_seek 1;
101 }
102
103 if {[regexp {^\# Disc length: +([0-9]+)} $line a tracks(disclen)] == 1} {
104 continue;
105 }
106
107 if {[regexp {([A-Z0-9]*)=(.*)} $line a key value] == 0} {
108 continue;
109 }
110
111 switch -regexp -- $key {
112 "DISCID" {
113 set tracks(discid) $value;
114 # puts "Got ID as $value";
115 }
116
117 "DTITLE" {
118 if { [ regexp {^(.*) / (.*)$} $value a tracks(albumartist) tracks(albumname)] == 1 } {
119 # puts "Got disc artist as $dartist, title as $dtitle";
120 ;
121 } else {
122 set tracks(albumname) $value;
123 # puts "Got title as $value";
124 }
125 }
126
127 "TTITLE([0-9]+)" {
128 regexp {TTITLE([0-9]+)} $key a t_no;
129 set t_no [ expr $t_no + 1 ];
130
131 incr tracks(number);
132
133 if {![regexp {^(.*) - (.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} {
134 if {![regexp {^(.*)--(.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} {
135 set tracks(artist:$t_no) $value;
136 }
137 }
138 }
139
140 "EXTT([0-9]+)" {
141 regexp {EXTT([0-9]+)} $key a t_no;
142 set t_no [ expr $t_no + 1 ];
143 set ext($t_no) $value;
144 }
145 }
146 }
147
148 # puts "Offset list - $offset_list";
149 # puts "Disclength - $tracks(disclen)";
150
151 if {$tracks(number) < 1 || [llength $offset_list] != $tracks(number)} {
152 puts stderr "Invalid file, $tracks(number)";
153 exit 1;
154 }
155
156 set i 1;
157 foreach t $offset_list {
158 set tracks(offset:$i) $t;
159 incr i;
160 }
161
162 for {set i 1} {$i <= $tracks(number)} {incr i} {
163 if {![info exists tracks(title:$i)]} {
164 error "track $i has no title!";
165 }
166
167 if {[info exists tracks(artist:$i)]} {
168 # puts "artist exists for $i";
169 continue;
170 }
171
172 if {$dartist != ""} {
173 set tracks(artist:$i) $tracks(albumartist);
174 # puts "setting artists to $dartist for $i (disc artist)";
175 continue;
176 }
177
178 if {[info exists ext($i)] && $ext($i) != ""} {
179 set tracks(artist:$i) $ext($i);
180 # puts "setting artist to $ext($i) for $i";
181 continue;
182 }
183
184 set t_artist($i) "Unknown";
185 }
186
187 # puts "$tracks(number) tracks";
188
189 }
190
191 print_tracks "Before editing";
192 set tracks(updated) 0;
193 edit_tracks;
194 print_tracks "After editing";
195
196 if {$tracks(updated) == 0} {
197 puts "Nothing saved aborting";
198 puts "";
199 exit 0;
200 }
201
202 set out [ open $shellfile "w" ];
203
204 if { [ catch {
205 if {$infile == "-"} {
206 set fh stdout;
207 } else {
208 file rename -force "$infile" "$infile.old"
209 set fh [ open "$infile" "w" ];
210 }
211
212 puts $fh "# xmcd CD database file
213 # Copyright (C) 1993-1999 CDDB, Inc.
214 #
215 # Track frame offsets:";
216 for { set i 1 } { $i <= $tracks(number) } { incr i } {
217 puts $fh "#\t$tracks(offset:$i)"
218 }
219 puts $fh "#
220 # Disc length: $tracks(disclen) seconds
221 #
222 # Revision: 1
223 # Submitted via: Tcl Mangler 0.1 - Copyright (c) 1999 Daniel O'Connor
224 #
225 DISCID=$tracks(discid)
226 DTITLE=$tracks(albumname)";
227 for { set i 1 } { $i <= $tracks(number) } { incr i } {
228 puts $fh "TTITLE[expr $i - 1]=$tracks(artist:$i) - $tracks(title:$i)";
229 }
230 } msg ] } {
231 puts "Failed to write $infile, $msg";
232 }
233
234 # Eat illegal characters
235 regsub {\\} $tracks(albumname) / tracks(albumname)
236 regsub \" $tracks(albumname) \\\" tracks(albumname)
237
238 if { $dartist != "" } {
239 set dirname "$dartist - $tracks(albumname)";
240 } else {
241 set dirname "Various Artists - $tracks(albumname)";
242 }
243
244 puts $out "mkdir \"$dirname\"";
245 for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } {
246 # Eat illegal characters
247 regsub -- {\\} $tracks(artist:$t_no) / tracks(artist:$t_no);
248 regsub -- {\\} $tracks(title:$t_no) / tracks(title:$t_no);
249 regsub -- \" $tracks(artist:$t_no) \\\" tracks(artist:$t_no);
250 regsub -- \" $tracks(title:$t_no) \\\" tracks(title:$t_no);
251 set oldname [ eval format {$oldbasename} {$t_no} ];
252 set newname [ eval format {$newbasename} {"$tracks(artist:$t_no) - $tracks(title:$t_no)"} ];
253
254 puts $out "mp3info -a \"$tracks(artist:$t_no)\" -t \"$tracks(title:$t_no)\" -l \"$tracks(albumname)\" -c \"Track $t_no\" -F 4 \"$oldname\"";
255 puts $out "mv \"$oldname\" \"$dirname/$newname\"";
256 }
257
258 if {[catch {close $out} msg]} {
259 puts "Got $msg closing renamer";
260 }
261
262 if {[info exists fh]} {
263 if {[catch {close $fh} msg]} {
264 puts "Got $msg closing fh";
265 }
266 }
267 }
268
269 proc print_tracks {outtitle} {
270 global tracks;
271
272 puts "================================================================================";
273 puts "$outtitle";
274 puts "================================================================================";
275 puts "Title: $tracks(albumname)"
276 if { $tracks(albumartist) != "" } {
277 puts "Artist: $tracks(albumartist)";
278 }
279 for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } {
280 puts [format "Track %d: %s - %s" $t_no $tracks(artist:$t_no) $tracks(title:$t_no)];
281 }
282
283 puts "================================================================================";
284 }
285
286 main;