-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAPI.pm
220 lines (177 loc) · 4.93 KB
/
API.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package Plugins::RadioParadise::API;
use strict;
use Digest::MD5 qw(md5_hex);
use JSON::XS::VersionOneAndTwo;
use URI ();
use URI::QueryParam;
use Slim::Networking::SimpleAsyncHTTP;
use Slim::Utils::Log;
# from J.F. (RP), October '24:
# "Some of the calls reference a source id. It's just for tracking any issues. Yours is 21."
use constant SOURCE_ID => 21;
use constant BASE_URL => 'http://api.radioparadise.com';
use constant AUTH_URL => BASE_URL . '/api/auth';
use constant CHANNEL_LIST_URL => BASE_URL . '/api/list_chan?C_user_id=%s&ver=2&source=' . SOURCE_ID;
use constant GAPLESS_URL => BASE_URL . '/api/gapless';
use constant UPDATE_HISTORY_URL => BASE_URL . '/api/update_history';
use constant UPDATE_PAUSE_URL => BASE_URL . '/api/update_pause';
use constant FALLBACK_IMG_BASE => '//img.radioparadise.com/';
my $log = logger('plugin.radioparadise');
my ($userId, $countryCode, $imageBase, %maxEventId);
sub auth {
my ($class, $cb) = @_;
main::INFOLOG && $log->is_info && $log->info("Authenticating...");
_get(sub {
my $result = shift;
if ($result && ref $result) {
$userId = $result->{user_id};
$countryCode = $result->{country_code};
}
$cb->($userId);
}, AUTH_URL);
}
sub getRadioStationList {
my ($class, $cb) = @_;
main::INFOLOG && $log->is_info && $log->info("Updating channel list...");
_get(
sub {
my $stationInfo = shift;
$cb->($stationInfo);
},
sprintf(CHANNEL_LIST_URL, $userId),
{
cache => 15 * 60,
}
);
}
sub getNextTrack {
my ($class, $cb, $args) = @_;
main::INFOLOG && $log->is_info && $log->info("Getting track information...");
my $playerId = _getPlayerId($args->{client});
my $channel = $args->{channel} || 0;
my $queryParams = {
C_user_id => $userId,
player_id => $playerId,
chan => $channel,
bitrate => 4,
numSongs => 1,
source => SOURCE_ID,
};
if (my $event = $args->{event}) {
$queryParams->{event} = $event;
}
if (my $action = $args->{action}) {
$queryParams->{action} = $action;
}
_get(
sub {
my $trackInfo = shift;
$imageBase ||= $trackInfo->{image_base};
$maxEventId{$channel} = $trackInfo->{max_gapless_event_id} if $trackInfo->{max_gapless_event_id};
$cb->($trackInfo);
},
GAPLESS_URL,
{
queryParams => $queryParams
}
);
}
sub updateHistory {
my ($class, $cb, $songInfo, $args) = @_;
return unless $songInfo && $songInfo->{event_id} && $songInfo->{song_id} && $args->{client};
my $playerId = _getPlayerId($args->{client});
my $channel = $args->{channel} || 0;
my $position = $args->{position} || 0;
_get(
sub { $cb->(@_) if $cb; },
UPDATE_HISTORY_URL,
{
queryParams => {
C_user_id => $userId,
song_id => $songInfo->{song_id},
player_id => $playerId,
event => $songInfo->{event_id},
chan => $channel,
country_code => $countryCode,
'time' => time() - $position,
playtime_secs => time(),
source => SOURCE_ID,
},
}
);
}
sub updatePause {
my ($class, $cb, $songInfo, $args) = @_;
return unless $songInfo && $songInfo->{event_id} && $songInfo->{song_id} && $args->{client};
my $playerId = _getPlayerId($args->{client});
my $channel = $args->{channel} || 0;
my $position = $args->{position} || 0;
_get(
sub { $cb->(@_) if $cb; },
UPDATE_PAUSE_URL,
{
queryParams => {
pause => $position * 1000,
C_user_id => $userId,
player_id => $playerId,
event => $songInfo->{event_id},
chan => $channel,
playtime_secs => time(),
source => SOURCE_ID,
},
}
);
}
sub getImageUrl {
my ($class, $songInfo) = @_;
return 'http:' . ($imageBase || FALLBACK_IMG_BASE) . ($songInfo->{cover_art} || $songInfo->{cover_large} || $songInfo->{cover_medium} || $songInfo->{cover_small});
}
sub getChannelIdFromUrl {
my ($class, $url) = @_;
my ($channel) = $url =~ m{radioparadise://(?:.+?)-?(\d+)?}i;
return $channel || 0;
}
sub getMaxEventId {
my ($class, $channel) = @_;
return $maxEventId{$channel || 0} || -1;
}
sub _getPlayerId {
my ($client) = shift;
return md5_hex(ref $client ? $client->id : $client);
}
sub _get {
my ($cb, $url, $args) = @_;
my $params = {
cache => 0,
timeout => 15,
};
if (my $ttl = $args->{cache}) {
$params->{cache} = 1;
$params->{expires} = $ttl;
}
if (my $queryParams = $args->{queryParams}) {
my $uri = URI->new($url);
$uri->query_form(%$queryParams);
$url = $uri->as_string();
}
main::INFOLOG && $log->is_info && $log->info("Getting $url");
Slim::Networking::SimpleAsyncHTTP->new(
sub {
my $http = shift;
my $responseBody = eval { from_json($http->content) };
if ($@ && $http->content) {
$log->error("Failed to parse result for $url: $@");
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump($http));
}
main::DEBUGLOG && $log->is_debug && $log->debug('Got response:' . Data::Dump::dump($responseBody));
$cb->($responseBody);
},
sub {
my ($http, $error) = @_;
$log->error("Failed to get $url: $error" );
$cb->()
},
$params,
)->get($url);
}
1;