line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MP3::Find::Filesystem; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
21440
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
64
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
10
|
use base 'MP3::Find::Base'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
980
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
11
|
use File::Find; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
145
|
|
9
|
2
|
|
|
2
|
|
2217
|
use MP3::Info; |
|
2
|
|
|
|
|
73838
|
|
|
2
|
|
|
|
|
183
|
|
10
|
2
|
|
|
2
|
|
18
|
use Scalar::Util qw(looks_like_number); |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
188
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
1141
|
use MP3::Find::Util qw(get_mp3_metadata); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1677
|
|
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
|
4
|
|
|
4
|
1
|
7
|
my $self = shift; |
29
|
4
|
|
|
|
|
8
|
my ($query, $dirs, $sort, $options) = @_; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# prep the search patterns as regexes |
32
|
4
|
|
|
|
|
10
|
foreach (keys(%$query)) { |
33
|
0
|
|
|
|
|
0
|
my $ref = ref $$query{$_}; |
34
|
|
|
|
|
|
|
# make arrays into 'OR' searches |
35
|
0
|
0
|
|
|
|
0
|
if ($ref eq 'ARRAY') { |
36
|
0
|
|
|
|
|
0
|
$$query{$_} = '(' . join('|', @{ $$query{$_} }) . ')'; |
|
0
|
|
|
|
|
0
|
|
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
# convert to a regex unless it already IS a regex |
39
|
0
|
0
|
|
|
|
0
|
unless ($ref eq 'Regexp') { |
40
|
0
|
0
|
|
|
|
0
|
$$query{$_} = "^$$query{$_}\$" if $$options{exact_match}; |
41
|
0
|
0
|
|
|
|
0
|
$$query{$_} = $$options{ignore_case} ? qr[$$query{$_}]i : qr[$$query{$_}]; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
4
|
100
|
|
|
|
17
|
if ($$options{exclude_path}) { |
46
|
2
|
|
|
|
|
6
|
my $ref = ref $$options{exclude_path}; |
47
|
2
|
100
|
|
|
|
6
|
if ($ref eq 'ARRAY') { |
48
|
1
|
|
|
|
|
2
|
$$options{exclude_path} = '(' . join('|', @{ $$options{exclude_path} }) . ')'; |
|
1
|
|
|
|
|
3
|
|
49
|
|
|
|
|
|
|
} |
50
|
2
|
50
|
|
|
|
7
|
unless ($ref eq 'Regexp') { |
51
|
2
|
|
|
|
|
36
|
$$options{exclude_path} = qr[$$options{exclude_path}]; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
4
|
50
|
33
|
|
|
13
|
if ($$options{use_id3v2} and not $CAN_USE_ID3V2) { |
56
|
|
|
|
|
|
|
# they want to use ID3v2, but don't have MP3::Tag |
57
|
0
|
|
|
|
|
0
|
warn "MP3::Tag is required to search ID3v2 tags\n"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# run the actual find |
61
|
4
|
|
|
|
|
5
|
my @results; |
62
|
4
|
|
|
16
|
|
280
|
find(sub { match_mp3($File::Find::name, $query, \@results, $options) }, $_) foreach @$dirs; |
|
16
|
|
|
|
|
37
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# sort the results |
65
|
4
|
50
|
|
|
|
12
|
if (@$sort) { |
66
|
0
|
0
|
|
|
|
0
|
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
|
0
|
|
|
0
|
|
0
|
sub { my $info = $_; map { $info->{uc $_} } @$sort }, |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
72
|
0
|
|
|
|
|
0
|
map { 'natural' } @$sort |
|
0
|
|
|
|
|
0
|
|
73
|
|
|
|
|
|
|
); |
74
|
0
|
|
|
|
|
0
|
@results = $sorter->(@results); |
75
|
|
|
|
|
|
|
} else { |
76
|
0
|
|
|
|
|
0
|
@results = sort { |
77
|
0
|
|
|
|
|
0
|
my $compare; |
78
|
0
|
|
|
|
|
0
|
foreach (map { uc } @$sort) { |
|
0
|
|
|
|
|
0
|
|
79
|
|
|
|
|
|
|
# use Scalar::Util to do the right sort of comparison |
80
|
0
|
0
|
0
|
|
|
0
|
$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
|
0
|
0
|
|
|
|
0
|
last if $compare; |
85
|
|
|
|
|
|
|
} |
86
|
0
|
|
|
|
|
0
|
return $compare; |
87
|
|
|
|
|
|
|
} @results; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
return @results |
92
|
4
|
|
|
|
|
24
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub match_mp3 { |
95
|
16
|
|
|
16
|
0
|
24
|
my ($filename, $query, $results, $options) = @_; |
96
|
|
|
|
|
|
|
|
97
|
16
|
100
|
|
|
|
640
|
return unless $filename =~ m{[^/]\.mp3$}; |
98
|
4
|
100
|
|
|
|
12
|
if ($$options{exclude_path}) { |
99
|
2
|
50
|
|
|
|
69
|
return if $filename =~ $$options{exclude_path}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
2
|
|
|
|
|
13
|
my $mp3 = get_mp3_metadata({ |
103
|
|
|
|
|
|
|
filename => $filename, |
104
|
|
|
|
|
|
|
use_id3v2 => $options->{use_id3v2}, |
105
|
|
|
|
|
|
|
}); |
106
|
|
|
|
|
|
|
|
107
|
2
|
|
|
|
|
5
|
for my $field (keys(%{ $query })) { |
|
2
|
|
|
|
|
5
|
|
108
|
0
|
|
|
|
|
0
|
my $value = $mp3->{uc($field)}; |
109
|
0
|
0
|
|
|
|
0
|
return unless defined $value; |
110
|
0
|
0
|
|
|
|
0
|
return unless $value =~ $query->{$field}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
2
|
|
|
|
|
4
|
push @{ $results }, $mp3; |
|
2
|
|
|
|
|
79
|
|
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, L, L |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
L is also needed if you want to search using ID3v2 tags. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 DESCRIPTION |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This module implements the C method from L |
146
|
|
|
|
|
|
|
using a L based search of the local filesystem. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 Special Options |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=over |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item C |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Scalar or arrayref; any file whose name matches any of these paths |
155
|
|
|
|
|
|
|
will be skipped. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item C |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Boolean, defaults to false. If set to true, MP3::Find::Filesystem will |
160
|
|
|
|
|
|
|
use L 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 ("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, L |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 AUTHOR |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Peter Eichman |
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 |