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