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