line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Audio::DB; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: DB.pm,v 1.2 2005/02/27 16:56:25 todd Exp $ |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
24474
|
use strict 'vars'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
6
|
1
|
|
|
1
|
|
6
|
use Carp 'croak','cluck'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
84
|
|
7
|
1
|
|
|
1
|
|
6
|
use vars qw(@ISA $VERSION); |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
98
|
|
8
|
1
|
|
|
1
|
|
551
|
use Audio::DB::Util::Rearrange; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
250
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$|++; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
$VERSION = ''; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
######################## |
16
|
|
|
|
|
|
|
## NEW CONSTRUCTOR ## |
17
|
|
|
|
|
|
|
######################## |
18
|
|
|
|
|
|
|
# DB.pm is a factory for all types of objects. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# It controls the generic new and |
21
|
|
|
|
|
|
|
# establishes the connection to the database |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Generic factory for Audio::DB::objects |
25
|
|
|
|
|
|
|
sub new { |
26
|
0
|
|
|
0
|
1
|
|
my ($self,@p) = @_; |
27
|
0
|
|
|
|
|
|
my ($adaptor,$task,@args) = rearrange(['ADAPTOR','TASK'],@p); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# This will create a new database Adaptor object depending on that which is passed. |
30
|
|
|
|
|
|
|
# Eventually, I should enable other DBs |
31
|
|
|
|
|
|
|
# This is how it would be done (I've redefined the $adaptor below |
32
|
|
|
|
|
|
|
# so it doesn't quite follow...) |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
0
|
|
|
|
$adaptor ||= 'dbi::mysql'; |
35
|
0
|
|
|
|
|
|
my $class = "Audio::DB::Adaptor::\L${adaptor}\E"; |
36
|
0
|
0
|
|
|
|
|
eval "require $class" unless $class->can('new'); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my $this = bless {},$self; |
39
|
0
|
|
|
|
|
|
$this->{adaptor} = $class->new(@args); |
40
|
0
|
|
|
|
|
|
return $this; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
0
|
0
|
|
sub adaptor { return shift->{adaptor}; } |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
1; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=pod |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 NAME |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Audio::DB - Tools for generating relational databases of music files |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 SYNOPSIS |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
use Audio::DB; |
56
|
|
|
|
|
|
|
my $mp3 = Audio::DB->new(-user => 'user', |
57
|
|
|
|
|
|
|
-pass => 'password', |
58
|
|
|
|
|
|
|
-host => 'db_host', |
59
|
|
|
|
|
|
|
-dsn => 'music_db', |
60
|
|
|
|
|
|
|
-adaptor => 'dbi::mysql'); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$mp3->initialize(1); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
$mp3->load_database(-dirs =>['/path/to/MP3s/'], |
65
|
|
|
|
|
|
|
-tmp =>'/tmp/'); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 DESCRIPTION |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Audio::DB is a series of modules for creating relational databases of |
70
|
|
|
|
|
|
|
music files directly from data stored in ID3 tags or from flatfiles of |
71
|
|
|
|
|
|
|
information of track information. Once created, Audio::DB provides |
72
|
|
|
|
|
|
|
various methods for creating reports and web pages of your |
73
|
|
|
|
|
|
|
collection. Although it's nutritious and delicious on its own, Audio::DB |
74
|
|
|
|
|
|
|
was created for use with Apache::MP3::DB, a subclass of Apache::MP3. |
75
|
|
|
|
|
|
|
This module makes it easy to make your collection web-accessible, |
76
|
|
|
|
|
|
|
complete with browsing, searching, streaming, multiple users, |
77
|
|
|
|
|
|
|
playlists, ratings, and more! |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 USAGE |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
There are three central modules that you will be interacting with. |
82
|
|
|
|
|
|
|
Audio::DB::Build, Audio::DB::Web, and Audio::DB::Reports. Audio::DB itself |
83
|
|
|
|
|
|
|
provides a generic factory interface for building these objects. |
84
|
|
|
|
|
|
|
Audio::DB returns an appropriate object for the desired task at hand. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 Creating A New MP3 Database |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Creating a new database is as easy as: |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
use strict; |
91
|
|
|
|
|
|
|
use Audio::DB; |
92
|
|
|
|
|
|
|
my $mp3 = Audio::DB->new(-user =>'user', |
93
|
|
|
|
|
|
|
-pass =>'password', |
94
|
|
|
|
|
|
|
-host =>'db_host', |
95
|
|
|
|
|
|
|
-dsn =>'music_db', |
96
|
|
|
|
|
|
|
-create =>1); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$mp3->initialize(1); # Populates the database with schema |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
my $stats = $mp3->load_database(-dirs =>['/path/to/MP3s/'], |
101
|
|
|
|
|
|
|
-tmp =>'/tmp/'); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 Appending To A Preexisting MP3 Database |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Appending new mp3s to a preexisting database is as easy as: |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
use strict; |
109
|
|
|
|
|
|
|
use Audio::DB::Build; |
110
|
|
|
|
|
|
|
my $mp3 = Audio::DB->new(-user =>'user', |
111
|
|
|
|
|
|
|
-pass =>'password', |
112
|
|
|
|
|
|
|
-host =>'db_host', |
113
|
|
|
|
|
|
|
-dsn =>'music_db'); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
$mp3->update_database(-dirs =>['/path/to/MP3s/'], |
116
|
|
|
|
|
|
|
-tmp =>'/tmp/'); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 REQUIRES |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Perl Modules: |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
B for reading ID3 tags, B for |
124
|
|
|
|
|
|
|
distinguising types of readable files, B for SQLite |
125
|
|
|
|
|
|
|
support; B for interacting with MySQL databases. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
MySQL must be installed if you wish to use MySQL as your RDBMS. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 EXPORTS |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
No methods are exported. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 METHODS |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head2 new |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Title : new |
138
|
|
|
|
|
|
|
Usage : Audio::DB->new(-adaptor => 'dbi::mysql', |
139
|
|
|
|
|
|
|
-user => 'user', |
140
|
|
|
|
|
|
|
-pass => 'password', |
141
|
|
|
|
|
|
|
-host => 'db_host', |
142
|
|
|
|
|
|
|
-dsn => 'dbi:mysql:music_db', |
143
|
|
|
|
|
|
|
-task => '[build|web|reports]'); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Function : create a new Audio::DB:: object |
146
|
|
|
|
|
|
|
Returns : new Audio::DB::Build,Audio::DB:Web,or Audio::DB::Reports object |
147
|
|
|
|
|
|
|
Args : lists of adaptors and arguments |
148
|
|
|
|
|
|
|
Status : Public |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
These are the arguments: |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
-adaptor Name of the adaptor module to use. Currently only supports |
153
|
|
|
|
|
|
|
dbi:mysql. defaults to dbi:mysql if not provided. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
-dsn The DBI data source, e.g. 'music_db'. Can also be specified |
156
|
|
|
|
|
|
|
as -database. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
-create Optional. If passed boolean true, the database will be created |
159
|
|
|
|
|
|
|
if it does not already exist. (Requires create privileges for the |
160
|
|
|
|
|
|
|
provided user). |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
The commonly used dbi-adaptor is passed the following arguments via the |
163
|
|
|
|
|
|
|
new method: |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
-user[name] username for authentication |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
-pass[word] the password for authentication |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
-host where the database lives |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Any other named argument pairs are passed to |
172
|
|
|
|
|
|
|
the adaptor for processing. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
The adaptor argument must correspond to a module contained within the |
175
|
|
|
|
|
|
|
Audio::DB::Adaptor namespace. For example, the |
176
|
|
|
|
|
|
|
Audio::DB::Adaptor::dbi::mysql adaptor is loaded by specifying |
177
|
|
|
|
|
|
|
'dbi::mysql'. By Perl convention, the adaptors names are lower case |
178
|
|
|
|
|
|
|
because they are loaded at run time. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Audio::DB currently supports dbi::mysql and dbi::sqlite. dbi::sqlite |
181
|
|
|
|
|
|
|
is a small standalone SQL server that is usually sufficient for most |
182
|
|
|
|
|
|
|
Audio::DB tasks. This makes it the perfect option for including |
183
|
|
|
|
|
|
|
Audio::DB in embedded applications. If you are interested in adding |
184
|
|
|
|
|
|
|
other adaptors, please contact me. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 SEE ALSO |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Also see the documentation for Audio::DB::Build for information on building |
189
|
|
|
|
|
|
|
databases, Audio::DB::Web, a module that provides a web browsable interface to |
190
|
|
|
|
|
|
|
the database, and Audio::DB::Reports, for methods that generate reports |
191
|
|
|
|
|
|
|
on your collection. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 BUGS |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
This module implements a fairly complex internal data structure, which |
196
|
|
|
|
|
|
|
in itself rests upon lots of things going right, like reading ID3 |
197
|
|
|
|
|
|
|
tags, tag naming conventions, etc. On top of that, I wrote most of |
198
|
|
|
|
|
|
|
this in a Starbucks full of screaming children. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 TODO |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Need a resonable way of dealing with tags that can't be read |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Lots of error checking needs to be added. Support for custom data schemas, |
205
|
|
|
|
|
|
|
including new data types like more extensive artist info, paths to images, |
206
|
|
|
|
|
|
|
etc. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Keep track of stats for updates. |
209
|
|
|
|
|
|
|
Fix update - needs to use mysql (these are the _check_artist_db routines that |
210
|
|
|
|
|
|
|
all need to be implemented) |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Robusticize new for different adaptor types |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Add in full MP4 support |
215
|
|
|
|
|
|
|
make the data dumps rely on the schema in the module |
216
|
|
|
|
|
|
|
put the schema into its own module |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 AUTHOR |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Copyright 2002-2004, Todd W. Harris . |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
This module is distributed under the same terms as Perl itself. Feel |
223
|
|
|
|
|
|
|
free to use, modify and redistribute it as long as you retain the |
224
|
|
|
|
|
|
|
correct attribution. |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Chris Nandor wrote MP3::Info, the module responsible for |
230
|
|
|
|
|
|
|
reading MP3 tags. Without, this module would be a best-selling pulp |
231
|
|
|
|
|
|
|
romance novel behind the gum at the grocery store checkout. Chris has |
232
|
|
|
|
|
|
|
been really helpful with issues that arose with various MP3 tags from |
233
|
|
|
|
|
|
|
different taggers. Kudos, dude! |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Lincoln (Dr. Leichtenstein) Stein wrote much of the original |
236
|
|
|
|
|
|
|
adaptor code as part of the l module. Much of that code is |
237
|
|
|
|
|
|
|
incorporated here, albeit in a pared-down form. The code for reading ID3 tags |
238
|
|
|
|
|
|
|
from files only with appropriate MIME-types is borrowed from his |
239
|
|
|
|
|
|
|
module. This was a much more elegant than my lame solution of checking for .mp3! |
240
|
|
|
|
|
|
|
Lincoln tolerates having me in his lab, too, even though I use a Mac. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=cut |