summaryrefslogtreecommitdiff
path: root/MpdToys.pm
diff options
context:
space:
mode:
Diffstat (limited to 'MpdToys.pm')
-rw-r--r--MpdToys.pm62
1 files changed, 62 insertions, 0 deletions
diff --git a/MpdToys.pm b/MpdToys.pm
new file mode 100644
index 0000000..4290b80
--- /dev/null
+++ b/MpdToys.pm
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+package MpdToys;
+
+sub findmatchingsongs {
+ my $term=shift;
+ my $mpd=shift;
+
+ # pass urls through
+ if ($term=~/^(\w+):\/\//) {
+ return Audio::MPD::Common::Item->new(file => $term);
+ }
+
+ my $coll=$mpd->collection;
+
+ my $exactmatch=$coll->song($term);
+ if ($exactmatch) {
+ return $exactmatch;
+ }
+
+ my @matches = (
+ $coll->songs_from_album_partial($term),
+ $coll->songs_by_artist_partial($term),
+ $coll->songs_with_title_partial($term),
+ );
+
+ return dedupsongs(@matches);
+}
+
+sub canmatch_fuzzy {
+ eval q{use String::Approx 'amatch'};
+ return ! $@;
+}
+
+sub findmatchingsongs_fuzzy {
+ my $term=shift;
+ my $mpd=shift;
+
+ my $coll=$mpd->collection;
+ my @matches = (
+ (map { $coll->songs_from_album($_) }
+ amatch($term, ['i'], $coll->all_albums())),
+ (map { $coll->songs_by_artist($_) }
+ amatch($term, ['i'], $coll->all_artists()))
+ );
+
+ # very slow, only try if nothing else matched
+ @matches = amatch($term, ['i'], $coll->all_songs())
+ if ! @matches;
+
+ return dedupsongs(@matches);
+}
+
+sub dedupsongs {
+ my %seen;
+ my @ret;
+ foreach my $song (@_) {
+ push @ret, $song unless $seen{$song->file}++;
+ }
+ return @ret;
+}
+
+1