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

Last change on this file since 30 was 29, checked in by peter, 19 years ago
  • factored out 'get_mp3_metadata' function from Filesystem.pm to Util.pm
  • added 'update' function to DB.pm that combines 'update_db' and 'update_file' (for updating just specific files); idea courtesy of Matt Dietrich
  • modified mp3db to let you mix and match files and directories on the command line; now also uses the 'update' function in DB.pm
File size: 4.6 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
14eval {
15    require Sort::Key;
16    Sort::Key->import(qw(multikeysorter));
17    use Sort::Key::Natural;
18};
19my $USE_SORT_KEY = $@ ? 0 : 1;
20
21
22eval { require MP3::Tag };
23my $CAN_USE_ID3V2 = $@ ? 0 : 1;
24
25use_winamp_genres();
26
27sub 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
94sub 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
1171;
118
119=head1 NAME
120
121MP3::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
139L<File::Find>, L<MP3::Info>, L<Scalar::Util>
140
141L<MP3::Tag> is also needed if you want to search using ID3v2 tags.
142
143=head1 DESCRIPTION
144
145This module implements the C<search> method from L<MP3::Find::Base>
146using 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
154Scalar or arrayref; any file whose name matches any of these paths
155will be skipped.
156
157=item C<use_id3v2>
158
159Boolean, defaults to false. If set to true, MP3::Find::Filesystem will
160use L<MP3::Tag> to get the ID3v2 tag for each file. You can then search
161for files by their ID3v2 data, using the four-character frame names.
162This isn't very useful if you are just search by artist or title, but if,
163for example, you have made use of the C<TOPE> ("Orignal Performer") frame,
164you could search for all the cover songs in your collection:
165
166    $finder->find_mp3s(query => { tope => '.' });
167
168As with the basic query keys, ID3v2 query keys are converted to uppercase
169internally.
170
171=back
172
173=head1 SEE ALSO
174
175L<MP3::Find>, L<MP3::Find::DB>
176
177=head1 AUTHOR
178
179Peter Eichman <peichman@cpan.org>
180
181=head1 COPYRIGHT AND LICENSE
182
183Copyright (c) 2006 by Peter Eichman. All rights reserved.
184
185This program is free software; you can redistribute it and/or
186modify it under the same terms as Perl itself.
187
188=cut
Note: See TracBrowser for help on using the repository browser.