-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDontStopTheMusic.pm
99 lines (77 loc) · 2.31 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
package Plugins::RadioParadise::DontStopTheMusic;
use strict;
use base qw(Plugins::LastMix::Services::Base);
use JSON::XS::VersionOneAndTwo;
use List::Util qw(min);
use Slim::Utils::Log;
use Plugins::RadioParadise::Favorites;
# XXX - make this a pref?
use constant LOWER_LIMIT => 5;
use constant MAX_ITEMS => 250;
use constant FAVORITES_URL => 'http://api.radioparadise.com/siteapi.php?file=account%%3A%%3Aprofile-favorites&profile_user_id=%s&mode=High&lower_limit=%s&upper_limit=10&list_limit=50&list_offset=%s';
my $log = logger('plugin.radioparadise');
sub please {
my ($client, $cb) = @_;
if (!Plugins::RadioParadise::Favorites->isSignedIn()) {
return $cb->($client, []);
}
my $userId = Plugins::RadioParadise::Favorites->getUserId() || return $cb->($client, []);
my $seedTracks = [];
getFavorites($userId, 0, sub {
my ($songs, $num_songs) = @_;
push @$seedTracks, @$songs;
if ($num_songs > 50) {
if ($num_songs > 100 && Slim::Utils::Versions->compareVersions($::VERSION, '8.0.0') >= 0) {
require Async::Util;
my $iterations = int(min($num_songs, MAX_ITEMS) / 50) - 1;
$iterations++ if $num_songs % 50;
Async::Util::amap(
inputs => [1..$iterations],
action => sub {
my ($input, $acb) = @_;
getFavorites($userId, $input * 50, sub {
my ($moreSongs) = @_;
push @$seedTracks, @$moreSongs;
$acb->();
});
},
output => 0,
cb => sub {
mixIt($client, $cb, $seedTracks);
}
);
}
else {
getFavorites($userId, 50, sub {
my ($moreSongs) = @_;
push @$seedTracks, @$moreSongs;
mixIt($client, $cb, $seedTracks);
});
}
}
else {
mixIt($client, $cb, $seedTracks);
}
});
}
sub mixIt {
my ($client, $cb, $seedTracks) = @_;
Slim::Player::Playlist::fischer_yates_shuffle($seedTracks);
Plugins::LastMix::DontStopTheMusic::please($client, $cb, [ splice(@$seedTracks, 0, 10) ]);
}
sub getFavorites {
my ($userId, $offset, $cb) = @_;
Slim::Networking::SimpleAsyncHTTP->new(
sub {
my ($http) = @_;
my $result = eval { from_json($http->content) } || {};
$cb->($result->{songs} || [], $result->{num_songs});
},
sub {
my ($http, $error) = @_;
$log->error("Failed to look up user favorites: $error" );
$cb->([]);
}
)->get(sprintf(FAVORITES_URL, $userId, LOWER_LIMIT, $offset || 0));
}
1;