Codebase list libcddb-get-perl / HEAD cddb.pl
HEAD

Tree @HEAD (Download .tar.gz)

cddb.pl @HEADraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/perl -I.
#
#  CDDB - Read the CDDB entry for an audio CD in your drive
#
#  This module/script gets the CDDB info for an audio cd. You need
#  LINUX, a cdrom drive and an active internet connection in order
#  to do that.
#
#  (c) 2004 Armin Obersteiner <armin@xos.net>
#
#  LICENSE
#
#  This library is released under the same conditions as Perl, that
#  is, either of the following:
#
#  a) the GNU General Public License Version 2 as published by the
#  Free Software Foundation,
#
#  b) the Artistic License.
#

#use CDDB_get qw( get_cddb get_discids );

use Data::Dumper;
use Getopt::Std;

use strict;

my %option = ();
getopts("oghdtsi:SfDlOFc:H:CIRGP", \%option); 

if($option{h}) {
  print "$0: gets CDDB info of a CD\n";
  print "  no argument - gets CDDB info of CD in your drive\n";
  print "  -c  device (other than default device)\n";
  print "  -o  offline mode - just stores CD info\n";
  print "  -d  output in xmcd format\n";
  print "  -s  save in xmcd format\n";
  print "  -i  db. one of: mysql, pg, oracle, sqlite\n";
  print "  -O  overwrite file or db\n";
  print "  -t  output toc\n";
  print "  -l  output lame command\n";
  print "  -f  http mode (e.g. through firewalls)\n";
  print "  -F  some stateful firewalls/http proxies need additional newlines\n";
  print "  -g  get CDDB info for stored CDs\n";
  print "  -I  non interactive mode\n";
  print "  -H  CDDB hostname\n";
  print "  -C  use local cache\n";
  print "  -R  readonly cache\n";
  print "  -G  cache has not the diskid as filenames (much slower)\n";
  print "  -P  cache path (default: /tmp/xmcd)\n";
  print "  -D  put CDDB_get in debug mode\n";
  exit;
}

my %config;

my $diskid;
my $total;
my $toc;
my $savedir="/tmp/cddb";
my $xmcddir="/tmp/xmcd";

if($option{C}) {
  # use CDDB_cache qw( get_cddb get_discids );
  require CDDB_cache;
  CDDB_cache->import( qw( get_cddb get_discids ) );

  $CDDB_cache::debug=1 if($option{D});
  $CDDB_cache::readonly=1 if($option{R});
  $CDDB_cache::grep=1 if($option{G});

  $CDDB_cache::dir="/tmp/xmcd"; # default
  # $CDDB_cache::dir="/opt/kde2/share/apps/kscd/cddb";
  $CDDB_cache::dir=$option{P} if($option{P});

} else {
  # use CDDB_get qw( get_cddb get_discids );
  require CDDB_get;
  CDDB_get->import( qw( get_cddb get_discids ) );
}

$CDDB_get::debug=1 if($option{D});

# following variables just need to be declared if different from defaults
# defaults are listed below (cdrom default is os specific)

# $config{CDDB_HOST}="freedb.freedb.org";	# set cddb host
if($option{H}) {
  $config{CDDB_HOST}=$option{H};
}
# $config{CDDB_PORT}=8880; 			# set cddb port
# $config{CDDB_MODE}="cddb";			# set cddb mode: cddb or http, this is switched with -f
# $config{CD_DEVICE}="/dev/cdrom";		# set cd device

# $config{HELLO_ID} ="root nowhere.com fastrip 0.77"; # hello string: username hostname clientname version
# $config{PROTO_VERSION} = 5; # cddb protokol version


# get proxy settings for cddb mode

$config{HTTP_PROXY}=$ENV{http_proxy} if $ENV{http_proxy}; # maybe wanna use a proxy ?

$config{CDDB_MODE}="http" if($option{f}); 
if($option{F}) {
  $config{CDDB_MODE}="http";
  $config{FW}=1;
}

$config{CD_DEVICE}=$option{c} if $option{c};

# user interaction welcome?

$config{input}=1;   # 1: ask user if more than one possibility
                    # 0: no user interaction
$config{multi}=0;   # 1: do not ask user and get all of them
                    # 0: just the first one

$config{input}=0 if($option{I});

my %db;

