forked from findechris/lms_mixcloud
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProtocolHandler.pm
481 lines (393 loc) · 14.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package Plugins::MixCloud::ProtocolHandler;
# Plugin to stream audio from MixCloud streams
#
# Released under GNU General Public License version 2 (GPLv2)
#
# Written by Christian Mueller (first release),
# Daniel Vijge (improvements),
# KwarkLabs (added functionality)
#
# See file LICENSE for full license details
use strict;
use vars qw(@ISA);
use base qw(Slim::Player::Protocols::HTTPS);
use List::Util qw(min max);
use HTML::Parser;
use URI::Escape;
use JSON::XS::VersionOneAndTwo;
use XML::Simple;
use IO::Socket qw(:crlf);
use Data::Dump qw(dump);
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Slim::Utils::Errno;
use Slim::Utils::Cache;
use Scalar::Util qw(blessed);
use Slim::Utils::Strings qw(string cstring);
use constant PAGE_URL_REGEXP => qr{^https?://(?:www|m)\.mixcloud\.com/};
use constant USER_AGENT => 'Mozilla/5.0 (X11; Linux x86_64; rv:124.0; SlimServer) Gecko/20100101 Firefox/124.0';
use constant META_CACHE_TTL => 86400 * 30; # 24 hours x 30 = 30 days
use constant EXEC => 'yt-dlp';
use constant EXEC_OPTIONS => '--skip-download --dump-json';
my $log = logger('plugin.mixcloud');
my $prefs = preferences('plugin.mixcloud');
my $cache = Slim::Utils::Cache->new;
Slim::Player::ProtocolHandlers->registerURLHandler(PAGE_URL_REGEXP, __PACKAGE__);
sub isPlaylistURL { 0 }
sub canDirectStream {
return 0 if $prefs->get('useBuffered') && !Slim::Player::Protocols::HTTP->can('response');
return shift->SUPER::canDirectStream(@_);
}
# MixCloud streams must use Persistent mode streaming, else they fail after a few minutes
sub canEnhanceHTTP {
return 1;
}
sub scanUrl {
my ($class, $url, $args) = @_;
$args->{cb}->( $args->{song}->currentTrack() );
}
sub getFormatForURL {
my ($class, $url) = @_;
my $meta = $cache->get('mixcloud_item_' . getId($url));
return $meta ? $meta->{'format'} : $prefs->get('playformat');
}
sub new {
my $class = shift;
my $args = shift;
my $client = $args->{client};
my $song = $args->{song};
# When the stream URL is a redirect and the socket closes between chunks.
# The HTTP code is 206 - Partial Content - so not keep-alive mode.
# This only happens on some clients (e.g. SqueezePlay on Windows).
my $streamUrl = $args->{'url'} =~ /^mixcloud/ ? $song->streamUrl() : $args->{'url'};
# my $streamUrl = $song->streamUrl() || return;
my $track = $song->pluginData();
$log->info( 'Remote streaming Mixcloud track: ' . $streamUrl );
my $params = {
url => $streamUrl,
song => $song,
client => $client,
};
# this may be a bit dangerous if another track is streaming...
if (Slim::Player::Protocols::HTTP->can('canEnhanceHTTP') || !$prefs->get('useBuffered')) {
require Slim::Player::Protocols::HTTPS;
@ISA = qw(Slim::Player::Protocols::HTTPS);
} else {
require Plugins::MixCloud::Buffered;
@ISA = qw(Plugins::MixCloud::Buffered);
}
return $class->SUPER::new($params);
}
# Tweak user-agent for mixcloud to accept our request
sub requestString {
my $self = shift;
my $request = $self->SUPER::requestString(@_);
my $ua = USER_AGENT;
$request =~ s/(User-Agent:)\s*.*/\1: $ua/;
$request =~ s/Icy-MetaData:.+$CRLF//m;
return $request;
}
sub explodePlaylist {
my ( $class, $client, $uri, $callback ) = @_;
if ( $uri =~ PAGE_URL_REGEXP ) {
Plugins::MixCloud::Plugin::urlHandler(
$client,
sub { $callback->([map {$_->{'play'}} @{$_[0]->{'items'}}]) },
{'search' => $uri},
);
}
else {
$callback->([$uri]);
}
}
sub getNextTrack {
my ($class, $song, $successCb, $errorCb) = @_;
my $client = $song->master();
my $url = $song->currentTrack()->url;
_fetchTrackExtra($url, sub {
my $meta = shift;
return $successCb->() unless $meta;
$song->_streamFormat($meta->{'format'});
$song->streamUrl($meta->{'url'});
# See comments regarding bitrate and type in makeCacheItem.
# $song->bitrate($meta->{'bitrate'} * 1000);
if ($meta->{'format'} =~ /mp3|mp4|aac|m4a/i) {
my $http = Slim::Networking::Async::HTTP->new;
$http->send_request( {
request => HTTP::Request->new( GET => $meta->{'url'}, [ 'User-Agent' => USER_AGENT ] ),
onStream => $meta->{format} eq 'mp3'
? \&Slim::Utils::Scanner::Remote::parseAudioStream
: \&Slim::Utils::Scanner::Remote::parseMp4Header,
onError => sub {
my ($self, $error) = @_;
$log->error( "could not find $meta->{'url'} header with format $meta->{'format'} $error" );
$successCb->();
},
passthrough => [ $song->track,
{ cb => sub {
# See comments regarding bitrate and type in makeCacheItem.
# This line causes the actual bitrate of the stream to be cached.
$meta->{bitrate} = int($song->track->bitrate/1000) . 'kbps';
$cache->set('mixcloud_item_extra' . getId($url), $meta, META_CACHE_TTL);
$successCb->();
}},
$meta->{'url'} ],
} );
} else {
$successCb->();
}
}
);
}
sub findExec {
my $exec = EXEC;
if ($^O eq 'MSWin32') {
$exec = "$exec.exe";
}
if ($prefs->get('helper_application') eq 'custom') {
if ($prefs->get('helper_application_custom_path') eq '') {
return $exec;
}
else {
return $prefs->get('helper_application_custom_path');
}
return
}
else {
my %paths = Slim::Utils::Misc::getBinPaths();
for my $path (%paths) {
if (index($path, 'MixCloud') != -1) {
$log->debug("Use bin path $path/$exec");
return "$path/$exec";
}
}
$log->error("Error: Cannot find bin path for yt-dlp");
}
}
# complement track details (url, format, bitrate)
sub _fetchTrackExtra {
my ($url, $cb) = @_;
my $id = getId($url);
my $simpleMeta = $cache->get("mixcloud_item_$id") || {};
my $meta = $cache->get("mixcloud_item_extra_$id") || {};
$log->debug("Getting complement for $url => $id");
# we already have everything
if ($cache->{'url'} && $simpleMeta->{'updated_time'} eq $meta->{'updated_time'}) {
$log->debug("Got play URL $meta->{'url'} for $url from cache");
$cb->($meta) if $cb;
return $meta;
}
my $mixcloud_url = "https://www.mixcloud.com/$id";
# use yt-dlp to extract stream URL
my $exec = findExec();
my $exec_options = EXEC_OPTIONS;
my $yt_dlp_cmd = "$exec $exec_options $mixcloud_url 2>&1"; # pipe STDERR to STDOUT
$log->info("Executing helper binary: $yt_dlp_cmd");
my $info_json_str = `$yt_dlp_cmd`;
my $json = eval { from_json($info_json_str) };
if ($json) {
my $mixcloud_stream_url;
my $mixcloud_stream_formats = $json->{'formats'};
# we're interested in the format labelled 'http' and nothing else
foreach my $mixcloud_format (@$mixcloud_stream_formats) {
if ($mixcloud_format->{'format_id'} eq 'http'){
$mixcloud_stream_url = $mixcloud_format->{'url'};
}
}
my $format = ($mixcloud_stream_url =~ /.mp3/ ? "mp3" : "mp4");
# need to re-read from cache in case TrackDetails have been updated
$meta = $cache->get("mixcloud_item_$id") || {};
# See comments regarding bitrate and type in makeCacheItem.
# $meta->{'bitrate'} = $format eq 'mp3' ? '128k' : '64k';
$meta->{'format'} = $format;
$meta->{'type'} = "$format";
$meta->{'url'} = $mixcloud_stream_url;
$cache->set("mixcloud_item_extra_$id", $meta, META_CACHE_TTL);
$meta->{'album'} = 'Mixcloud';
$log->info("Got play URL $meta->{'url'} for $url from download");
} else {
$log->error("Failed to determine stream URL for $url");
$log->error("Tried to execure command: $yt_dlp_cmd");
$log->error("$info_json_str");
}
$cb->($meta) if $cb;
return $meta;
}
sub getMetadataFor {
my ($class, $client, $url, $args) = @_;
my $id = getId($url);
my $item = $cache->get("mixcloud_item_$id");
# this is ugly... for whatever reason the EN/Classic skins can't handle tracks with an items element
if ($args ne 'forceCurrent' && ($args->{params} && $args->{params}->{isWeb} && preferences('server')->get('skin')=~ /Classic|EN/i)) {
delete @$item{'items'};
}
return $item if $item && $item->{'play'};
if (!$client->pluginData('fetchingMeta')) {
my $fetchURL = "https://api.mixcloud.com/$id";
$client->pluginData( fetchingMeta => 1 ) if $client;
$log->info("Getting track details for $url", dump($item));
Slim::Networking::SimpleAsyncHTTP->new(
sub {
my $track = eval { from_json($_[0]->content) };
$log->warn($@) if ($@);
makeCacheItem($client, $track, $args);
$client->pluginData( fetchingMeta => 0 ) if $client;
},
sub {
$client->pluginData( fetchingMeta => 0 ) if $client;
$log->error("Error fetching track metadata for $url => $_[1]");
},
{ timeout => 30 },
)->get($fetchURL);
}
return {
bitrate => '128kbps/64kbps',
type => 'mp3/mp4 (Mixcloud)',
icon => __PACKAGE__->getIcon,
};
}
# Track Info menu
sub trackInfo {
my ( $class, $client, $track ) = @_;
my $url = $track->url;
$log->debug("trackInfo: " . $url);
return undef;
}
# Track Info menu
sub trackInfoURL {
my ( $class, $client, $url ) = @_;
$log->debug("trackInfoURL: " . $url);
return undef;
}
# 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_MIXCLOUD_STREAM_FAILED' );
}
sub getIcon {
my ( $class, $url, $noFallback ) = @_;
my $handler;
if ( ($handler = Slim::Player::ProtocolHandlers->iconHandlerForURL($url)) && ref $handler eq 'CODE' ) {
return &{$handler};
}
return $noFallback ? '' : 'html/images/radio.png';
}
sub getId {
my $url = shift;
my ($id) = $url =~ m{^(?:mixcloud)://(.*)$};
return $id;
}
sub makeCacheItem {
my ($client, $json, $args) = @_;
my $icon = __PACKAGE__->getIcon;
my ($id) = ($json->{'key'} =~ /(?:\/)*(\S*)/);
my $trackInfo = [];
my $duration;
if ($json->{'audio_length'}) {
$duration = sprintf('%s:%02s:%02s', int($json->{'audio_length'} / 3600), int($json->{'audio_length'} / 60 % 60), int($json->{'audio_length'} % 60));
}
my $year;
if ($json->{'created_time'}) {
$year = substr $json->{'created_time'}, 0, 4;
}
push @$trackInfo, {
name => cstring($client, 'TITLE') . cstring($client, 'COLON') . ' ' . $json->{'name'},
play => "mixcloud://$id",
type => 'text',
};
push @$trackInfo, {
name => cstring($client, 'LENGTH') . cstring($client, 'COLON') . ' ' . $duration,
type => 'text',
} if $duration;
push @$trackInfo, {
name => cstring($client, 'YEAR') . cstring($client, 'COLON') . ' ' . $year,
type => 'text',
} if $year;
push @$trackInfo, {
name => string('PLUGIN_MIXCLOUD_LINK') . cstring($client, 'COLON') . ' ' . $json->{'url'},
type => 'text',
} if $json->{'url'};
push @$trackInfo, {
name => string('PLUGIN_MIXCLOUD_EXCLUSIVE') . cstring($client, 'COLON') . ' ' . ($json->{'is_exclusive'} eq 1 ? string('PLUGIN_MIXCLOUD_TRUE') : string('PLUGIN_MIXCLOUD_FALSE')),
type => 'text',
};
push @$trackInfo, {
type => 'link',
name => cstring($client, 'ARTIST') . cstring($client, 'COLON') . ' ' . $json->{'user'}->{'name'},
url => \&Plugins::MixCloud::Plugin::tracksHandler,
passthrough => [ { params => substr($json->{'user'}->{'key'},1) , type => 'user', parser => \&Plugins::MixCloud::Plugin::_parseUser } ]
} if $json->{'user'}->{'key'};
push @$trackInfo, {
type => 'link',
name => string('PLUGIN_MIXCLOUD_FAVORITE') . ' ' . string('PLUGIN_MIXCLOUD_TRACK'),
url => \&Plugins::MixCloud::Plugin::favoriteTrack,
passthrough => [ { key => $json->{'key'}, type => 'text' } ]
} if ($json->{'favorited'} =~ /0/);
push @$trackInfo, {
type => 'link',
name => string('PLUGIN_MIXCLOUD_UNFAVORITE') . ' ' . string('PLUGIN_MIXCLOUD_TRACK'),
url => \&Plugins::MixCloud::Plugin::unfavoriteTrack,
passthrough => [ { key => $json->{'key'}, type => 'text' } ]
} if ($json->{'favorited'} =~ /1/);
push @$trackInfo, {
type => 'link',
name => string('PLUGIN_MIXCLOUD_REPOST') . ' ' . string('PLUGIN_MIXCLOUD_TRACK'),
url => \&Plugins::MixCloud::Plugin::repostTrack,
passthrough => [ { key => $json->{'key'}, type => 'text' } ]
} if ($json->{'reposted'} =~ /0/);
push @$trackInfo, {
type => 'link',
name => string('PLUGIN_MIXCLOUD_UNREPOST') . ' ' . string('PLUGIN_MIXCLOUD_TRACK'),
url => \&Plugins::MixCloud::Plugin::unrepostTrack,
passthrough => [ { key => $json->{'key'}, type => 'text' } ]
} if ($json->{'reposted'} =~ /1/);
if (defined $json->{'pictures'}->{'large'}) {
$icon = $json->{'pictures'}->{'large'};
} elsif (defined $json->{'pictures'}->{'medium'}) {
$icon = $json->{'pictures'}->{'medium'};
}
my $item = {
id => $id,
duration => $json->{'audio_length'},
name => $json->{'name'} . ($json->{'is_exclusive'} eq 1 ? (' (' . string('PLUGIN_MIXCLOUD_EXCLUSIVE_SHORT') . ')') : ''),
title => $json->{'name'} . ($json->{'is_exclusive'} eq 1 ? (' (' . string('PLUGIN_MIXCLOUD_EXCLUSIVE_SHORT') . ')') : ''),
artist => ($json->{'user'}->{'name'} ? $json->{'user'}->{'name'} : $json->{'user'}->{'username'}),
album => "Mixcloud",
play => "mixcloud://$id",
# There's no way to derive bitrate and type until the stream headers are read.
# If bitrate and type fields are set here then they are not updated correctly with data from the headers.
# The web UI doesn't update these fields until after the stream starts and the user interacts but cannot be fixed here.
# bitrate => '128kbps/64kbps',
# type => 'mp3/mp4',
passthrough => [ { key => $json->{'key'}} ],
updated_time => $json->{'updated_time'},
icon => $icon,
image => $icon,
cover => $icon,
on_select => 'play',
};
# Set meta cache here, so that playlist does not have to query each track
# individually although small risk to overwrite the trackDetail query
$log->debug("Caching mixcloud_item_$id", dump($item));
$cache->set("mixcloud_item_$id", $item, META_CACHE_TTL);
# this is ugly... for whatever reason the EN/Classic skins can't handle tracks with an items element
my $simpleTracks = (($args->{params} && $args->{params}->{isWeb} && preferences('server')->get('skin')=~ /Classic|EN/i) ? 1 : 0);
if (!$simpleTracks) {
$item->{'items'} = $trackInfo;
}
# Replace some fields if the call comes from Plugin.pm but do not cache.
if ($args->{params} && $args->{params}->{isPlugin}) {
# line1 and line2 are used in browse view
# artist and title are used in the now playing and playlist views
$item->{name} = $json->{'name'} . ' by ' . ($json->{'user'}->{'name'} ? $json->{'user'}->{'name'} : $json->{'user'}->{'username'}) . ($duration ? ' (' . $duration . ')': '') .
($json->{'is_exclusive'} eq 1 ? (' (' . string('PLUGIN_MIXCLOUD_EXCLUSIVE_SHORT') . ')') : ''),
$item->{title} = $json->{'name'} . ' by ' . ($json->{'user'}->{'name'} ? $json->{'user'}->{'name'} : $json->{'user'}->{'username'}) . ($duration ? ' (' . $duration . ')': '') .
($json->{'is_exclusive'} eq 1 ? (' (' . string('PLUGIN_MIXCLOUD_EXCLUSIVE_SHORT') . ')') : ''),
$item->{line1} = $json->{'name'} . ($duration ? ' (' . $duration . ')': '') .
($json->{'is_exclusive'} eq 1 ? (' (' . string('PLUGIN_MIXCLOUD_EXCLUSIVE_SHORT') . ')') : ''),
$item->{line2} = $json->{'user'}->{'name'} . ($year ? ' (' . $year . ')' : ''),
}
return $item;
}
1;