File: CDDB_File.pm

package info (click to toggle)
libmp3-tag-perl 1.16-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,488 kB
  • sloc: perl: 10,139; makefile: 16; sh: 11
file content (345 lines) | stat: -rw-r--r-- 8,987 bytes parent folder | download | duplicates (5)
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
package MP3::Tag::CDDB_File;

use strict;
use File::Basename;
use File::Spec;
use vars qw /$VERSION @ISA/;

$VERSION="1.00";
@ISA = 'MP3::Tag::__hasparent';

=pod

=head1 NAME

MP3::Tag::CDDB_File - Module for parsing CDDB files.

=head1 SYNOPSIS

  my $db = MP3::Tag::CDDB_File->new($filename, $track);	# Name of audio file
  my $db = MP3::Tag::CDDB_File->new_from($record, $track); # Contents of CDDB 

  ($title, $artist, $album, $year, $comment, $track) = $db->parse();

see L<MP3::Tag>

=head1 DESCRIPTION

MP3::Tag::CDDB_File is designed to be called from the MP3::Tag module.

It parses the content of CDDB file.

The file is found in the same directory as audio file; the list of possible
file names is taken from the field C<cddb_files> if set by MP3::Tag config()
method.

=over 4

=cut


# Constructor

sub new_from {
    my ($class, $data, $track) = @_;
    bless {data => [split /\n/, $data], track => $track}, $class;
}

sub new_setdir {
    my $class = shift;
    my $filename = shift;
    $filename = $filename->filename if ref $filename;
    $filename = dirname($filename);
    return bless {dir => $filename}, $class;	# bless to enable get_config()
}

sub new_fromdir {
    my $class = shift;
    my $h = shift;
    my $dir = $h->{dir};
    my ($found, $e);
    my $l = $h->get_config('cddb_files');
    for my $file (@$l) {
	my $f = File::Spec->catdir($dir, $file);
	$found = $f, last if -r $f;
    }
    return unless $found;
    local *F;
    open F, "< $found" or die "Can't open `$found': $!";
    if ($e = $h->get_config('decode_encoding_cddb_file') and $e->[0]) {
      eval "binmode F, ':encoding($e->[0])'"; # old binmode won't compile...
    }
    my @data = <F>;
    close F or die "Error closing `$found': $!";
    bless {filename => $found, data => \@data, track => shift,
	   parent => $h->{parent}}, $class;
}

sub new {
    my $class = shift;
    my $h = $class->new_setdir(@_);
    $class->new_fromdir($h);
}

sub new_with_parent {
    my ($class, $filename, $parent) = @_;
    my $h = $class->new_setdir($filename);
    $h->{parent} = $parent;
    $class->new_fromdir($h);
}

# Destructor

sub DESTROY {}

=item parse()

  ($title, $artist, $album, $year, $comment, $track) =
     $db->parse($what);

parse_filename() extracts information about artist, title, track number,
album and year from the CDDB record.  $what is optional; it maybe title,
track, artist, album, year, genre or comment. If $what is defined parse() will return
only this element.

Additionally, $what can take values C<artist_collection> (returns the value of
artist in the disk-info field DTITLE, but only if author is specified in the
track-info field TTITLE), C<title_track> (returns the title specifically from
track-info field - the C<track> may fall back to the info from disk-info
field), C<comment_collection> (processed EXTD comment), C<comment_track>
(processed EXTT comment).

The returned year and genre is taken from DYEAR, DGENRE, EXTT, EXTD fields;
recognized prefixes in the two last fields are YEAR, ID3Y, ID3G.
The declarations of this form are stripped from the returned comment.

An alternative
syntax "Recorded"/"Recorded on"/"Recorded in"/ is also supported; the format
of the date recognized by ID3v2::year(), or just a date field without a prefix.

=cut

sub return_parsed {
    my ($self,$what) = @_;
    if (defined $what) {
	return $self->{parsed}{a_in_title}  if $what =~/^artist_collection/i;
	return $self->{parsed}{t_in_track}  if $what =~/^title_track/i;
	return $self->{parsed}{extt}  if $what =~/^comment_track/i;
	return $self->{parsed}{extd}  if $what =~/^comment_collection/i;
	return $self->{parsed}{DISCID}  if $what =~/^cddb_id/i;
	return $self->{parsed}{album}  if $what =~/^al/i;
	return $self->{parsed}{artist} if $what =~/^a/i;
	return $self->{parsed}{track}  if $what =~/^tr/i;
	return $self->{parsed}{year}   if $what =~/^y/i;
	return $self->{parsed}{comment}if $what =~/^c/i;
	return $self->{parsed}{genre}  if $what =~/^g/i;
	return $self->{parsed}{title};
    }
    
    return $self->{parsed} unless wantarray;
    return map $self->{parsed}{$_} , qw(title artist album year comment track);
}

my %r = ( 'n' => "\n", 't' => "\t", '\\' => "\\"  );

sub parse_lines {
    my ($self) = @_;
    return if $self->{fields};
    for my $l (@{$self->{data}}) {
	next unless $l =~ /^\s*(\w+)\s*=(\s*(.*))/;
	my $app = $2;
	$self->{fields}{$1} = "", $app = $3 unless exists $self->{fields}{$1};
	$self->{fields}{$1} .= $app;
	$self->{last} = $1 if $1 =~ /\d+$/;
    }    
    s/\\([nt\\])/$r{$1}/g for values %{$self->{fields}};
}