if($option{i}) {
  require DBI;

  $db{table_cds} = "cds";
  $db{table_tracks} = "tracks";

  # not needed for sqlite
  $db{host} = "localhost";
  $db{port} = "3306";

  # not needed for oracle/sqlite
  $db{name} = "mp3-test";

  # just for oracle
  $db{sid} = "xxx";
  $db{home} = "xxx";

  # just for sqlite
  $db{file} = "xxx";

  # not needed for sqlite
  $db{user} = "root";
  $db{passwd} = "xxx";


  if($option{i} eq "mysql") {
    $db{connect} = sub { "dbi:mysql:database=$db{name};host=$db{host};port=$db{port}", $db{user}, $db{passwd} };
  } elsif($option{i} eq "pg") {
    $db{connect} = sub { "dbi:Pg:dbname=$db{dbname};host=$db{host};port=$db{port}", $db{user}, $db{passwd} };
  } elsif($option{i} eq "oracle") {
    $db{connect} = sub { "dbi:Oracle:host=$db{host};sid=$db{sid};port=$db{port}", $db{user}, $db{passwd} };
    $ENV{ORACLE_HOME} = $db{home};
  } elsif($option{i} eq "sqlite") {
    $db{connect} = sub { "dbi:SQLite:dbname=$db{file}","","" };
  } else {
    die "unkown database: $option{i}";
  }
}
  
if($option{o}) {
  my $ids=get_discids($config{CD_DEVICE});

  unless(-e $savedir) {
    mkdir $savedir,0755 or die "cannot create $savedir";
  }

  open OUT,">$savedir/$ids->[0]\_$$" or die "cannot open outfile";
  print OUT Data::Dumper->Dump($ids,["diskid","total","toc"]);
  close OUT;

  print STDERR "saved in: $savedir/$ids->[0]\_$$\n";
  exit;
}

if($option{g}) {
  print STDERR "retrieving stored cds ...\n";

  opendir(DIR, $savedir) or die "cannot opendir $savedir";
  while (defined(my $file = readdir(DIR))) {
    next if($file =~ /^\./);
    print "\n";

    my $in=`/bin/cat $savedir/$file`;
    my $exit  = $? >> 8; 

    if($exit>0) {
      die "error reading file";
    }

    unless($in=~ m/^\$diskid\s+=\s+('\d+'|\d+);\s+         # $diskid
                    \$total\s+=\s+('\d+'|\d+);\s+          # $total
                    \$toc\s+=\s+\[\s+                      # $toc
                      (\{\s+
                        ('(frame|frames|min|sec|data)'\s+=\>\s+('\d+'|\d+)(,|)\s+){5}
                      \}(,|)\s+)+
                    \];\s+$/xs) {
      print "not a save file: $savedir/$file\n";
      next;                 
    }

    eval $in;

    if($@) {
      print "not a save file (eval error): $savedir/$file\n";
      next;
    }

    my %cd=get_cddb(\%config,[$diskid,$total,$toc]);

    unless(defined $cd{title}) {
      print "no cddb entry found: $savedir/$file\n";
    }

    unlink "$savedir/$file";

    next unless defined $cd{title};

    if($option{d} || $option{s}) {
      print_xmcd(\%cd,$option{s});
    } elsif($option{i}) {
      insert_db(\%cd,\%db);
    } elsif($option{l}) {
      print_lame(\%cd);
    } else {
      print_cd(\%cd);
    }
  }
  closedir(DIR);
  exit;
}

# get it on

unless($config{multi}) {
  my %cd;

  # for those who don't like 'die' in modules ;-)
  eval { 
    %cd = get_cddb(\%config);
  };
  if ($@) {
    print "fatal error: $!\n";
    exit;
  }

  print Dumper(\%cd) if $option{D};

  unless(defined $cd{title}) {
    die "no cddb entry found";
  }

  # do somthing with the results

  if($option{d} || $option{s}) {
    print_xmcd(\%cd,$option{s});
  } elsif($option{i}) {
    insert_db(\%cd,\%db);
  } elsif($option{l}) {
    print_lame(\%cd);
  } else {
    print_cd(\%cd);
  }
} else { 
  my @cd;

  # for those who don't like 'die' in modules ;-)
  eval { 
    @cd=get_cddb(\%config);
  };
  if ($@) {
    print "fatal error: $!\n";
    exit;
  }

  print Dumper(\@cd) if $option{D};

  for my $c (@cd) {
    unless(defined $c->{title}) {
      die "no cddb entry found";
    }

    # do somthing with the results

    if($option{d} || $option{s}) {
      print_xmcd($c,$option{s});
    } elsif($option{i}) {
      insert_db($c,\%db);
    } elsif($option{l}) {
      print_lame($c);
      print "\n";
    } else {
      print_cd($c);
      print "\n";
    }
  }
}

exit;


# subroutines

