-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProtocolHandler.pm
294 lines (220 loc) · 7.63 KB
/
ProtocolHandler.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package Plugins::SqueezeCloud::ProtocolHandler;
use strict;
use base qw(Slim::Player::Protocols::HTTP);
use List::Util qw(min max);
use LWP::Simple;
use LWP::UserAgent;
use HTML::Parser;
use URI::Escape;
use JSON::XS::VersionOneAndTwo;
use XML::Simple;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Slim::Utils::Errno;
use Slim::Utils::Cache;
use Scalar::Util qw(blessed);
my $log = logger('plugin.squeezecloud');
my $prefs = preferences('plugin.squeezecloud');
my %fetching; # hash of ids we are fetching metadata for to avoid multiple fetches
Slim::Player::ProtocolHandlers->registerHandler('soundcloud', __PACKAGE__);
use strict;
use base 'Slim::Player::Protocols::HTTP';
my $CLIENT_ID = "ff21e0d51f1ea3baf9607a1d072c564f";
my $prefs = preferences('plugin.squeezecloud');
sub canSeek { 1 }
sub addClientId {
my ($url) = shift;
my $prefix = "?";
if ($url =~ /\?/) {
my $prefix = "&";
}
my $decorated = $url . $prefix . "client_id=$CLIENT_ID";
if (0 && $prefs->get('apiKey')) {
my $decorated = $url . $prefix . "oauth_token=" . $prefs->get('apiKey');
$log->info($decorated);
}
return $decorated;
}
sub _makeMetadata {
my ($json) = shift;
my $stream = addClientId($json->{'stream_url'});
$stream =~ s/https/http/;
my $DATA = {
duration => int($json->{'duration'} / 1000),
name => $json->{'title'},
title => $json->{'title'},
artist => $json->{'user'}->{'username'},
#type => 'soundcloud',
#play => $stream,
#url => $json->{'permalink_url'},
#link => "soundcloud://" . $json->{'id'},
bitrate => '128k',
type => 'MP3 stream (soundcloud.com)',
#info_link => $json->{'permalink_url'},
icon => $json->{'artwork_url'} || "",
image => $json->{'artwork_url'} || "",
cover => $json->{'artwork_url'} || "",
};
}
sub getFormatForURL () { 'mp3' }
sub isRemote { 1 }
sub scanUrl {
my ($class, $url, $args) = @_;
$args->{cb}->( $args->{song}->currentTrack() );
}
sub gotNextTrack {
my $http = shift;
my $client = $http->params->{client};
my $song = $http->params->{song};
my $url = $song->currentTrack()->url;
my $track = eval { from_json( $http->content ) };
if ( $@ || $track->{error} ) {
# We didn't get the next track to play
if ( $log->is_warn ) {
$log->warn( 'Soundcloud error getting next track: ' . ( $@ || $track->{error} ) );
}
if ( $client->playingSong() ) {
$client->playingSong()->pluginData( {
songName => $@ || $track->{error},
} );
}
$http->params->{'errorCallback'}->( 'PLUGIN_SQUEEZECLOUD_NO_INFO', $track->{error} );
return;
}
# Save metadata for this track
$song->pluginData( $track );
my $stream = addClientId($track->{'stream_url'});
$stream =~ s/https/http/;
$log->info($stream);
my $ua = LWP::UserAgent->new(
requests_redirectable => [],
);
my $res = $ua->get($stream);
my $redirector = $res->header( 'location' );
$song->streamUrl($redirector);
my $meta = _makeMetadata($track);
$song->duration( $meta->{duration} );
my $cache = Slim::Utils::Cache->new;
$log->info("setting ". 'soundcloud_meta_' . $track->{id});
$cache->set( 'soundcloud_meta_' . $track->{id}, $meta, 86400 );
$http->params->{callback}->();
}
sub gotNextTrackError {
my $http = shift;
$http->params->{errorCallback}->( 'PLUGIN_SQUEEZECLOUD_ERROR', $http->error );
}
sub getNextTrack {
my ($class, $song, $successCb, $errorCb) = @_;
my $client = $song->master();
my $url = $song->currentTrack()->url;
# Get next track
my ($id) = $url =~ m{^soundcloud://(.*)$};
# Talk to SN and get the next track to play
my $trackURL = addClientId("http://api.soundcloud.com/tracks/" . $id . ".json");
my $http = Slim::Networking::SimpleAsyncHTTP->new(
\&gotNextTrack,
\&gotNextTrackError,
{
client => $client,
song => $song,
callback => $successCb,
errorCallback => $errorCb,
timeout => 35,
},
);
main::DEBUGLOG && $log->is_debug && $log->debug("Getting track from soundcloud for $id");
$http->get( $trackURL );
}
# To support remote streaming (synced players, slimp3/SB1), we need to subclass Protocols::HTTP
sub new {
my $class = shift;
my $args = shift;
my $client = $args->{client};
my $song = $args->{song};
my $streamUrl = $song->streamUrl() || return;
my $track = $song->pluginData();
$log->info( 'Remote streaming Soundcloud track: ' . $streamUrl );
my $sock = $class->SUPER::new( {
url => $streamUrl,
song => $song,
client => $client,
} ) || return;
${*$sock}{contentType} = 'audio/mpeg';
return $sock;
}
# Track Info menu
sub trackInfo {
my ( $class, $client, $track ) = @_;
my $url = $track->url;
$log->info("trackInfo: " . $url);
}
# Track Info menu
sub trackInfoURL {
my ( $class, $client, $url ) = @_;
$log->info("trackInfoURL: " . $url);
}
use Data::Dumper;
# Metadata for a URL, used by CLI/JSON clients
sub getMetadataFor {
my ( $class, $client, $url ) = @_;
return {} unless $url;
#$log->info("metadata: " . $url);
my $icon = $class->getIcon();
my $cache = Slim::Utils::Cache->new;
# If metadata is not here, fetch it so the next poll will include the data
my ($trackId) = $url =~ m{soundcloud://(.+)};
#$log->info("looking for ". 'soundcloud_meta_' . $trackId );
my $meta = $cache->get( 'soundcloud_meta_' . $trackId );
if ( !$meta && !$client->master->pluginData('fetchingMeta') ) {
# Go fetch metadata for all tracks on the playlist without metadata
my @need;
for my $track ( @{ Slim::Player::Playlist::playList($client) } ) {
my $trackURL = blessed($track) ? $track->url : $track;
if ( $trackURL =~ m{soundcloud://(.+)} ) {
my $id = $1;
if ( !$cache->get("soundcloud_meta_$id") ) {
push @need, $id;
}
}
}
if ( main::DEBUGLOG && $log->is_debug ) {
$log->debug( "Need to fetch metadata for: " . join( ', ', @need ) );
}
# $client->master->pluginData( fetchingMeta => 1 );
# my $metaUrl = Slim::Networking::SqueezeNetwork->url(
# "/api/classical/v1/playback/getBulkMetadata"
# );
# my $http = Slim::Networking::SqueezeNetwork->new(
# \&_gotBulkMetadata,
# \&_gotBulkMetadataError,
# {
# client => $client,
# timeout => 60,
# },
# );
# $http->post(
# $metaUrl,
# 'Content-Type' => 'application/x-www-form-urlencoded',
# 'trackIds=' . join( ',', @need ),
# );
}
#$log->debug( "Returning metadata for: $url" . ($meta ? '' : ': default') );
return $meta || {
type => 'MP3 stream (soundcloud.com)',
icon => $icon,
cover => $icon,
};
}
sub canDirectStreamSong {
my ( $class, $client, $song ) = @_;
# We need to check with the base class (HTTP) to see if we
# are synced or if the user has set mp3StreamingMethod
return $class->SUPER::canDirectStream( $client, $song->streamUrl(), $class->getFormatForURL() );
}
# If an audio stream fails, keep playing
sub handleDirectError {
my ( $class, $client, $url, $response, $status_line ) = @_;
main::INFOLOG && $log->info("Direct stream failed: $url [$response] $status_line");
$client->controller()->playerStreamingFailed( $client, 'PLUGIN_SQUEEZECLOUD_STREAM_FAILED' );
}
1;