[1] | 1 | package MP3::Find::Filesystem; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | |
---|
| 6 | use base 'MP3::Find::Base'; |
---|
| 7 | |
---|
| 8 | use File::Find; |
---|
| 9 | use MP3::Info; |
---|
| 10 | use Scalar::Util qw(looks_like_number); |
---|
| 11 | |
---|
| 12 | use_winamp_genres(); |
---|
| 13 | |
---|
| 14 | sub 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 | |
---|
[12] | 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 | |
---|
[1] | 42 | # run the actual find |
---|
| 43 | my @results; |
---|
[12] | 44 | find(sub { match_mp3($File::Find::name, $query, \@results, $options) }, $_) foreach @$dirs; |
---|
[1] | 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 | |
---|
| 65 | sub match_mp3 { |
---|
[12] | 66 | my ($filename, $query, $results, $options) = @_; |
---|
[1] | 67 | |
---|
| 68 | return unless $filename =~ m{[^/]\.mp3$}; |
---|
[12] | 69 | if ($$options{exclude_path}) { |
---|
| 70 | return if $filename =~ $$options{exclude_path}; |
---|
| 71 | } |
---|
| 72 | |
---|
[1] | 73 | my $mp3 = { |
---|
| 74 | FILENAME => $filename, |
---|
| 75 | %{ get_mp3tag($filename) || {} }, |
---|
| 76 | %{ get_mp3info($filename) || {} }, |
---|
| 77 | }; |
---|
[14] | 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 | |
---|
[1] | 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 |
---|
| 106 | 1; |
---|
[3] | 107 | |
---|
| 108 | =head1 NAME |
---|
| 109 | |
---|
| 110 | MP3::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 | |
---|
| 128 | L<File::Find>, L<MP3::Info>, L<Scalar::Util> |
---|
| 129 | |
---|
[14] | 130 | L<MP3::Tag> is also needed if you want to search using ID3v2 tags. |
---|
| 131 | |
---|
[3] | 132 | =head1 DESCRIPTION |
---|
| 133 | |
---|
| 134 | This module implements the C<search> method from L<MP3::Find::Base> |
---|
| 135 | using a L<File::Find> based search of the local filesystem. |
---|
| 136 | |
---|
| 137 | =head2 Special Options |
---|
| 138 | |
---|
[12] | 139 | =over |
---|
[3] | 140 | |
---|
[12] | 141 | =item C<exclude_path> |
---|
| 142 | |
---|
| 143 | Scalar or arrayref; any file whose name matches any of these paths |
---|
| 144 | will be skipped. |
---|
| 145 | |
---|
[14] | 146 | =item C<use_id3v2> |
---|
| 147 | |
---|
| 148 | Boolean, defaults to false. If set to true, MP3::Find::Filesystem will |
---|
| 149 | use L<MP3::Tag> to get the ID3v2 tag for each file. You can then search |
---|
| 150 | for files by their ID3v2 data, using the four-character frame names. |
---|
| 151 | This isn't very useful if you are just search by artist or title, but if, |
---|
| 152 | for example, you have made use of the C<TOPE> ("Orignal Performer") frame, |
---|
| 153 | you could search for all the cover songs in your collection: |
---|
| 154 | |
---|
| 155 | $finder->find_mp3s(query => { tope => '.' }); |
---|
| 156 | |
---|
| 157 | As with the basic query keys, ID3v2 query keys are converted to uppercase |
---|
| 158 | internally. |
---|
| 159 | |
---|
| 160 | =back |
---|
| 161 | |
---|
[3] | 162 | =head1 SEE ALSO |
---|
| 163 | |
---|
| 164 | L<MP3::Find>, L<MP3::Find::DB> |
---|
| 165 | |
---|
| 166 | =head1 AUTHOR |
---|
| 167 | |
---|
| 168 | Peter Eichman <peichman@cpan.org> |
---|
| 169 | |
---|
| 170 | =head1 COPYRIGHT AND LICENSE |
---|
| 171 | |
---|
| 172 | Copyright (c) 2006 by Peter Eichman. All rights reserved. |
---|
| 173 | |
---|
| 174 | This program is free software; you can redistribute it and/or |
---|
| 175 | modify it under the same terms as Perl itself. |
---|
| 176 | |
---|
| 177 | =cut |
---|