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

Last change on this file since 3 was 3, checked in by peter, 18 years ago
  • updated MANIFEST to include all lib/* and Makefile.PL to include bin/mp3find
  • doc updates to *.pm and mp3find
File size: 4.9 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.01';
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
71=head1 REQUIRES
72
73L<File::Find>, L<MP3::Info>, and L<Scalar::Util> are needed for
74the filesystem backend (L<MP3::Find::Filesystem>).
75
76L<DBI>, L<DBD::SQLite>, and L<SQL::Abstract> are needed for the
77database backend (L<MP3::Find::DB>).
78
79=head1 EXPORTS
80
81=head2 find_mp3s
82
83    my @results = find_mp3s(%options);
84
85Takes the following options:
86
87=over
88
89=item C<dir>
90
91Where to start the search. This can either be a single string or
92an arrayref. Defaults to your home directory.
93
94=item C<query>
95
96Hashref of search parameters. Recognized fields are anything that
97L<MP3::Info> knows about. Field names can be given in either upper
98or lower case; C<find_mp3s> will convert them into upper case for
99you. Value may either be strings, which are converted into regular
100exporessions, or may be C<qr[...]> regular expressions already.
101
102=item C<ignore_case>
103
104Ignore case when matching search strings to the ID3 tag values.
105
106=item C<exact_match>
107
108Adds an implicit C<^> and C<$> around each query string. Does nothing
109if the query is already a regular expression.
110
111=item C<sort>
112
113What field or fields to sort the results by. Can either be a single
114scalar field name to sort by, or an arrayref of field names. Again,
115acceptable field names are anything that L<MP3::Info> knows about;
116field names will be converted to upper case as with the C<query>
117option.
118
119=item C<printf>
120
121By default, C<find_mp3s> just returns the list of filenames. The
122C<printf> option allows you to provide a formatting string to apply
123to the data for each file. The style is roughly similar to Perl's
124C<printf> format strings. The following formatting codes are
125recognized:
126
127    %a - artist
128    %t - title
129    %b - album
130    %n - track number
131    %y - year
132    %g - genre
133    %% - literal '%'
134
135Numeric modifers may be used in the same manner as with C<%s> in
136Perl's C<printf>.
137
138=item C<no_format>
139
140Causes C<find_mp3s> to return an array of hashrefs instead of an array
141of (formatted) strings. Each hashref consists of the key-value pairs
142from C<MP3::Info::get_mp3_tag> and C<MP3::Info::get_mp3_info>, plus
143the key C<FILENAME> (with the obvious value ;-)
144
145    @results = (
146        {
147            FILENAME => ...,
148            TITLE    => ...,
149            ARTIST   => ...,
150            ...
151            SECS     => ...,
152            BITRATE  => ...,
153            ...
154        },
155        ...
156    );
157
158=back
159
160=head1 BUGS
161
162There are probably some in there; let me know if you find any (patches
163welcome).
164
165=head1 TODO
166
167Better tests, using some actual sample mp3 files.
168
169Other backends (a caching filesystem backend, perhaps?)
170
171=head1 SEE ALSO
172
173L<MP3::Find::Filesystem>, L<MP3::Find::DB>
174
175L<mp3find> is the command line frontend to this module (it
176currently only uses the filesystem backend).
177
178See L<MP3::Info> for more information about the fields you can
179search and sort on.
180
181L<File::Find::Rule::MP3Info> is another way to search for MP3
182files based on their ID3 tags.
183
184=head1 AUTHOR
185
186Peter Eichman <peichman@cpan.org>
187
188=head1 COPYRIGHT AND LICENSE
189
190Copyright (c) 2006 by Peter Eichman. All rights reserved.
191
192This program is free software; you can redistribute it and/or
193modify it under the same terms as Perl itself.
194
195=cut
Note: See TracBrowser for help on using the repository browser.