source: mp3-find/trunk/lib/MP3/Find.pm @ 10

Last change on this file since 10 was 10, checked in by peter, 19 years ago
  • added test suite for the DB backend
  • doc corrections and updates to Find.pm and Base.pm
  • moved create and update database functions from mp3db to DB.pm
  • added "destroy_db" function to DB.pm
  • documented mp3db
  • set version to 0.02
File size: 5.3 KB
Line 
1package MP3::Find;
2
3use strict;
4use warnings;
5
6use base qw(Exporter);
7use vars qw($VERSION @EXPORT);
8
9use Carp;
10
11$VERSION = '0.02';
12
13@EXPORT = qw(find_mp3s);
14
15my $finder;
16sub import {
17    my $calling_pkg = shift;
18    # default to a filesystem search
19    my $finder_type = shift || 'Filesystem';
20    my $package = "MP3::Find::$finder_type";
21    eval "require $package";
22    croak $@ if $@;
23    $finder = $package->new;
24    __PACKAGE__->export_to_level(1, @EXPORT);
25}
26
27sub find_mp3s { $finder->find_mp3s(@_) }
28
29
30# module return
311;
32
33=head1 NAME
34
35MP3::Find - Search and sort MP3 files based on their ID3 tags
36
37=head1 SYNOPSIS
38
39    # select with backend you want
40    use MP3::Find qw(Filesystem);
41   
42    print "$_\n" foreach find_mp3s(
43        dir => '/home/peter/cds',
44        query => {
45            artist => 'ilyaimy',
46            title => 'deep in the am',
47        },
48        ignore_case => 1,
49        match_words => 1,
50        sort => [qw(year album tracknum)],
51        printf => '%2n. %a - %t (%b: %y)',
52    );
53
54=head1 DESCRIPTION
55
56This module allows you to search for MP3 files by their ID3 tags.
57You can ask for the results to be sorted by one or more of those
58tags, and return either the list of filenames (the deault), a
59C<printf>-style formatted string for each file using its ID3 tags,
60or the actual Perl data structure representing the results.
61
62There are currently two backends to this module: L<MP3::Find::Filesystem>
63and L<MP3::Find::DB>. You choose which one you want by passing its
64name as the argument to you C<use> statement; B<MP3::Find> will look for
65a B<MP3::Find::$BACKEND> module. If no backend name is given, it will
66default to using L<MP3::Find::Filesystem>.
67
68B<Note:> I'm still working out some kinks in the DB backend, so it
69is currently not as stable as the Filesystem backend.
70
71B<Note the second>: This whole project is still in the alpha stage, so
72I can make no guarentees that there won't be significant interface changes
73in the next few versions or so. Also, comments about what about the API
74rocks (or sucks!) are appreciated.
75
76=head1 REQUIRES
77
78L<File::Find>, L<MP3::Info>, and L<Scalar::Util> are needed for
79the filesystem backend (L<MP3::Find::Filesystem>).
80
81L<DBI>, L<DBD::SQLite>, and L<SQL::Abstract> are needed for the
82database backend (L<MP3::Find::DB>).
83
84=head1 EXPORTS
85
86=head2 find_mp3s
87
88    my @results = find_mp3s(%options);
89
90Takes the following options:
91
92=over
93
94=item C<dir>
95
96Arrayref or scalar; tell C<find_mp3s> where to start the search.
97Directories in the arrayref are searched sequentially.
98
99=item C<query>
100
101Hashref of search parameters. Recognized fields are anything that
102L<MP3::Info> knows about. Field names can be given in either upper
103or lower case; C<find_mp3s> will convert them into upper case for
104you. Value may either be strings, which are converted into regular
105exporessions, or may be C<qr/.../> regular expressions already.
106
107=item C<ignore_case>
108
109Boolean, default false; set to a true value to ignore case when
110matching search strings to the ID3 tag values.
111
112=item C<exact_match>
113
114Boolean, default false; set to a true value to add an implicit
115C<^> and C<$> around each query string. Does nothing if the query
116term is already a regular expression.
117
118=item C<sort>
119
120What field or fields to sort the results by. Can either be a single
121scalar field name to sort by, or an arrayref of field names. Again,
122acceptable field names are anything that L<MP3::Info> knows about;
123field names will be converted to upper case as with the C<query>
124option.
125
126=item C<printf>
127
128By default, C<find_mp3s> just returns the list of filenames. The
129C<printf> option allows you to provide a formatting string to apply
130to the data for each file. The style is roughly similar to Perl's
131C<printf> format strings. The following formatting codes are
132recognized:
133
134    %a - artist
135    %t - title
136    %b - album
137    %n - track number
138    %y - year
139    %g - genre
140    %% - literal '%'
141
142Numeric modifers may be used in the same manner as with C<%s> in
143Perl's C<printf>.
144
145=item C<no_format>
146
147Boolean, default false; set to a true value to have C<find_mp3s> to
148return an array of hashrefs instead of an array of (formatted) strings.
149Each hashref consists of the key-value pairs from C<MP3::Info::get_mp3_tag>
150and C<MP3::Info::get_mp3_info>, plus the key C<FILENAME> (with the obvious
151value ;-)
152
153    @results = (
154        {
155            FILENAME => ...,
156            TITLE    => ...,
157            ARTIST   => ...,
158            ...
159            SECS     => ...,
160            BITRATE  => ...,
161            ...
162        },
163        ...
164    );
165
166=back
167
168=head1 BUGS
169
170There are probably some in there; let me know if you find any (patches
171welcome).
172
173=head1 TODO
174
175Better tests, using some actual sample mp3 files.
176
177Other backends (a caching filesystem backend, perhaps?)
178
179=head1 SEE ALSO
180
181L<MP3::Find::Filesystem>, L<MP3::Find::DB>
182
183L<mp3find> is the command line frontend to this module (it
184currently only uses the filesystem backend).
185
186See L<MP3::Info> for more information about the fields you can
187search and sort on.
188
189L<File::Find::Rule::MP3Info> is another way to search for MP3
190files based on their ID3 tags.
191
192=head1 AUTHOR
193
194Peter Eichman <peichman@cpan.org>
195
196=head1 COPYRIGHT AND LICENSE
197
198Copyright (c) 2006 by Peter Eichman. All rights reserved.
199
200This program is free software; you can redistribute it and/or
201modify it under the same terms as Perl itself.
202
203=cut
Note: See TracBrowser for help on using the repository browser.