forked from LMS-Community/plugin-Qobuz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LastMix.pm
77 lines (50 loc) · 1.81 KB
/
LastMix.pm
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package Plugins::Qobuz::LastMix;
use strict;
use base qw(Plugins::LastMix::Services::Base);
use Slim::Utils::Prefs;
my $prefs = preferences('plugin.qobuz');
sub isEnabled {
my ($class, $client) = @_;
return unless $client;
return unless Slim::Utils::PluginManager->isEnabled('Plugins::Qobuz::Plugin');
require Plugins::Qobuz::API::Common;
return Plugins::Qobuz::API::Common->hasAccount() ? 'Qobuz' : undef;
}
sub lookup {
my ($class, $client, $cb, $args) = @_;
$class->client($client) if $client;
$class->cb($cb) if $cb;
$class->args($args) if $args;
Plugins::Qobuz::Plugin::getAPIHandler($client)->search(sub {
my $searchResult = shift;
if (!$searchResult) {
$class->cb->();
}
my $candidates = [];
my $searchArtist = $class->args->{artist};
my %tracks;
for my $track ( @{ Plugins::Qobuz::API::Common->filterPlayables($searchResult->{tracks}->{items}) } ) {
next unless $track->{performer} && $track->{id} && $track->{title};
my $artist = '';
$artist = $track->{album}->{artist}->{name} if $track->{album} && $track->{album}->{artist};
$artist = $track->{performer}->{name} if $artist !~ /\Q$searchArtist\E/i && $track->{performer}->{name} =~ /\Q$searchArtist\E/i;
$artist = $track->{composer}->{name} if $artist !~ /\Q$searchArtist\E/i && $track->{composer}->{name} =~ /\Q$searchArtist\E/i;
next unless $artist;
my $url = Plugins::Qobuz::API::Common->getUrl($client, $track);
$tracks{$url} = $track;
push @$candidates, {
title => $track->{title},
artist => $artist,
url => $url,
};
}
my $track = $class->extractTrack($candidates);
Plugins::Qobuz::API::Common->precacheTrack($tracks{$track}) if $tracks{$track};
$class->cb->($track);
}, $class->args->{title}, 'tracks', {
_dontPreCache => 1,
limit => 20,
});
}
sub protocol { 'qobuz' }
1;