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