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

Last change on this file since 2 was 1, checked in by peter, 19 years ago

Initial import

File size: 4.1 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    use MP3Find;
40   
41    print "$_\n" foreach find_mp3s(
42        dir => '/home/peter/cds',
43        query => {
44            artist => 'ilyaimy',
45            title => 'deep in the am',
46        },
47        ignore_case => 1,
48        match_words => 1,
49        sort => [qw(year album tracknum)],
50        printf => '%2n. %a - %t (%b: %y)',
51    );
52
53=head1 DESCRIPTION
54
55This module allows you to search for MP3 files by their ID3 tags.
56You can ask for the results to be sorted by one or more of those
57tags, and return either the list of filenames (the deault), a
58C<printf>-style formatted string for each file using its ID3 tags,
59or the actual Perl data structure representing the results.
60
61=head1 REQUIRES
62
63L<File::Find>, L<MP3::Info>, L<Scalar::Util>
64
65L<DBI> and L<DBD::SQLite> are needed if you want to have a
66database backend.
67
68=head1 EXPORTS
69
70=head2 find_mp3s
71
72    my @results = find_mp3s(%options);
73
74Takes the following options:
75
76=over
77
78=item C<dir>
79
80Where to start the search. This can either be a single string or
81an arrayref. Defaults to your home directory.
82
83=item C<query>
84
85Hashref of search parameters. Recognized fields are anything that
86L<MP3::Info> knows about. Field names can be given in either upper
87or lower case; C<find_mp3s> will convert them into upper case for
88you. Value may either be strings, which are converted into regular
89exporessions, or may be C<qr[...]> regular expressions already.
90
91=item C<ignore_case>
92
93Ignore case when matching search strings to the ID3 tag values.
94
95=item C<exact_match>
96
97Adds an implicit C<^> and C<$> around each query string.
98
99=item C<sort>
100
101What field or fields to sort the results by. Can either be a single
102scalar field name to sort by, or an arrayref of field names. Again,
103acceptable field names are anything that L<MP3::Info> knows about.
104
105=item C<printf>
106
107By default, C<find_mp3s> just returns the list of filenames. The
108C<printf> option allows you to provide a formatting string to apply
109to the data for each file. The style is roughly similar to Perl's
110C<printf> format strings. The following formatting codes are
111recognized:
112
113    %a - artist
114    %t - title
115    %b - album
116    %n - track number
117    %y - year
118    %g - genre
119    %% - literal '%'
120
121Numeric modifers may be used in the same manner as with C<%s> in
122Perl's C<printf>.
123
124=item C<no_format>
125
126Causes C<find_mp3s> to return an array of hashrefs instead of an array
127of (formatted) strings. Each hashref consists of the key-value pairs
128from C<MP3::Info::get_mp3_tag> and C<MP3::Info::get_mp3_info>, plus
129the key C<FILENAME> (with the obvious value ;-)
130
131    @results = (
132        {
133            FILENAME => ...,
134            TITLE    => ...,
135            ARTIST   => ...,
136            ...
137            SECS     => ...,
138            BITRATE  => ...,
139            ...
140        },
141        ...
142    );
143
144=back
145
146=head1 TODO
147
148More of a structured query would be nice; currently everything
149is and-ed together, and it would be nice to be able to put query
150keys together with a mixture of and and or.
151
152Searching a big directory is slo-o-ow! Investigate some sort of
153caching of results?
154
155The current sorting function is also probably quite inefficient.
156
157=head1 SEE ALSO
158
159See L<MP3::Info> for more information about the fields you can
160search and sort on.
161
162L<File::Find::Rule::MP3Info> is another way to search for MP3
163files based on their ID3 tags.
164
165=head1 AUTHOR
166
167Peter Eichman <peichman@cpan.org>
168
169=head1 COPYRIGHT AND LICENSE
170
171Copyright (c) 2006 by Peter Eichman. All rights reserved.
172
173This program is free software; you can redistribute it and/or
174modify it under the same terms as Perl itself.
175
176=cut
Note: See TracBrowser for help on using the repository browser.