File: MpdToys.pm

package info (click to toggle)
mpdtoys 0.24
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 104 kB
  • ctags: 22
  • sloc: perl: 842; sh: 58; makefile: 21
file content (62 lines) | stat: -rw-r--r-- 1,140 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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