sub parse {
    my ($self,$what) = @_;
    return $self->return_parsed($what)	if exists $self->{parsed};
    $self->parse_lines;
    my %parsed;
    my ($t1, $c1, $t2, $c2) = map $self->{fields}{$_}, qw(DTITLE EXTD);
    my $track = $self->track;
    if ($track) {
	my $t = $track - 1;
	($t2, $c2) = map $self->{fields}{$_}, "TTITLE$t", "EXTT$t";
    }
    my ($a, $t, $aa, $tt, $a_in_title, $t_in_track);
    ($a, $t) = split /\s+\/\s+/, $t1, 2 if defined $t1;
    ($a, $t) = ($t, $a) unless defined $t;
    ($aa, $tt) = split /\s+\/\s+/, $t2, 2 if defined $t2;
    ($aa, $tt) = ($tt, $aa) unless defined $tt;
    undef $a if defined $a and $a =~ 
	/^\s*(<<\s*)?(Various Artists|compilation disc)\s*(>>\s*)?$/i;
    undef $aa if defined $aa and $aa =~ 
	/^\s*(<<\s*)?(Various Artists|compilation disc)\s*(>>\s*)?$/i;
    $a_in_title = $a if defined $a and length $a and defined $aa and length $aa;
    $aa = $a unless defined $aa and length $aa;
    $t_in_track = $tt;
    $tt = $t unless defined $tt and length $tt;

    my ($y, $cat) = ($self->{fields}{DYEAR}, $self->{fields}{DGENRE});
    for my $f ($c2, $c1) {
      if (defined $f and length $f) { # Process old style declarations
	while ($f =~ s/^\s*((YEAR|ID3Y)|ID3G)\b:?\s*(\d+)\b\s*(([;.,]|\s-\s)\s*)?//i
	       || $f =~ s/(?:\s*(?:[;.,]|\s-\s))?\s*\b((YEAR|ID3Y)|ID3G)\b:?\s*(\d+)\s*([;.,]\s*)?$//i) {
	    $y = $3 if $2 and not $y;
	    $cat = $3 if not $2 and not $cat;
	}
	if ($f =~ s{
		     ((^|[;,.]|\s+-\s) # 1,2
		      \s*
		      (Recorded (\s+[io]n)? \s* (:\s*)? )? # 3, 4, 5
		      (\d{4}([-,][-\d\/,]+)?) # 6, 7
		      \b \s* (?: [.;] \s* )? 
		      ((?:[;.,]|\s-\s|$)\s*)) # 8
		   }
	           {
		    ((($self->{parent}->get_config('comment_remove_date'))->[0]
		       and not ($2 and $8))
		      ? '' : $1) . ($2 && $8 ? $8 : '')
		   }xeim and not ($2 and $8)) {
	    # Overwrite the disk year for longer forms
	    $y = $6 if $3 or $7 or not $y or $c2 and $f eq $c2;
	}
	$f =~ s/^\s+//;
	$f =~ s/\s+$//;
	undef $f unless length $f;
      }
    }
    my ($cc1, $cc2) = ($c1, $c2);
    if (defined $c2 and length $c2) { # Merge unless one is truncation of another
	if ( defined $c1 and length $c1
	     and $c1 ne substr $c2, 0, length $c1
	     and $c1 ne substr $c2, -length $c1 ) {
	    $c2 =~ s/\s*[.,:;]$//;
	    my $sep = (("$c1$c2" =~ /\n/) ? "\n" : '; ');
	    $c1 = "$c2$sep$c1";
	} else {
	    $c1 = $c2;
	}
    }
    if (defined $cat and $cat =~ /^\d+$/) {
	require MP3::Tag::ID3v1;
	$cat = $MP3::Tag::ID3v1::winamp_genres[$cat] if $cat < scalar @MP3::Tag::ID3v1::winamp_genres;
    }

    @parsed{ qw( title artist album year comment track genre
		 a_in_title t_in_track extt extd) } =
	($tt, $aa, $t, $y, $c1, $track, $cat, $a_in_title, $t_in_track, $cc2, $cc1);
    $parsed{DISCID} = $self->{fields}{DISCID};
    $self->{parsed} = \%parsed;
    $self->return_parsed($what);
}


=pod

=item title()

 $title = $db->title();

Returns the title, obtained from the C<'Tracktitle'> entry of the file.

=cut

*song = \&title;

sub title {
    return shift->parse("title");
}

=pod

=item artist()

 $artist = $db->artist();

Returns the artist name, obtained from the C<'Performer'> or
C<'Albumperformer'> entries (the first which is present) of the file.

=cut

sub artist {
    return shift->parse("artist");
}

=pod

=item track()

 $track = $db->track();

Returns the track number, stored during object creation, or queried from
the parent.


=cut

sub track {
  my $self = shift;
  return $self->{track} if defined $self->{track};
  return if $self->{recursive} or not $self->parent_ok;
  local $self->{recursive} = 1;
  return $self->{parent}->track1;
}

=item year()

 $year = $db->year();

Returns the year, obtained from the C<'Year'> entry of the file.  (Often
not present.)

=cut

sub year {
    return shift->parse("year");
}

=pod

=item album()

 $album = $db->album();

Returns the album name, obtained from the C<'Albumtitle'> entry of the file.

=cut

sub album {
    return shift->parse("album");
}

=item comment()

 $comment = $db->comment();

Returns the C<'Trackcomment'> entry of the file.  (Often not present.)

=cut

sub comment {
    return shift->parse("comment");
}

=item genre()

 $genre = $db->genre($filename);

=cut

sub genre {
    return shift->parse("genre");
}

for my $elt ( qw( cddb_id ) ) {
  no strict 'refs';
  *$elt = sub (;$) {
    return shift->parse($elt);
  }
}

1;