App-pimpd
view release on metacpan or search on metacpan
lib/App/Pimpd/Collection/Search.pm view on Meta::CPAN
package App::Pimpd::Collection::Search;
use strict;
BEGIN {
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(
search_db_quick
search_db_artist
search_db_album
search_db_title
);
}
use App::Pimpd;
use App::Pimpd::Validate;
use Carp 'confess';
sub search_db_quick {
my $query = shift;
return if !$query;
if(invalid_regex($query)) {
confess("Invalid regex: '$query'");
}
my @result;
for($mpd->collection->all_pathes) {
if($_ =~ /$query/im) {
push(@result, $_);
}
}
return (wantarray()) ? @result : scalar(@result);
}
sub search_db_artist {
my $artist = shift; # Not a regex
return if !$artist;
my @tracks = $mpd->collection->songs_by_artist_partial($artist);
if(!@tracks) {
return;
}
# check if called from the interactive shell
my $caller_sub = (caller(1))[3];
if($caller_sub =~ m/App::Pimpd::Shell/) {
print "$_\n" for map {
sprintf("%s [%s] %s", $_->artist, $_->album, $_->title);
} @tracks;
}
map{ $_ = $_->file } @tracks;
return (wantarray()) ? @tracks : scalar(@tracks);
}
sub search_db_title {
my $title = shift;
return if !$title;
my @titles = $mpd->collection->songs_with_title_partial($title);
if(!@titles) {
return;
}
# check if called from the interactive shell
my $caller_sub = (caller(1))[3];
if($caller_sub =~ m/App::Pimpd::Shell/) {
print "$_\n" for map {
sprintf("%s [%s] %s", $_->artist, $_->album, $_->title);
} @titles;
}
map{ $_ = $_->file } @titles;
return (wantarray()) ? @titles : scalar(@titles);
}
sub search_db_album {
my $album = shift;
return if !$album;
my @albums = $mpd->collection->songs_from_album_partial($album);
if(!@albums) {
return;
}
# check if called from the interactive shell
my $caller_sub = (caller(1))[3];
if($caller_sub =~ m/App::Pimpd::Shell/) {
print "$_\n" for map {
sprintf("%s [%s] %s", $_->artist, $_->album, $_->title);
} @albums;
}
map { $_ = $_->file } @albums;
return (wantarray()) ? @albums : scalar(@albums);
}
1;
__END__
=pod
=head1 NAME
App::Pimpd::Collection::Search - Package exporting various search functions for
the MPD collection
=head1 SYNOPSIS
use App::Pimpd;
use App::Pimpd::Collection::Search
my @album = search_db_album('Stripped');
my @songs = search_db_quick('love');
=head1 DESCRIPTION
App::Pimpd::Collection::Search provides search functions for the MPD collection
=head1 EXPORTS
=over
=item search_db_quick()
my @paths = search_db_quick('foo');
Parameters: $regex
Returns: @paths
Given a valid regular expression, searches the collection for matching
filenames. The search is performed case insensitive.
In list context, returns full paths for the matched songs.
In scalar context, returns the number of matches.
=item search_db_artist()
my @paths = search_db_artist('Laleh');
Parameters: $string
( run in 0.824 second using v1.01-cache-2.11-cpan-437f7b0c052 )