-
Notifications
You must be signed in to change notification settings - Fork 24
/
DontStopTheMusic.pm
165 lines (138 loc) · 3.81 KB
/
DontStopTheMusic.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package Plugins::Spotty::DontStopTheMusic;
use strict;
use Digest::MD5 qw(md5_hex);
use Slim::Plugin::DontStopTheMusic::Plugin;
use Slim::Schema;
use Slim::Utils::Log;
use Plugins::Spotty::Plugin;
my $log = logger('plugin.spotty');
sub init {
Slim::Plugin::DontStopTheMusic::Plugin->registerHandler('PLUGIN_SPOTTY_RECOMMENDATIONS', \&dontStopTheMusic);
}
sub dontStopTheMusic {
my ($client, $cb) = @_;
my $seedTracks = Slim::Plugin::DontStopTheMusic::Plugin->getMixableProperties($client, 5);
# don't seed from radio stations - only do if we're playing from some track based source
if ($seedTracks && ref $seedTracks && scalar @$seedTracks) {
main::INFOLOG && $log->info("Auto-mixing Spotify tracks from random items in current playlist");
my $spotty = Plugins::Spotty::Plugin->getAPIHandler($client);
if (!$spotty) {
$cb->($client);
return;
}
my @searchData;
my $seedData = {
limit => 25
};
my $getRecommendations = sub {
if ( grep /^seed_/, keys %$seedData ) {
$spotty->recommendations(sub {
$cb->($client, [
map {
$_->{uri} =~ /(track:.*)/;
"spotify://$1";
} @{$_[0] || []}
]);
}, $seedData);
}
else {
$cb->($client);
}
};
foreach my $track ( @$seedTracks ) {
# if this is a RemoteTrack item, we might want to check whether it's a Spotify track already
if ( $track->{id} && $track->{id} =~ /^-\d+$/ ) {
my $trackObj = Slim::Schema->find('Track', $track->{id});
if ($trackObj && $trackObj->url) {
$track->{id} = $trackObj->url;
}
}
if ( $track->{id} && $track->{id} =~ /track:([a-z0-9]+)/i ) {
$seedData->{seed_tracks} ||= [];
push @{$seedData->{seed_tracks}}, $1;
}
# if we haven't found an ID already, build search data
elsif ( $track->{artist} && $track->{title} ) {
push @searchData, [ $track->{artist}, $track->{title} ];
}
}
# if we're not done yet...
if ( scalar @searchData ) {
my $findArtistSeed = sub {
$spotty->search(sub {
my $artists = shift || [];
foreach my $searchData ( @searchData ) {
my $artist = $searchData->[0] || next;
if ( my ($match) = grep {
$_->{name} =~ /\Q$artist\E/i
} @$artists ) {
$seedData->{seed_artists} ||= [];
push @{$seedData->{seed_artists}}, $match->{id};
$searchData = [];
}
}
if ( $seedData->{seed_artists} && scalar @{$seedData->{seed_artists}} > 5 ) {
splice @{$seedData->{seed_artists}}, 5;
}
$getRecommendations->();
},{
series => { map { $_ => {
q => sprintf('artist:"%s"', $_),
type => 'artist',
market => 'from_token',
limit => 5
} } map {
$_->[0];
} grep {
$_->[0]
} @searchData },
type => 'artist'
});
};
$spotty->search(sub {
my $tracks = shift || [];
foreach my $searchData ( @searchData ) {
my ($artist, $title) = @$searchData;
if ( my ($match) = grep {
$_->{name} =~ /^\Q$title\E/i
&& $_->{artists} && grep {
$_->{name} =~ /\Q$artist\E/i
} @{$_->{artists}}
} @$tracks ) {
$seedData->{seed_tracks} ||= [];
push @{$seedData->{seed_tracks}}, $match->{id};
$searchData = [];
}
}
if ( $seedData->{seed_tracks} && scalar @{$seedData->{seed_tracks}} > 5 ) {
splice @{$seedData->{seed_tracks}}, 5;
}
if ( !grep { scalar @{$_} } @searchData ) {
$getRecommendations->();
}
else {
$findArtistSeed->();
}
},{
series => { map {
my $key = $_->[0] . $_->[1];
utf8::encode($key);
md5_hex($key) => {
q => sprintf('%s artist:"%s"', $_->[1], $_->[0]),
type => 'track',
market => 'from_token',
limit => 5
};
} @searchData },
type => 'track'
});
}
else {
$getRecommendations->();
}
}
else {
$cb->($client);
}
}
1;