| 1 | # | ||
| 2 | # CDDB - Read the CDDB entry for an audio CD in your drive | ||
| 3 | # | ||
| 4 | # This module/script gets the CDDB info for an audio cd. You need | ||
| 5 | # LINUX, a cdrom drive and an active internet connection in order | ||
| 6 | # to do that. | ||
| 7 | # | ||
| 8 | # (c) 2004 Armin Obersteiner <armin@xos.net> | ||
| 9 | # | ||
| 10 | # LICENSE | ||
| 11 | # | ||
| 12 | # This library is released under the same conditions as Perl, that | ||
| 13 | # is, either of the following: | ||
| 14 | # | ||
| 15 | # a) the GNU General Public License Version 2 as published by the | ||
| 16 | # Free Software Foundation, | ||
| 17 | # | ||
| 18 | # b) the Artistic License. | ||
| 19 | # | ||
| 20 | |||
| 21 | package CDDB_cache; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $debug $dir $readonly $grep); | ||
| 25 | |||
| 26 | require Exporter; | ||
| 27 | |||
| 28 | @ISA = qw(Exporter AutoLoader); | ||
| 29 | # Items to export into callers namespace by default. Note: do not export | ||
| 30 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 31 | # Do not simply export all your public functions/methods/constants. | ||
| 32 | @EXPORT_OK = qw( | ||
| 33 | get_cddb | ||
| 34 | get_discids | ||
| 35 | ); | ||
| 36 | $VERSION = '1.0'; | ||
| 37 | |||
| 38 | use CDDB_get; | ||
| 39 | use File::Find; | ||
| 40 | use Data::Dumper qw(Dumper); | ||
| 41 | |||
| 42 | #$debug=1; | ||
| 43 | $dir="/tmp/xmcd"; | ||
| 44 | |||
| 45 | my $CD_DEVICE = "/dev/cdrom"; | ||
| 46 | |||
| 47 | sub get_discids { | ||
| 48 | my $cd=shift; | ||
| 49 | |||
| 50 | return CDDB_get::get_discids($cd); | ||
| 51 | } | ||
| 52 | |||
| 53 | my @files; | ||
| 54 | |||
| 55 | sub get_cddb { | ||
| 56 | my $config=shift; | ||
| 57 | my $diskid=shift; | ||
| 58 | |||
| 59 | if($debug) { | ||
| 60 | print STDERR "dir: [$dir] readonly [$readonly] grep [$grep]\n"; | ||
| 61 | } | ||
| 62 | |||
| 63 | $config->{multi}=0; | ||
| 64 | $CD_DEVICE = $config->{CD_DEVICE} if (defined($config->{CD_DEVICE})); | ||
| 65 | |||
| 66 | my $did=$diskid; | ||
| 67 | $did=CDDB_get::get_discids($CD_DEVICE) unless($did); | ||
| 68 | #print Dumper($did); | ||
| 69 | |||
| 70 | printf STDERR "%08x %08lx\n", $did->[0], $did->[0] if($debug); | ||
| 71 | |||
| 72 | my $sid = sprintf "%08x", $did->[0]; | ||
| 73 | my $id = sprintf "%08x %d", $did->[0], $did->[1]; | ||
| 74 | for(0..$did->[1]-1) { | ||
| 75 | $id.=" ".$did->[2]->[$_]->{frames}; | ||
| 76 | } | ||
| 77 | $id.=" ".int($did->[2]->[$did->[1]]->{frames}/75); | ||
| 78 | print STDERR "id: $id\n" if $debug; | ||
| 79 | |||
| 80 | @files=(); | ||
| 81 | if($grep) { | ||
| 82 | find({ wanted => sub { | ||
| 83 | next unless -f $File::Find::name; | ||
| 84 | my $tid=`grep -H $sid $File::Find::name`; | ||
| 85 | if($tid =~ /\S+/) { | ||
| 86 | push @files, $File::Find::name; | ||
| 87 | } | ||
| 88 | }, follow=>1}, $dir); | ||
| 89 | } else { | ||
| 90 | find({ wanted => sub { | ||
| 91 | if($_ eq $sid) { | ||
| 92 | push @files, $File::Find::name; | ||
| 93 | } | ||
| 94 | }, follow=>1}, $dir); | ||
| 95 | } | ||
| 96 | print STDERR Dumper(\@files) if $debug; | ||
| 97 | |||
| 98 | my $file; | ||
| 99 | my $found; | ||
| 100 | |||
| 101 | for(@files) { | ||
| 102 | open IN,$_; | ||
| 103 | undef $/; | ||
| 104 | $file=<IN>; | ||
| 105 | close IN; | ||
| 106 | |||
| 107 | my ($fid)=$file =~ /DISCID=(\S+)/i; | ||
| 108 | my ($len)=$file =~ /Disc length: (\d+) seconds/i; | ||
| 109 | my ($s)=$file =~ /Track frame offsets:\s+(.*)\s+Disc length/si; | ||
| 110 | my $t=""; | ||
| 111 | my @tr=split /#/,$s; | ||
| 112 | my $c=0; | ||
| 113 | for(@tr) { | ||
| 114 | next unless /(\d+)/; | ||
| 115 | $c++; | ||
| 116 | $t.=" $1"; | ||
| 117 | last if($c>=$did->[1]); | ||
| 118 | } | ||
| 119 | my $tid="$fid $c$t $len"; | ||
| 120 | |||
| 121 | if($tid eq $id) { | ||
| 122 | $found=$_; | ||
| 123 | last; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | my $toc=$did->[2]; | ||
| 128 | |||
| 129 | my %cd; | ||
| 130 | if($found) { | ||
| 131 | print STDERR "CDDB_cache: reading from file: $found\n"; | ||
| 132 | |||
| 133 | my @lines=split /\n/,$file; | ||
| 134 | for(@lines) { | ||
| 135 | push @{$cd{raw}},$_."\n"; | ||
| 136 | #TTITLE0=Bitch (Edit) | ||
| 137 | if(/^TTITLE(\d+)\=\s*(.*)/) { | ||
| 138 | my $t= $2; | ||
| 139 | chomp $t; | ||
| 140 | $cd{frames}[$1]=$toc->[$1]->{frames}; | ||
| 141 | $cd{data}[$1]=$toc->[$1]->{data}; | ||
| 142 | unless (defined $cd{track}[$1]) { | ||
| 143 | $cd{track}[$1]=$t; | ||
| 144 | } else { | ||
| 145 | $cd{track}[$1]=$cd{track}[$1].$t; | ||
| 146 | } | ||
| 147 | } elsif(/^DYEAR=\s*(\d+)/) { | ||
| 148 | $cd{'year'} = $1; | ||
| 149 | } elsif(/^DGENRE=\s*(\S+.*)/) { | ||
| 150 | my $t = $1; | ||
| 151 | chomp $t; | ||
| 152 | $cd{'genre'} = $t; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | $cd{tno}=$#{$cd{track}}+1; | ||
| 157 | $cd{frames}[$cd{tno}]=$toc->[$cd{tno}]->{frames}; | ||
| 158 | |||
| 159 | $cd{id}=$sid; | ||
| 160 | |||
| 161 | my ($at)=$file=~/DTITLE=(.*?)\n/; | ||
| 162 | my ($artist,$title); | ||
| 163 | #chop $at if($at =~ /\r/); | ||
| 164 | chomp $at; | ||
| 165 | |||
| 166 | if($at =~ /\//) { | ||
| 167 | ($artist,$title)= $at =~ /^(.*?)\s\/\s(.*)/; | ||
| 168 | } else { | ||
| 169 | $artist=$at; | ||
| 170 | $title=$at; | ||
| 171 | } | ||
| 172 | |||
| 173 | $cd{artist}=$artist; | ||
| 174 | $cd{title}=$title; | ||
| 175 | |||
| 176 | my ($cat) = $found =~ /$dir\/(\S+?)\/$sid/; | ||
| 177 | $cd{cat}=$cat; | ||
| 178 | |||
| 179 | } else { | ||
| 180 | print STDERR "CDDB_cache: reading from network\n"; | ||
| 181 | |||
| 182 | %cd=CDDB_get::get_cddb($config,$diskid); | ||
| 183 | } | ||
| 184 | |||
| 185 | print STDERR Dumper(\%cd) if $debug; | ||
| 186 | |||
| 187 | unless($readonly || $found) { | ||
| 188 | my $file=$dir."/$cd{cat}/$cd{id}"; | ||
| 189 | my $ddir=$dir."/$cd{cat}"; | ||
| 190 | mkdir $ddir,0755; | ||
| 191 | |||
| 192 | open OUT,">$file"; | ||
| 193 | for(@{$cd{raw}}) { | ||
| 194 | print OUT $_; | ||
| 195 | } | ||
| 196 | close OUT; | ||
| 197 | } | ||
| 198 | |||
| 199 | return %cd; | ||
| 200 | } | ||
| 201 | |||
| 202 | 1; | ||
| 203 | __END__ |