| 1 | package App::Pimpd::Playlist::Search; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | BEGIN { | ||
| 5 | use Exporter; | ||
| 6 | use vars qw(@ISA @EXPORT); | ||
| 7 | @ISA = qw(Exporter); | ||
| 8 | @EXPORT = qw( | ||
| 9 | search_playlist | ||
| 10 | search_all_playlists | ||
| 11 | ); | ||
| 12 | } | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use App::Pimpd; | ||
| 16 | use App::Pimpd::Validate; | ||
| 17 | use Term::ExtendedColor; | ||
| 18 | |||
| 19 | |||
| 20 | sub search_playlist { | ||
| 21 | my $query = shift; | ||
| 22 | |||
| 23 | if(!defined($query)) { | ||
| 24 | confess("You must specify a query for search_playlist()"); | ||
| 25 | } | ||
| 26 | |||
| 27 | if(invalid_regex($query)) { | ||
| 28 | $query =~ s;^\Q;;; | ||
| 29 | } | ||
| 30 | |||
| 31 | my %result; | ||
| 32 | for($mpd->playlist->as_items) { | ||
| 33 | my $str = join(' - ', $_->artist, $_->album, $_->title); | ||
| 34 | |||
| 35 | if($str =~ /$query/gpi) { | ||
| 36 | $result{$_->pos} = $_->file; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | return \%result; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub search_all_playlists { | ||
| 45 | my $query = shift; | ||
| 46 | if(!defined($query)) { | ||
| 47 | confess("You must specify a query for search_all_playlists()"); | ||
| 48 | } | ||
| 49 | |||
| 50 | if(invalid_regex($query)) { | ||
| 51 | $query =~ s;\Q;;; | ||
| 52 | } | ||
| 53 | |||
| 54 | my @matched_files; | ||
| 55 | |||
| 56 | open(my $fh, '<', "$config{loved_database}") | ||
| 57 | or print "No DB found\n" and return 1; | ||
| 58 | chomp(my @tracks = <$fh>); | ||
| 59 | |||
| 60 | for(@tracks) { | ||
| 61 | if(invalid_regex($query)) { | ||
| 62 | if($_ =~ /\Q$query/i) { | ||
| 63 | push(@matched_files, $_); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | if($_ =~ /$query/i) { | ||
| 68 | push(@matched_files, $_); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | close($fh); | ||
| 73 | return @matched_files; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | __END__ |