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