sub print_cd {
  my $cd=shift;

  print "artist: $cd->{artist}\n";
  print "title: $cd->{title}\n";
  print "category: $cd->{cat}\n";
  print "genre: $cd->{genre}\n" if($cd->{genre});
  print "year: $cd->{year}\n" if($cd->{year});
  print "cddbid: $cd->{id}\n";
  print "trackno: $cd->{tno}\n";

  my $n=1;
  foreach my $i ( @{$cd->{track}} ) {
    if($option{t}) {
      my $from=$cd->{frames}[$n-1];
      my $to=$cd->{frames}[$n]-1;
      my $dur=$to-$from;
      my $min=int($dur/75/60);
      my $sec=int($dur/75)-$min*60;
      my $frm=($dur-$sec*75-$min*75*60)*100/75;
      my $out=sprintf "track %2d: %8d - %8d  [%2d:%.2d.%.2d]: $i\n",$n,$from,$to,$min,$sec,$frm;
      print "$out"; 
    } else {
      print "track $n: $i\n";
    }
    $n++;
  }  
}

sub print_xmcd {
  my $cd=shift;
  my $save=shift;

  *OUT=*STDOUT;

  if($save) {
    unless(-e $xmcddir) {
      mkdir $xmcddir,0755 or die "cannot create $savedir";
    }

    unless($option{O}) {
      if(-e "$xmcddir/$cd->{id}") {
        print "XMCD file exists\n";
        exit;
      }
    }

    open XMCD,">$xmcddir/$cd->{id}" or die "cannot open outfile";
    *OUT=*XMCD;
  }

  for(@{$cd->{raw}}) {
    print OUT "$_";
  }

  if($save) {
    print STDERR "saved in: $xmcddir/$cd->{id}\n";
    close OUT;
  }
}  

sub insert_db {
  my $cd=shift;
  my $db=shift;

  my ($artist, $title, $category, $cddbid, $trackno) =
    ($cd->{artist}, $cd->{title}, $cd->{cat}, $cd->{id}, $cd->{tno});

  my $sql = "SELECT cddbid FROM $db->{table_cds} WHERE CDDBID = \'$cddbid\'";
  my $dbh = DBI->connect($db->{connect}->()) or die "cannot connect to db: $DBI::errstr";
  my $sth = $dbh->prepare($sql);
  my $r = $sth->execute or die "cannot check for cd: $DBI::errstr";
  if ($r == 1) {
    print "cd already in db\n";
    if($option{O}) {
      my $sql = "DELETE FROM $db->{table_cds} WHERE CDDBID = \'$cddbid\'";
      my $sth = $dbh->prepare($sql);
      my $r = $sth->execute or die "cannot delete from $db->{table_cds}: $DBI::errstr";
      $sql = "DELETE FROM $db->{table_tracks} WHERE CDDBID = \'$cddbid\'";
      $sth = $dbh->prepare($sql);
      $r = $sth->execute or die "cannot delete from $db->{table_tracks}: $DBI::errstr";
    } else {
      exit;
    }
  }

  $title =~ s/'/\\'/g;
  $artist =~ s/'/\\'/g;
  $category =~ s/'/\\'/g;

  $sql = "INSERT INTO $db->{table_cds} (cddbid, artist, title, category, tracks) VALUES (\'$cddbid\', \'$artist\', \'$title\', \'$category\' , \'$trackno\')";
  $sth = $dbh->prepare($sql);
  $r = $sth->execute or die "failed to insert cd: $DBI::errstr";

  my $n=1;

  print "titel: $title\n";
  print "artist: $artist\n";
  print "category: $category\n\n";

  for my $t ( @{$cd->{track}} ) {
    $t =~ s/'/\\'/g;
    my $dur=($cd->{frames}[$n]-1-$cd->{frames}[$n-1])/75;
    my $hour=int($dur/3600);
    my $min=int($dur/60-$hour*60);
    my $sec=$dur-$hour*3600-$min*60;
    my $fr=substr(sprintf("%5.2f",$sec-int($sec)),2,3);
    my $time=sprintf "%.2d:%.2d:%.2d%s",$hour,$min,int($sec),$fr;

    print "track $n: $t  [$time]\n";
    
    my $sql = "INSERT INTO $db->{table_tracks} (cddbid, title, trackno, time) 
               VALUES (\'$cddbid\',\'$t\', \'$n\', \'$time\')";
    my $sth = $dbh->prepare($sql);
    my $r = $sth->execute or die "failed to insert track $n: $DBI::errstr";
    $n++;
  }

  $dbh->disconnect();
} 

sub print_lame {
  my $cd=shift;

  print_cd($cd);
  print "\n";

  my $n=1;
  for my $i ( @{$cd->{track}} ) {
    $i =~ s/"/'/g;
    print 'lame --tl "'.$cd->{title}.'" --ta "'.$cd->{artist}.'" --tt "'.$i.'" ';
    printf "audio_%02d.wav ",$n;
    $i =~ s/[^\S]|['"\/]/_/g;
    $i =~ s/_+-_+/-/g;
    print " $i.mp3\n";
    $n++;
  }
}