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

Last change on this file since 3 was 3, checked in by peter, 19 years ago
  • updated MANIFEST to include all lib/* and Makefile.PL to include bin/mp3find
  • doc updates to *.pm and mp3find
File size: 3.0 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_winamp_genres();
13
14sub search {
15    my $self = shift;
16    my ($query, $dirs, $sort, $options) = @_;
17   
18    # prep the search patterns as regexes
19    foreach (keys(%$query)) {
20        my $ref = ref $$query{$_};
21        # make arrays into 'OR' searches
22        if ($ref eq 'ARRAY') {
23            $$query{$_} = '(' . join('|', @{ $$query{$_} }) . ')';
24        }
25        # convert to a regex unless it already IS a regex       
26        unless ($ref eq 'Regexp') {
27            $$query{$_} = "^$$query{$_}\$" if $$options{exact_match};
28            $$query{$_} = $$options{ignore_case} ? qr[$$query{$_}]i : qr[$$query{$_}];
29        }
30    }
31   
32    # run the actual find
33    my @results;
34    find(sub { match_mp3($File::Find::name, $query, \@results) }, $_) foreach @$dirs;
35   
36    # sort the results
37    if (@$sort) {
38        @results = sort {
39            my $compare;
40            foreach (map { uc } @$sort) {
41                # use Scalar::Util to do the right sort of comparison
42                $compare = (looks_like_number($a->{$_}) && looks_like_number($b->{$_})) ?
43                    $a->{$_} <=> $b->{$_} :
44                    $a->{$_} cmp $b->{$_};
45                # we found a field they differ on
46                last if $compare;
47            }
48            return $compare;
49        } @results;
50    }
51   
52    return @results
53}
54
55sub match_mp3 {
56    my ($filename, $query, $results) = @_;
57   
58    return unless $filename =~ m{[^/]\.mp3$};
59    my $mp3 = {
60        FILENAME => $filename,
61        %{ get_mp3tag($filename)  || {} },
62        %{ get_mp3info($filename) || {} },
63    };
64    for my $field (keys(%{ $query })) {
65        my $value = $mp3->{uc($field)};
66        return unless defined $value;
67        return unless $value =~ $query->{$field};
68    }
69   
70    push @{ $results }, $mp3;
71}
72
73# module return
741;
75
76=head1 NAME
77
78MP3::Find::Filesystem - File::Find-based backend to MP3::Find
79
80=head1 SYNOPSIS
81
82    use MP3::Find::Filesystem;
83    my $finder = MP3::Find::Filesystem->new;
84   
85    my @mp3s = $finder->find_mp3s(
86        dir => '/home/peter/music',
87        query => {
88            artist => 'ilyaimy',
89            album  => 'myxomatosis',
90        },
91        ignore_case => 1,
92    );
93
94=head1 REQUIRES
95
96L<File::Find>, L<MP3::Info>, L<Scalar::Util>
97
98=head1 DESCRIPTION
99
100This module implements the C<search> method from L<MP3::Find::Base>
101using a L<File::Find> based search of the local filesystem.
102
103=head2 Special Options
104
105There are no special options for B<MP3::Find::Filesystem>. See
106L<MP3::Find> for the description of the general options.
107
108=head1 SEE ALSO
109
110L<MP3::Find>, L<MP3::Find::DB>
111
112=head1 AUTHOR
113
114Peter Eichman <peichman@cpan.org>
115
116=head1 COPYRIGHT AND LICENSE
117
118Copyright (c) 2006 by Peter Eichman. All rights reserved.
119
120This program is free software; you can redistribute it and/or
121modify it under the same terms as Perl itself.
122
123=cut
Note: See TracBrowser for help on using the repository browser.