[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 | |
---|
| 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 | |
---|
| 55 | sub 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 |
---|
| 74 | 1; |
---|
[3] | 75 | |
---|
| 76 | =head1 NAME |
---|
| 77 | |
---|
| 78 | MP3::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 | |
---|
| 96 | L<File::Find>, L<MP3::Info>, L<Scalar::Util> |
---|
| 97 | |
---|
| 98 | =head1 DESCRIPTION |
---|
| 99 | |
---|
| 100 | This module implements the C<search> method from L<MP3::Find::Base> |
---|
| 101 | using a L<File::Find> based search of the local filesystem. |
---|
| 102 | |
---|
| 103 | =head2 Special Options |
---|
| 104 | |
---|
| 105 | There are no special options for B<MP3::Find::Filesystem>. See |
---|
| 106 | L<MP3::Find> for the description of the general options. |
---|
| 107 | |
---|
| 108 | =head1 SEE ALSO |
---|
| 109 | |
---|
| 110 | L<MP3::Find>, L<MP3::Find::DB> |
---|
| 111 | |
---|
| 112 | =head1 AUTHOR |
---|
| 113 | |
---|
| 114 | Peter Eichman <peichman@cpan.org> |
---|
| 115 | |
---|
| 116 | =head1 COPYRIGHT AND LICENSE |
---|
| 117 | |
---|
| 118 | Copyright (c) 2006 by Peter Eichman. All rights reserved. |
---|
| 119 | |
---|
| 120 | This program is free software; you can redistribute it and/or |
---|
| 121 | modify it under the same terms as Perl itself. |
---|
| 122 | |
---|
| 123 | =cut |
---|