1 | package MP3::Find::Filesystem; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | use base 'MP3::Find::Base'; |
---|
7 | |
---|
8 | use File::Find; |
---|
9 | use MP3::Info; |
---|
10 | use Scalar::Util qw(looks_like_number); |
---|
11 | |
---|
12 | use MP3::Find::Util qw(get_mp3_metadata); |
---|
13 | |
---|
14 | eval { |
---|
15 | require Sort::Key; |
---|
16 | Sort::Key->import(qw(multikeysorter)); |
---|
17 | require Sort::Key::Natural; |
---|
18 | }; |
---|
19 | my $USE_SORT_KEY = $@ ? 0 : 1; |
---|
20 | |
---|
21 | |
---|
22 | eval { require MP3::Tag }; |
---|
23 | my $CAN_USE_ID3V2 = $@ ? 0 : 1; |
---|
24 | |
---|
25 | use_winamp_genres(); |
---|
26 | |
---|
27 | sub search { |
---|
28 | my $self = shift; |
---|
29 | my ($query, $dirs, $sort, $options) = @_; |
---|
30 | |
---|
31 | # prep the search patterns as regexes |
---|
32 | foreach (keys(%$query)) { |
---|
33 | my $ref = ref $$query{$_}; |
---|
34 | # make arrays into 'OR' searches |
---|
35 | if ($ref eq 'ARRAY') { |
---|
36 | $$query{$_} = '(' . join('|', @{ $$query{$_} }) . ')'; |
---|
37 | } |
---|
38 | # convert to a regex unless it already IS a regex |
---|
39 | unless ($ref eq 'Regexp') { |
---|
40 | $$query{$_} = "^$$query{$_}\$" if $$options{exact_match}; |
---|
41 | $$query{$_} = $$options{ignore_case} ? qr[$$query{$_}]i : qr[$$query{$_}]; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | if ($$options{exclude_path}) { |
---|
46 | my $ref = ref $$options{exclude_path}; |
---|
47 | if ($ref eq 'ARRAY') { |
---|
48 | $$options{exclude_path} = '(' . join('|', @{ $$options{exclude_path} }) . ')'; |
---|
49 | } |
---|
50 | unless ($ref eq 'Regexp') { |
---|
51 | $$options{exclude_path} = qr[$$options{exclude_path}]; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | if ($$options{use_id3v2} and not $CAN_USE_ID3V2) { |
---|
56 | # they want to use ID3v2, but don't have MP3::Tag |
---|
57 | warn "MP3::Tag is required to search ID3v2 tags\n"; |
---|
58 | } |
---|
59 | |
---|
60 | # run the actual find |
---|
61 | my @results; |
---|
62 | find(sub { match_mp3($File::Find::name, $query, \@results, $options) }, $_) foreach @$dirs; |
---|
63 | |
---|
64 | # sort the results |
---|
65 | if (@$sort) { |
---|
66 | if ($USE_SORT_KEY) { |
---|
67 | # use Sort::Key to do a (hopefully!) faster sort |
---|
68 | #TODO: profile this; at first glance, it doesn't actually seem to be any faster |
---|
69 | #warn "Using Sort::Key"; |
---|
70 | my $sorter = multikeysorter( |
---|
71 | sub { my $info = $_; map { $info->{uc $_} } @$sort }, |
---|
72 | map { 'natural' } @$sort |
---|
73 | ); |
---|
74 | @results = $sorter->(@results); |
---|
75 | } else { |
---|
76 | @results = sort { |
---|
77 | my $compare; |
---|
78 | foreach (map { uc } @$sort) { |
---|
79 | # use Scalar::Util to do the right sort of comparison |
---|
80 | $compare = (looks_like_number($a->{$_}) && looks_like_number($b->{$_})) ? |
---|
81 | $a->{$_} <=> $b->{$_} : |
---|
82 | $a->{$_} cmp $b->{$_}; |
---|
83 | # we found a field they differ on |
---|
84 | last if $compare; |
---|
85 | } |
---|
86 | return $compare; |
---|
87 | } @results; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | return @results |
---|
92 | } |
---|
93 | |
---|
94 | sub match_mp3 { |
---|
95 | my ($filename, $query, $results, $options) = @_; |
---|
96 | |
---|
97 | return unless $filename =~ m{[^/]\.mp3$}; |
---|
98 | if ($$options{exclude_path}) { |
---|
99 | return if $filename =~ $$options{exclude_path}; |
---|
100 | } |
---|
101 | |
---|
102 | my $mp3 = get_mp3_metadata({ |
---|
103 | filename => $filename, |
---|
104 | use_id3v2 => $options->{use_id3v2}, |
---|
105 | }); |
---|
106 | |
---|
107 | for my $field (keys(%{ $query })) { |
---|
108 | my $value = $mp3->{uc($field)}; |
---|
109 | return unless defined $value; |
---|
110 | return unless $value =~ $query->{$field}; |
---|
111 | } |
---|
112 | |
---|
113 | push @{ $results }, $mp3; |
---|
114 | } |
---|
115 | |
---|
116 | # module return |
---|
117 | 1; |
---|
118 | |
---|
119 | =head1 NAME |
---|
120 | |
---|
121 | MP3::Find::Filesystem - File::Find-based backend to MP3::Find |
---|
122 | |
---|
123 | =head1 SYNOPSIS |
---|
124 | |
---|
125 | use MP3::Find::Filesystem; |
---|
126 | my $finder = MP3::Find::Filesystem->new; |
---|
127 | |
---|
128 | my @mp3s = $finder->find_mp3s( |
---|
129 | dir => '/home/peter/music', |
---|
130 | query => { |
---|
131 | artist => 'ilyaimy', |
---|
132 | album => 'myxomatosis', |
---|
133 | }, |
---|
134 | ignore_case => 1, |
---|
135 | ); |
---|
136 | |
---|
137 | =head1 REQUIRES |
---|
138 | |
---|
139 | L<File::Find>, L<MP3::Info>, L<Scalar::Util> |
---|
140 | |
---|
141 | L<MP3::Tag> is also needed if you want to search using ID3v2 tags. |
---|
142 | |
---|
143 | =head1 DESCRIPTION |
---|
144 | |
---|
145 | This module implements the C<search> method from L<MP3::Find::Base> |
---|
146 | using a L<File::Find> based search of the local filesystem. |
---|
147 | |
---|
148 | =head2 Special Options |
---|
149 | |
---|
150 | =over |
---|
151 | |
---|
152 | =item C<exclude_path> |
---|
153 | |
---|
154 | Scalar or arrayref; any file whose name matches any of these paths |
---|
155 | will be skipped. |
---|
156 | |
---|
157 | =item C<use_id3v2> |
---|
158 | |
---|
159 | Boolean, defaults to false. If set to true, MP3::Find::Filesystem will |
---|
160 | use L<MP3::Tag> to get the ID3v2 tag for each file. You can then search |
---|
161 | for files by their ID3v2 data, using the four-character frame names. |
---|
162 | This isn't very useful if you are just search by artist or title, but if, |
---|
163 | for example, you have made use of the C<TOPE> ("Orignal Performer") frame, |
---|
164 | you could search for all the cover songs in your collection: |
---|
165 | |
---|
166 | $finder->find_mp3s(query => { tope => '.' }); |
---|
167 | |
---|
168 | As with the basic query keys, ID3v2 query keys are converted to uppercase |
---|
169 | internally. |
---|
170 | |
---|
171 | =back |
---|
172 | |
---|
173 | =head1 SEE ALSO |
---|
174 | |
---|
175 | L<MP3::Find>, L<MP3::Find::DB> |
---|
176 | |
---|
177 | =head1 AUTHOR |
---|
178 | |
---|
179 | Peter Eichman <peichman@cpan.org> |
---|
180 | |
---|
181 | =head1 COPYRIGHT AND LICENSE |
---|
182 | |
---|
183 | Copyright (c) 2006 by Peter Eichman. All rights reserved. |
---|
184 | |
---|
185 | This program is free software; you can redistribute it and/or |
---|
186 | modify it under the same terms as Perl itself. |
---|
187 | |
---|
188 | =cut |
---|