lib/cddb.pl untested

filter: Statementstmt 0.0% 0 / 260 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 428 CC 11 SCAR 29.5
CC
11
Cov
0%
print_xmcd
42.0
print_cd
20.0
insert_db
12.0
CRAP 19.1
1 #!/usr/bin/perl -I.
2 #
3 # CDDB - Read the CDDB entry for an audio CD in your drive
4 #
5 # This module/script gets the CDDB info for an audio cd. You need
6 # LINUX, a cdrom drive and an active internet connection in order
7 # to do that.
8 #
9 # (c) 2004 Armin Obersteiner <armin@xos.net>
10 #
11 # LICENSE
12 #
13 # This library is released under the same conditions as Perl, that
14 # is, either of the following:
15 #
16 # a) the GNU General Public License Version 2 as published by the
17 # Free Software Foundation,
18 #
19 # b) the Artistic License.
20 #
21  
22 #use CDDB_get qw( get_cddb get_discids );
23  
24 use Data::Dumper;
25 use Getopt::Std;
26  
27 use strict;
28  
29 my %option = ();
30 getopts("oghdtsi:SfDlOFc:H:CIRGP", \%option);
31  
32 if($option{h}) {
33 print "$0: gets CDDB info of a CD\n";
34 print " no argument - gets CDDB info of CD in your drive\n";
35 print " -c device (other than default device)\n";
36 print " -o offline mode - just stores CD info\n";
37 print " -d output in xmcd format\n";
38 print " -s save in xmcd format\n";
39 print " -i db. one of: mysql, pg, oracle, sqlite\n";
40 print " -O overwrite file or db\n";
41 print " -t output toc\n";
42 print " -l output lame command\n";
43 print " -f http mode (e.g. through firewalls)\n";
44 print " -F some stateful firewalls/http proxies need additional newlines\n";
45 print " -g get CDDB info for stored CDs\n";
46 print " -I non interactive mode\n";
47 print " -H CDDB hostname\n";
48 print " -C use local cache\n";
49 print " -R readonly cache\n";
50 print " -G cache has not the diskid as filenames (much slower)\n";
51 print " -P cache path (default: /tmp/xmcd)\n";
52 print " -D put CDDB_get in debug mode\n";
53 exit;
54 }
55  
56 my %config;
57  
58 my $diskid;
59 my $total;
60 my $toc;
61 my $savedir="/tmp/cddb";
62 my $xmcddir="/tmp/xmcd";
63  
64 if($option{C}) {
65 # use CDDB_cache qw( get_cddb get_discids );
66 require CDDB_cache;
67 CDDB_cache->import( qw( get_cddb get_discids ) );
68  
69 $CDDB_cache::debug=1 if($option{D});
70 $CDDB_cache::readonly=1 if($option{R});
71 $CDDB_cache::grep=1 if($option{G});
72  
73 $CDDB_cache::dir="/tmp/xmcd"; # default
74 # $CDDB_cache::dir="/opt/kde2/share/apps/kscd/cddb";
75 $CDDB_cache::dir=$option{P} if($option{P});
76  
77 } else {
78 # use CDDB_get qw( get_cddb get_discids );
79 require CDDB_get;
80 CDDB_get->import( qw( get_cddb get_discids ) );
81 }
82  
83 $CDDB_get::debug=1 if($option{D});
84  
85 # following variables just need to be declared if different from defaults
86 # defaults are listed below (cdrom default is os specific)
87  
88 # $config{CDDB_HOST}="freedb.freedb.org"; # set cddb host
89 if($option{H}) {
90 $config{CDDB_HOST}=$option{H};
91 }
92 # $config{CDDB_PORT}=8880; # set cddb port
93 # $config{CDDB_MODE}="cddb"; # set cddb mode: cddb or http, this is switched with -f
94 # $config{CD_DEVICE}="/dev/cdrom"; # set cd device
95  
96 # $config{HELLO_ID} ="root nowhere.com fastrip 0.77"; # hello string: username hostname clientname version
97 # $config{PROTO_VERSION} = 5; # cddb protokol version
98  
99  
100 # get proxy settings for cddb mode
101  
102 $config{HTTP_PROXY}=$ENV{http_proxy} if $ENV{http_proxy}; # maybe wanna use a proxy ?
103  
104 $config{CDDB_MODE}="http" if($option{f});
105 if($option{F}) {
106 $config{CDDB_MODE}="http";
107 $config{FW}=1;
108 }
109  
110 $config{CD_DEVICE}=$option{c} if $option{c};
111  
112 # user interaction welcome?
113  
114 $config{input}=1; # 1: ask user if more than one possibility
115 # 0: no user interaction
116 $config{multi}=0; # 1: do not ask user and get all of them
117 # 0: just the first one
118  
119 $config{input}=0 if($option{I});
120  
121 my %db;
122  
123 if($option{i}) {
124 require DBI;
125  
126 $db{table_cds} = "cds";
127 $db{table_tracks} = "tracks";
128  
129 # not needed for sqlite
130 $db{host} = "localhost";
131 $db{port} = "3306";
132  
133 # not needed for oracle/sqlite
134 $db{name} = "mp3-test";
135  
136 # just for oracle
137 $db{sid} = "xxx";
138 $db{home} = "xxx";
139  
140 # just for sqlite
141 $db{file} = "xxx";
142  
143 # not needed for sqlite
144 $db{user} = "root";
145 $db{passwd} = "xxx";
146  
147  
148 if($option{i} eq "mysql") {
149 $db{connect} = sub { "dbi:mysql:database=$db{name};host=$db{host};port=$db{port}", $db{user}, $db{passwd} };
150 } elsif($option{i} eq "pg") {
151 $db{connect} = sub { "dbi:Pg:dbname=$db{dbname};host=$db{host};port=$db{port}", $db{user}, $db{passwd} };
152 } elsif($option{i} eq "oracle") {
153 $db{connect} = sub { "dbi:Oracle:host=$db{host};sid=$db{sid};port=$db{port}", $db{user}, $db{passwd} };
154 $ENV{ORACLE_HOME} = $db{home};
155 } elsif($option{i} eq "sqlite") {
156 $db{connect} = sub { "dbi:SQLite:dbname=$db{file}","","" };
157 } else {
158 die "unkown database: $option{i}";
159 }
160 }
161
162 if($option{o}) {
163 my $ids=get_discids($config{CD_DEVICE});
164  
165 unless(-e $savedir) {
166 mkdir $savedir,0755 or die "cannot create $savedir";
167 }
168  
169 open OUT,">$savedir/$ids->[0]\_$$" or die "cannot open outfile";
170 print OUT Data::Dumper->Dump($ids,["diskid","total","toc"]);
171 close OUT;
172  
173 print STDERR "saved in: $savedir/$ids->[0]\_$$\n";
174 exit;
175 }
176  
177 if($option{g}) {
178 print STDERR "retrieving stored cds ...\n";
179  
180 opendir(DIR, $savedir) or die "cannot opendir $savedir";
181 while (defined(my $file = readdir(DIR))) {
182 next if($file =~ /^\./);
183 print "\n";
184  
185 my $in=`/bin/cat $savedir/$file`;
186 my $exit = $? >> 8;
187  
188 if($exit>0) {
189 die "error reading file";
190 }
191  
192 unless($in=~ m/^\$diskid\s+=\s+('\d+'|\d+);\s+ # $diskid
193 \$total\s+=\s+('\d+'|\d+);\s+ # $total
194 \$toc\s+=\s+\[\s+ # $toc
195 (\{\s+
196 ('(frame|frames|min|sec|data)'\s+=\>\s+('\d+'|\d+)(,|)\s+){5}
197 \}(,|)\s+)+
198 \];\s+$/xs) {
199 print "not a save file: $savedir/$file\n";
200 next;
201 }
202  
203 eval $in;
204  
205 if($@) {
206 print "not a save file (eval error): $savedir/$file\n";
207 next;
208 }
209  
210 my %cd=get_cddb(\%config,[$diskid,$total,$toc]);
211  
212 unless(defined $cd{title}) {
213 print "no cddb entry found: $savedir/$file\n";
214 }
215  
216 unlink "$savedir/$file";
217  
218 next unless defined $cd{title};
219  
220 if($option{d} || $option{s}) {
221 print_xmcd(\%cd,$option{s});
222 } elsif($option{i}) {
223 insert_db(\%cd,\%db);
224 } elsif($option{l}) {
225 print_lame(\%cd);
226 } else {
227 print_cd(\%cd);
228 }
229 }
230 closedir(DIR);
231 exit;
232 }
233  
234 # get it on
235  
236 unless($config{multi}) {
237 my %cd;
238  
239 # for those who don't like 'die' in modules ;-)
240 eval {
241 %cd = get_cddb(\%config);
242 };
243 if ($@) {
244 print "fatal error: $!\n";
245 exit;
246 }
247  
248 print Dumper(\%cd) if $option{D};
249  
250 unless(defined $cd{title}) {
251 die "no cddb entry found";
252 }
253  
254 # do somthing with the results
255  
256 if($option{d} || $option{s}) {
257 print_xmcd(\%cd,$option{s});
258 } elsif($option{i}) {
259 insert_db(\%cd,\%db);
260 } elsif($option{l}) {
261 print_lame(\%cd);
262 } else {
263 print_cd(\%cd);
264 }
265 } else {
266 my @cd;
267  
268 # for those who don't like 'die' in modules ;-)
269 eval {
270 @cd=get_cddb(\%config);
271 };
272 if ($@) {
273 print "fatal error: $!\n";
274 exit;
275 }
276  
277 print Dumper(\@cd) if $option{D};
278  
279 for my $c (@cd) {
280 unless(defined $c->{title}) {
281 die "no cddb entry found";
282 }
283  
284 # do somthing with the results
285  
286 if($option{d} || $option{s}) {
287 print_xmcd($c,$option{s});
288 } elsif($option{i}) {
289 insert_db($c,\%db);
290 } elsif($option{l}) {
291 print_lame($c);
292 print "\n";
293 } else {
294 print_cd($c);
295 print "\n";
296 }
297 }
298 }
299  
300 exit;
301  
302  
303 # subroutines
304  
305 sub print_cd {
306 my $cd=shift;
307  
308 print "artist: $cd->{artist}\n";
309 print "title: $cd->{title}\n";
310 print "category: $cd->{cat}\n";
311 print "genre: $cd->{genre}\n" if($cd->{genre});
312 print "year: $cd->{year}\n" if($cd->{year});
313 print "cddbid: $cd->{id}\n";
314 print "trackno: $cd->{tno}\n";
315  
316 my $n=1;
317 foreach my $i ( @{$cd->{track}} ) {
318 if($option{t}) {
319 my $from=$cd->{frames}[$n-1];
320 my $to=$cd->{frames}[$n]-1;
321 my $dur=$to-$from;
322 my $min=int($dur/75/60);
323 my $sec=int($dur/75)-$min*60;
324 my $frm=($dur-$sec*75-$min*75*60)*100/75;
325 my $out=sprintf "track %2d: %8d - %8d [%2d:%.2d.%.2d]: $i\n",$n,$from,$to,$min,$sec,$frm;
326 print "$out";
327 } else {
328 print "track $n: $i\n";
329 }
330 $n++;
331 }
332 }
333  
334 sub print_xmcd {
335 my $cd=shift;
336 my $save=shift;
337  
338 *OUT=*STDOUT;
339  
340 if($save) {
341 unless(-e $xmcddir) {
342 mkdir $xmcddir,0755 or die "cannot create $savedir";
343 }
344  
345 unless($option{O}) {
346 if(-e "$xmcddir/$cd->{id}") {
347 print "XMCD file exists\n";
348 exit;
349 }
350 }
351  
352 open XMCD,">$xmcddir/$cd->{id}" or die "cannot open outfile";
353 *OUT=*XMCD;
354 }
355  
356 for(@{$cd->{raw}}) {
357 print OUT "$_";
358 }
359  
360 if($save) {
361 print STDERR "saved in: $xmcddir/$cd->{id}\n";
362 close OUT;
363 }
364 }
365  
366 sub insert_db {
367 my $cd=shift;
368 my $db=shift;
369  
370 my ($artist, $title, $category, $cddbid, $trackno) =
371 ($cd->{artist}, $cd->{title}, $cd->{cat}, $cd->{id}, $cd->{tno});
372  
373 my $sql = "SELECT cddbid FROM $db->{table_cds} WHERE CDDBID = \'$cddbid\'";
374 my $dbh = DBI->connect($db->{connect}->()) or die "cannot connect to db: $DBI::errstr";
375 my $sth = $dbh->prepare($sql);
376 my $r = $sth->execute or die "cannot check for cd: $DBI::errstr";
377 if ($r == 1) {
378 print "cd already in db\n";
379 if($option{O}) {
380 my $sql = "DELETE FROM $db->{table_cds} WHERE CDDBID = \'$cddbid\'";
381 my $sth = $dbh->prepare($sql);
382 my $r = $sth->execute or die "cannot delete from $db->{table_cds}: $DBI::errstr";
383 $sql = "DELETE FROM $db->{table_tracks} WHERE CDDBID = \'$cddbid\'";
384 $sth = $dbh->prepare($sql);
385 $r = $sth->execute or die "cannot delete from $db->{table_tracks}: $DBI::errstr";
386 } else {
387 exit;
388 }
389 }
390  
391 $title =~ s/'/\\'/g;
392 $artist =~ s/'/\\'/g;
393 $category =~ s/'/\\'/g;
394  
395 $sql = "INSERT INTO $db->{table_cds} (cddbid, artist, title, category, tracks) VALUES (\'$cddbid\', \'$artist\', \'$title\', \'$category\' , \'$trackno\')";
396 $sth = $dbh->prepare($sql);
397 $r = $sth->execute or die "failed to insert cd: $DBI::errstr";
398  
399 my $n=1;
400  
401 print "titel: $title\n";
402 print "artist: $artist\n";
403 print "category: $category\n\n";
404  
405 for my $t ( @{$cd->{track}} ) {
406 $t =~ s/'/\\'/g;
407 my $dur=($cd->{frames}[$n]-1-$cd->{frames}[$n-1])/75;
408 my $hour=int($dur/3600);
409 my $min=int($dur/60-$hour*60);
410 my $sec=$dur-$hour*3600-$min*60;
411 my $fr=substr(sprintf("%5.2f",$sec-int($sec)),2,3);
412 my $time=sprintf "%.2d:%.2d:%.2d%s",$hour,$min,int($sec),$fr;
413  
414 print "track $n: $t [$time]\n";
415
416 my $sql = "INSERT INTO $db->{table_tracks} (cddbid, title, trackno, time)
417 VALUES (\'$cddbid\',\'$t\', \'$n\', \'$time\')";
418 my $sth = $dbh->prepare($sql);
419 my $r = $sth->execute or die "failed to insert track $n: $DBI::errstr";
420 $n++;
421 }
422  
423 $dbh->disconnect();
424 }
425  
426 sub print_lame {
427 my $cd=shift;
428  
429 print_cd($cd);
430 print "\n";
431  
432 my $n=1;
433 for my $i ( @{$cd->{track}} ) {
434 $i =~ s/"/'/g;
435 print 'lame --tl "'.$cd->{title}.'" --ta "'.$cd->{artist}.'" --tt "'.$i.'" ';
436 printf "audio_%02d.wav ",$n;
437 $i =~ s/[^\S]|['"\/]/_/g;
438 $i =~ s/_+-_+/-/g;
439 print " $i.mp3\n";
440 $n++;
441 }
442 }