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

Last change on this file since 14 was 14, checked in by peter, 19 years ago
  • added support for searching by ID3v2 frames using MP3::Tag to the Filesystem.pm backend
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_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    if ($$options{exclude_path}) {
33        my $ref = ref $$options{exclude_path};
34        if ($ref eq 'ARRAY') {
35            $$options{exclude_path} = '(' . join('|', @{ $$options{exclude_path} }) . ')';
36        }
37        unless ($ref eq 'Regexp') {
38            $$options{exclude_path} = qr[$$options{exclude_path}];
39        }
40    }
41   
42    # run the actual find
43    my @results;
44    find(sub { match_mp3($File::Find::name, $query, \@results, $options) }, $_) foreach @$dirs;
45   
46    # sort the results
47    if (@$sort) {
48        @results = sort {
49            my $compare;
50            foreach (map { uc } @$sort) {
51                # use Scalar::Util to do the right sort of comparison
52                $compare = (looks_like_number($a->{$_}) && looks_like_number($b->{$_})) ?
53                    $a->{$_} <=> $b->{$_} :
54                    $a->{$_} cmp $b->{$_};
55                # we found a field they differ on
56                last if $compare;
57            }
58            return $compare;
59        } @results;
60    }
61   
62    return @results
63}
64
65sub match_mp3 {
66    my ($filename, $query, $results, $options) = @_;
67   
68    return unless $filename =~ m{[^/]\.mp3$};
69    if ($$options{exclude_path}) {
70        return if $filename =~ $$options{exclude_path};
71    }
72   
73    my $mp3 = {
74        FILENAME => $filename,
75        %{ get_mp3tag($filename)  || {} },
76        %{ get_mp3info($filename) || {} },
77    };
78   
79    if ($$options{use_id3v2}) {
80        require MP3::Tag;
81        # add ID3v2 tag info, if present
82        my $mp3_tags = MP3::Tag->new($filename);
83        $mp3_tags->get_tags;
84        if (my $id3v2 = $mp3_tags->{ID3v2}) {
85            for my $frame_id (keys %{ $id3v2->get_frame_ids }) {
86                my ($info) = $id3v2->get_frame($frame_id);
87                if (ref $info eq 'HASH') {
88                    #TODO: how should we handle these?
89                } else {
90                    $mp3->{$frame_id} = $info;
91                }
92            }
93        }
94    }
95
96    for my $field (keys(%{ $query })) {
97        my $value = $mp3->{uc($field)};
98        return unless defined $value;
99        return unless $value =~ $query->{$field};
100    }
101   
102    push @{ $results }, $mp3;
103}
104
105# module return
1061;
107
108=head1 NAME
109
110MP3::Find::Filesystem - File::Find-based backend to MP3::Find
111
112=head1 SYNOPSIS
113
114    use MP3::Find::Filesystem;
115    my $finder = MP3::Find::Filesystem->new;
116   
117    my @mp3s = $finder->find_mp3s(
118        dir => '/home/peter/music',
119        query => {
120            artist => 'ilyaimy',
121            album  => 'myxomatosis',
122        },
123        ignore_case => 1,
124    );
125
126=head1 REQUIRES
127
128L<File::Find>, L<MP3::Info>, L<Scalar::Util>
129
130L<MP3::Tag> is also needed if you want to search using ID3v2 tags.
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
137=head2 Special Options
138
139=over
140
141=item C<exclude_path>
142
143Scalar or arrayref; any file whose name matches any of these paths
144will be skipped.
145
146=item C<use_id3v2>
147
148Boolean, defaults to false. If set to true, MP3::Find::Filesystem will
149use L<MP3::Tag> to get the ID3v2 tag for each file. You can then search
150for files by their ID3v2 data, using the four-character frame names.
151This isn't very useful if you are just search by artist or title, but if,
152for example, you have made use of the C<TOPE> ("Orignal Performer") frame,
153you could search for all the cover songs in your collection:
154
155    $finder->find_mp3s(query => { tope => '.' });
156
157As with the basic query keys, ID3v2 query keys are converted to uppercase
158internally.
159
160=back
161
162=head1 SEE ALSO
163
164L<MP3::Find>, L<MP3::Find::DB>
165
166=head1 AUTHOR
167
168Peter Eichman <peichman@cpan.org>
169
170=head1 COPYRIGHT AND LICENSE
171
172Copyright (c) 2006 by Peter Eichman. All rights reserved.
173
174This program is free software; you can redistribute it and/or
175modify it under the same terms as Perl itself.
176
177=cut
Note: See TracBrowser for help on using the repository browser.