source: mp3-find/trunk/lib/MP3/Find/Filesystem.pm @ 39

Last change on this file since 39 was 39, checked in by peter, 13 years ago

Add the META.yml metadata file to version control

File size: 4.3 KB
Line 
1package MP3::Find::Filesystem;
2
3use strict;
4use warnings;
5
6use base 'MP3::Find::Base';
7
8use File::Find;
9use MP3::Info;
10use Scalar::Util qw(looks_like_number);
11
12use MP3::Find::Util qw(get_mp3_metadata);
13
14# XXX: this may or may not lead to faster searches
15eval {
16    require Sort::Key;
17    Sort::Key->import(qw(multikeysorter));
18    require Sort::Key::Natural;
19};
20my $USE_SORT_KEY = $@ ? 0 : 1;
21
22use_winamp_genres();
23
24sub 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
86sub 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
1081;
109
110=head1 NAME
111
112MP3::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
130L<File::Find>, L<MP3::Info>, L<Scalar::Util>
131
132=head1 DESCRIPTION
133
134This module implements the C<search> method from L<MP3::Find::Base>
135using a L<File::Find> based search of the local filesystem.
136
137In addition to just the basic ID3v1 tags, you can search for files by their
138ID3v2 data, using the four-character frame names.  This isn't very useful if
139you are just search by artist or title, but if, for example, you have made use
140of the C<TOPE> ("Orignal Performer") frame, you could search for all the cover
141songs in your collection:
142
143    $finder->find_mp3s(query => { tope => '.' });
144
145As with the basic query keys, ID3v2 query keys are converted to uppercase
146internally.
147
148=head2 Special Options
149
150=over
151
152=item C<exclude_path>
153
154Scalar or arrayref; any file whose name matches any of these paths will be
155skipped.
156
157=back
158
159=head1 SEE ALSO
160
161L<MP3::Find>, L<MP3::Find::DB>
162
163=head1 AUTHOR
164
165Peter Eichman <peichman@cpan.org>
166
167=head1 COPYRIGHT AND LICENSE
168
169Copyright (c) 2006-2008 by Peter Eichman. All rights reserved.
170
171This program is free software; you can redistribute it and/or
172modify it under the same terms as Perl itself.
173
174=cut
Note: See TracBrowser for help on using the repository browser.