forked from grobbie/SqueezeCloud
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Plugin.pm
1066 lines (868 loc) · 32.4 KB
/
Plugin.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Plugins::SqueezeCloud::Plugin;
# Plugin to stream audio from SoundCloud streams
#
# Released under GNU General Public License version 2 (GPLv2)
#
# Written by David Blackman (first release),
# Robert Gibbon (improvements),
# Daniel Vijge (improvements),
# Robert Siebert (improvements),
# KwarkLabs (major SoundCloud API changes)
#
# See file LICENSE for full license details
use strict;
use utf8;
use Encode;
use vars qw(@ISA);
use URI::Escape;
use JSON::XS::VersionOneAndTwo;
use LWP::UserAgent;
use File::Spec::Functions qw(:ALL);
use List::Util qw(min max);
use POSIX qw(strftime);
use Slim::Utils::Strings qw(string cstring);
use Slim::Utils::Prefs;
use Slim::Utils::Log;
use Plugins::SqueezeCloud::Oauth2;
# Defines the timeout in seconds for a http request
use constant HTTP_TIMEOUT => 15;
# The default number of items to fetch in one API call
use constant API_DEFAULT_ITEMS_COUNT => 50;
# The maximum value that should be fetched via the API.
# It is not advisable to increase this value.
use constant API_MAX_ITEMS => 200;
# Which URLs should we catch when pasted into the "Tune In URL" field?
use constant PAGE_URL_REGEXP => qr{^https?://soundcloud\.com/};
use constant META_CACHE_TTL => 86400 * 30; # 24 hours x 30 = 30 days
use IO::Socket::SSL;
IO::Socket::SSL::set_defaults(
SSL_verify_mode => Net::SSLeay::VERIFY_NONE()
) if preferences('server')->get('insecureHTTPS');
my $log;
my $compat;
my $cache = Slim::Utils::Cache->new('squeezecloud');
my $prefix = 'sc:';
# This is the entry point in the script
BEGIN {
# Initialize the logging
$log = Slim::Utils::Log->addLogCategory({
'category' => 'plugin.squeezecloud',
'defaultLevel' => 'WARN',
'description' => string('PLUGIN_SQUEEZECLOUD'),
});
# Always use OneBrowser version of XMLBrowser by using server or packaged
# version included with plugin
if (exists &Slim::Control::XMLBrowser::findAction) {
$log->info("using server XMLBrowser");
require Slim::Plugin::OPMLBased;
push @ISA, 'Slim::Plugin::OPMLBased';
} else {
$log->info("using packaged XMLBrowser: Slim76Compat");
require Slim76Compat::Plugin::OPMLBased;
push @ISA, 'Slim76Compat::Plugin::OPMLBased';
$compat = 1;
}
}
# Get the data related to this plugin and preset certain variables with
# default values in case they are not set
my $prefs = preferences('plugin.squeezecloud');
$prefs->init({ refresh_token => "", playmethod => "stream" });
# This is called when squeezebox server loads the plugin.
# It is used to initialize variables and the like.
sub initPlugin {
$log->debug('initPlugin started.');
my $class = shift;
# Initialize the plugin with the given values. The 'feed' is the first
# method called. The available menu entries will be shown in the new
# menu entry 'soundcloud'.
$class->SUPER::initPlugin(
feed => \&toplevel,
tag => 'squeezecloud',
menu => 'radios',
is_app => $class->can('nonSNApps') ? 1 : undef,
weight => 10,
);
if (!$::noweb) {
require Plugins::SqueezeCloud::Settings;
Plugins::SqueezeCloud::Settings->new;
}
Slim::Formats::RemoteMetadata->registerProvider(
match => qr/soundcloud\.com/,
func => \&metadata_provider,
);
Slim::Player::ProtocolHandlers->registerHandler(
soundcloud => 'Plugins::SqueezeCloud::ProtocolHandler'
);
Slim::Player::ProtocolHandlers->registerURLHandler(
PAGE_URL_REGEXP() => 'Plugins::SqueezeCloud::ProtocolHandler'
) if Slim::Player::ProtocolHandlers->can('registerURLHandler');
$log->debug('initPlugin ended.');
}
# Called when the plugin is stopped
sub shutdownPlugin {
$log->debug('shutdownPlugin started.');
my $class = shift;
}
# Returns the name to display on the squeezebox
sub getDisplayName { 'PLUGIN_SQUEEZECLOUD' }
# Returns the default metadata for the track which is specified by the URL.
# In this case only the track title that will be returned.
sub defaultMeta {
$log->debug('defaultMeta started.');
my ( $client, $url ) = @_;
return {
title => Slim::Music::Info::getCurrentTitle($url)
};
$log->debug('defaultMeta ended.');
}
# Extracts the available metadata for a tracks from the JSON data. The data
# is cached and then returned to be presented to the user.
sub _makeMetadata {
$log->debug('_makeMetadata started.');
my ($client, $json, $args) = @_;
# this is ugly... for whatever reason the EN/Classic skins can't handle tracks with an items element
# The protocol handler cannot handle tracks with an item element either...
my $simpleTracks = ($args->{params} && (($args->{params}->{isWeb} && preferences('server')->get('skin')=~ /Classic|EN/i) || $args->{params}->{isProtocolHandler})) ? 1 : 0;
my $isFromCache = ($args->{params} && $args->{params}->{isFromCache});
# Get the icon from the artwork_url.
# Get the 500x500 high quality version, as specified in SoundCloud API.
my $icon = "";
if (defined $json->{'artwork_url'}) {
$icon = $json->{'artwork_url'};
$icon =~ s/-large/-t500x500/g;
}
my $trackinfo = [];
my $duration;
if ($json->{'duration'}) {
$duration = $json->{'duration'} / 1000;
$duration = sprintf('%s:%02s', int($duration / 60), int($duration % 60));
}
my $year;
if (int($json->{'release_year'}) > 0) {
$year = int($json->{'release_year'});
} elsif ($json->{'created_at'}) {
$year = substr $json->{'created_at'}, 0, 4;
}
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;
# It is a requirement of the SoundCloud API Terms to include this link.
push @$trackinfo, {
name => string('PLUGIN_SQUEEZECLOUD_LINK') . cstring($client, 'COLON') . ' ' . $json->{'permalink_url'},
type => 'text',
} if $json->{'permalink_url'};
push @$trackinfo, {
type => 'link',
name => cstring($client, 'ARTIST') . cstring($client, 'COLON') . ' ' . $json->{'user'}->{'username'},
url => \&tracksHandler,
passthrough => [ { uid => $json->{'user'}->{'id'}, type => 'friend', parser => \&_parseFriend } ]
} if $json->{'user'}->{'id'};
push @$trackinfo, {
type => 'link',
name => string('PLUGIN_SQUEEZECLOUD_RELATED'),
url => \&tracksHandler,
passthrough => [ { id => $json->{'id'}, type => 'releated', parser => \&_parseTracks } ]
} if $json->{'user'}->{'id'};
my $DATA;
if ($simpleTracks) {
$log->debug('_makeMetadata simpleTracks used.');
$DATA = {
id => $json->{'id'},
duration => $json->{'duration'} / 1000,
name => $json->{'title'},
title => $json->{'title'},
artist => $json->{'user'}->{'username'},
album => "SoundCloud",
play => "soundcloud://" . $json->{'id'},
#url => $json->{'permalink_url'},
#link => "soundcloud://" . $json->{'id'},
bitrate => '128kbps',
bpm => (int($json->{'bpm'}) > 0 ? int($json->{'bpm'}) : ''),
type => 'MP3 (SoundCloud)',
icon => $icon,
image => $icon,
cover => $icon,
year => ($year ? $year : ''),
on_select => 'play',
}
} else {
$DATA = {
id => $json->{'id'},
duration => $json->{'duration'} / 1000,
name => $json->{'title'},
# line1 and line2 are used in browse view
# artist and title are used in the now playing and playlist views
line1 => $json->{'user'}->{'username'} && $json->{'title'} . ' (' . $duration . ')',
line2 => $json->{'user'}->{'username'} . ( $year ? ' (' . $year . ')' : ''),
title => $json->{'title'} . ' (' . $duration . ')',
artist => $json->{'user'}->{'username'},
album => "SoundCloud",
play => "soundcloud://" . $json->{'id'},
#url => $json->{'permalink_url'},
#link => "soundcloud://" . $json->{'id'},
bitrate => '128kbps',
bpm => (int($json->{'bpm'}) > 0 ? int($json->{'bpm'}) : ''),
type => 'MP3 (SoundCloud)',
icon => $icon,
image => $icon,
cover => $icon,
year => ($year ? $year : ''),,
on_select => 'play',
items => $trackinfo,
playall => 0,
passthrough => [{
track_id => $json->{'id'}
}]
}
}
if (!$isFromCache) {
# Re-write the cache data here to reset TTL but also because it might not be complete and it might have changed.
_cacheWriteTrack($DATA);
}
$log->debug('_makeMetadata ended.');
return \%$DATA;
}
sub _cacheWriteTrack {
$log->debug('_cacheWriteTrack started.');
my ($track) = @_;
my $searchType = 'track';
$log->debug('_cacheWriteTrack ID: ' . $track->{'id'});
$cache->set($prefix . $searchType . '-' . $track->{'id'} . '-duration', $track->{'duration'} * 1000, META_CACHE_TTL);
$cache->set($prefix . $searchType . '-' . $track->{'id'} . '-name', encode_utf8($track->{'name'}), META_CACHE_TTL);
$cache->set($prefix . $searchType . '-' . $track->{'id'} . '-artist', encode_utf8($track->{'artist'}), META_CACHE_TTL);
$cache->set($prefix . $searchType . '-' . $track->{'id'} . '-artwork_url', $track->{'icon'}, META_CACHE_TTL);
$cache->set($prefix . $searchType . '-' . $track->{'id'} . '-bpm', (int($track->{'bpm'}) > 0 ? int($track->{'bpm'}) : ''), META_CACHE_TTL);
$cache->set($prefix . $searchType . '-' . $track->{'id'} . '-year', (int($track->{'year'}) > 0 ? int($track->{'year'}) : ''), META_CACHE_TTL);
$cache->set($prefix . $searchType . '-' . $track->{'id'}, $track->{'id'}, META_CACHE_TTL);
$log->debug('_cacheWriteTrack ended.');
}
sub _cacheReadTrack {
$log->debug('_cacheReadTrack started.');
my ($id) = @_;
my %track;
my $searchType = 'track';
$track{duration} = $cache->get($prefix . $searchType . '-' . $id . '-duration');
$track{name} = $cache->get($prefix . $searchType . '-' . $id . '-name');
$track{title} = decode_utf8($track{name});
$track{artist} = decode_utf8($cache->get($prefix . $searchType . '-' . $id . '-artist'));
$track{user} = {username => $track{artist}};
$track{artwork_url} = $cache->get($prefix . $searchType . '-' . $id . '-artwork_url');
$track{bpm} = $cache->get($prefix . $searchType . '-' . $id . '-bpm');
$track{year} = $cache->get($prefix . $searchType . '-' . $id . '-year');
$track{id} = $id;
$log->debug('_cacheReadTrack ID: ' . $track{'id'} . ' ' . $id);
$log->debug('_cacheReadTrack ended.');
return \%track;
}
# This method is called when the Slim::Networking::SimpleAsyncHTTP encountered
# an error or no http repsonse was received.
sub _gotMetadataError {
$log->debug('_gotMetadataError started.');
my $http = shift;
my $client = $http->params('client');
my $url = $http->params('url');
my $error = $http->error;
$log->is_debug && $log->debug( "Error fetching Web API metadata: $error" );
$client->master->pluginData( webapifetchingMeta => 0 );
# To avoid flooding the SoundCloud servers in the case of errors, we just ignore further
# metadata for this track if we get an error
my $meta = defaultMeta( $client, $url );
$meta->{_url} = $url;
$client->master->pluginData( webapimetadata => $meta );
$log->debug('_gotMetadataError ended.');
}
# This method is called when the Slim::Networking::SimpleAsyncHTTP
# method has received a http response.
sub _gotMetadata {
$log->debug('_gotMetadata started.');
my $http = shift;
my $client = $http->params('client');
my $url = $http->params('url');
my $content = $http->content;
# Check if there is an error message from the last eval() operator
if ( $@ ) {
$http->error( $@ );
_gotMetadataError( $http );
return;
}
$client->master->pluginData( webapifetchingMeta => 0 );
my $json = eval { from_json($content) };
my $user_name = $json->{'user'}->{'username'};
# _gotMetadata is only called from ProtocolHandler and cannot handle track items.
my $args = { params => {isProtocolHandler => 1}};
my $DATA = _makeMetadata($client, $json, $args );
my $ua = LWP::UserAgent->new(
requests_redirectable => [],
);
my $res = $ua->get( getStreamURL($json), Plugins::SqueezeCloud::Oauth2::getAuthenticationHeaders() );
my $stream = $res->header( 'location' );
$log->debug('_gotMetadata ended.');
return;
}
# Returns either the stream URL or the download URL from the given JSON data.
sub getStreamURL {
$log->debug('getStreamURL started.');
my $json = shift;
if ($prefs->get('playmethod') eq 'download' && exists($json->{'download_url'}) && defined($json->{'download_url'}) && $json->{'downloadable'} eq '1') {
$log->debug('download_url: ' . $json->{'download_url'});
return $json->{'download_url'};
}
else {
$log->debug('stream_url: ' . $json->{'stream_url'});
return $json->{'stream_url'};
}
}
sub fetchMetadata {
$log->debug('fetchMetadata started.');
my ( $client, $url ) = @_;
if ($url =~ /soundcloud\:\/\/(.*)/i) {
my $resource = 'tracks/' . $1;
# my $params .= "&filter=streamable";
my $params .= "";
my $extras = "linked_partitioning=true&limit=1";
my $queryUrl = "https://api.soundcloud.com/".$resource."?" . $extras . $params;
if (Plugins::SqueezeCloud::Oauth2::isAccessTokenExpired()) {
Plugins::SqueezeCloud::Oauth2::getAccessTokenWithRefreshToken(\&fetchMetadata, @_);
return;
}
# Call the server to fetch the data via the asynchronous http request.
# The methods are called when a response was received or an error
# occurred. Additional information to the http call is passed via
# the hash (third parameter).
my $http = Slim::Networking::SimpleAsyncHTTP->new(
\&_gotMetadata,
\&_gotMetadataError,
{
client => $client,
url => $url,
timeout => HTTP_TIMEOUT,
},
);
$http->get($queryUrl, Plugins::SqueezeCloud::Oauth2::getAuthenticationHeaders());
}
$log->debug('fetchMetadata ended.');
}
sub _parseTracks {
$log->debug('_parseTracks started.');
my ($client, $json) = @_;
my $menuEntries = [];
for my $entry (@{$json->{'collection'}}) {
if ($entry->{'streamable'}) {
push @$menuEntries, _makeMetadata($client, $entry);
}
}
return $menuEntries;
$log->debug('_parseTracks ended.');
}
# Main method that is called when the user selects a menu item. It is
# specified in the menu array by the key 'url'. The passthrough array
# contains the additional values that is passed to this method to
# differentiate what shall be done in here.
sub tracksHandler {
$log->debug('tracksHandler started.');
my ($client, $callback, $args, $passDict) = @_;
# Get the index (offset) where to start fetching items
my $index = ($args->{'index'} || 0); # ie, offset
my $menu = [];
# The number of items to fetch, either specified in arguments or the maximum possible.
my $pageSize = API_DEFAULT_ITEMS_COUNT;
my $quantity = ''; # placeholder for all, can be overwritten later
my $searchType = $passDict->{'type'};
my $searchStr = ($searchType eq 'tags') ? "&tags=" : "&q=";
my $search = $args->{'search'} ? $searchStr . URI::Escape::uri_escape_utf8($args->{'search'}) : '';
# The parser is the method that will be called when the
# server has returned some data in the SimpleAsyncHTTP call.
my $parser = $passDict->{'parser'} || \&_parseTracks;
my $params = $passDict->{'params'} || '';
my $uid = $passDict->{'uid'} || '';
my $pid = $passDict->{'pid'} || '';
my $extras = 'access=playable,preview&linked_partitioning=true&limit=' . $pageSize;
my $resource;
# Check the given type (defined by the passthrough array). Depending
# on the type certain URL parameters will be set.
if ($searchType eq 'playlists') {
if ($pid eq '') {
if ($uid ne '') {
$resource = "users/$uid/playlists";
}
else {
$resource = "me/playlists";
}
} else {
$resource = "playlists/$pid";
}
} elsif ($searchType eq 'playlisttracks' && $pid ne '') {
$resource = "playlists/$pid/tracks";
} elsif ($searchType eq 'playlistsearch') {
$resource = "playlists";
} elsif ($searchType eq 'liked_playlists') {
$resource = "me/likes/playlists";
} elsif ($searchType eq 'tracks') {
$resource = "users/$uid/tracks";
} elsif ($searchType eq 'releated') {
$quantity = API_MAX_ITEMS;
my $id = $passDict->{'id'} || '';
$resource = "tracks/$id/related";
} elsif ($searchType eq 'favorites') {
$resource = "users/$uid/likes/tracks";
if ($uid eq '') {
$resource = "me/likes/tracks";
}
} elsif ($searchType eq 'friends') {
$resource = "me/followings";
} elsif ($searchType eq 'users') {
# Override maximum quantity
$quantity = API_MAX_ITEMS;
$resource = "users";
} elsif ($searchType eq 'friend') {
$resource = "users/$uid";
} elsif ($searchType eq 'activities') {
# Override maximum quantity
$quantity = API_MAX_ITEMS;
$resource = "me/activities";
} else {
$resource = "tracks";
# Override maximum quantity
$quantity = API_MAX_ITEMS;
# $params .= "&filter=streamable";
# access parameter only works with a search input
if ( $args->{'search'} ) {
$params .= "&access=playable,preview";
}
}
my $queryUrl = "https://api.soundcloud.com/" . $resource . "?" . $extras . $params . $search;
_getTracks($client, $searchType, $index, $quantity, $queryUrl, $uid, $parser, $callback, $menu);
$log->debug('tracksHandler ended.');
}
sub _getTracks {
$log->debug('_getTracks started.');
my ($client, $searchType, $index, $quantity, $queryUrl, $uid, $parser, $callback, $menu) = @_;
if (Plugins::SqueezeCloud::Oauth2::isAccessTokenExpired()) {
Plugins::SqueezeCloud::Oauth2::getAccessTokenWithRefreshToken(\&_getTracks, @_);
return;
}
$log->debug("fetching: " . $queryUrl);
Slim::Networking::SimpleAsyncHTTP->new(
# Called when a response has been received for the request.
sub {
my $http = shift;
my $json = eval { from_json($http->content) };
my $next_href = $json->{'next_href'} || '';
my $returnedMenu = [];
$returnedMenu = $parser->($client, $json);
for my $entry (@$returnedMenu) {
push @$menu, $entry;
};
my $total = scalar @$menu;
# Queries that uses recursion need to be terminated, either when the end of the list is reached (for some known search type),
# or when the maximum is reached (for search types that are 'infinite' (e.g. search or feed))
my $recursiveSearchTypes = ['favorites','friend','friends','liked_playlists','playlists','playlisttracks','tracks'];
if (
($next_href eq '' && $searchType ~~ $recursiveSearchTypes) ||
($total >= $quantity && !($searchType ~~ $recursiveSearchTypes))) {
if ($searchType eq 'friends') {
# Sort by Name.
$menu = [ sort { uc($a->{name}) cmp uc($b->{name}) } @$menu ];
}
my $i = 1;
if ($searchType eq 'tracks' || $searchType eq 'playlisttracks') {
for my $entry (@$menu) {
_cacheWriteTrack($entry);
$i++;
}
# Store the total in the cache last so that this is the last TTL to expire.
# If the total is in the cache then all the data should still be cached.
$cache->set($prefix . $searchType . $uid . '-' . '-total', ($total), META_CACHE_TTL);
}
# if quantity is still '' (meaning all), the quantity is equal to the total item retrieved
if ($quantity eq '') {
$quantity = $total;
}
_callbackTracks($menu, $index, $quantity, $callback);
} else {;
_getTracks($client, $searchType, $index, $quantity, $next_href, $uid, $parser, $callback, $menu);
}
},
# Called when no response was received or an error occurred.
sub {
$log->warn("error: $_[1]");
$callback->([ { name => $_[1], type => 'text' } ]);
},
)->get($queryUrl, Plugins::SqueezeCloud::Oauth2::getAuthenticationHeaders());
$log->debug('_getTracks ended.');
}
sub _callbackTracks {
$log->debug('_callbackTracks started.');
my ( $menu, $index, $quantity, $callback ) = @_;
my $total = scalar @$menu;
# if ($quantity ne 1) {
# $quantity = min($quantity, $total - $index);
# }
# my $returnMenu = [];
# my $i = 0;
# my $count = 0;
# for my $entry (@$menu) {
# if ($i >= $index && $count < $quantity) {
# push @$returnMenu, $entry;
# $count++;
# }
# $i++;
# }
$callback->({
items => $menu,
offset => 0,
total => $total,
});
$log->debug('_callbackTracks ended.');
}
sub metadata_provider {
$log->debug('metadata_provider started.');
my ( $client, $url, $args ) = @_;
my $id = track_key($url);
my $searchType = 'track';
if ( $cache->get($prefix . $searchType . '-' . $id)) {
$log->debug('Metadata cache hit on ID: ' . $id);
my $params = $args->{params};
$params->{isFromCache} = 1;
$args->{params} = $params;
return _makeMetadata($client, _cacheReadTrack($id), $args);
} else {
$log->debug('Metadata cache miss. Fetching: ' . $id);
if ( !$client->master->pluginData('webapifetchingMeta') ) {
# The fetchMetadata method will invoke an asynchronous http request. This will
# start a timer that is linked with the method fetchMetadata. Kill any pending
# or running request that is already active for the fetchMetadata method.
Slim::Utils::Timers::killTimers( $client, \&fetchMetadata );
# Start fetching new metadata in the background
$client->master->pluginData( webapifetchingMeta => 1 );
fetchMetadata( $client, $url );
};
}
$log->debug('metadata_provider ended.');
return { };
}
# This method is called when the user has selected the last main menu where
# an URL can be entered manually. It will assemble the given URL and fetch
# the data from the server.
sub urlHandler {
$log->debug('urlHandler started.');
my ($client, $callback, $args) = @_;
my $url = $args->{'search'};
# for some reason '.'' is converted to ' ' ??? Undo this
$url =~ s/ /./;
# Remove mobile website prefix
$url =~ s/\/\/m./\/\//;
$url = URI::Escape::uri_escape_utf8($url);
my $queryUrl = "https://api.soundcloud.com/resolve?url=$url";
$log->debug("fetching: $queryUrl");
if (Plugins::SqueezeCloud::Oauth2::isAccessTokenExpired()) {
Plugins::SqueezeCloud::Oauth2::getAccessTokenWithRefreshToken(\&urlHandler, @_);
return;
}
my $fetch = sub {
Slim::Networking::SimpleAsyncHTTP->new(
sub {
my $http = shift;
my $json = eval { from_json($http->content) };
if (exists $json->{'tracks'}) {
$callback->({ items => [ _parsePlaylist($json) ] });
} else {
$callback->({
items => [ _makeMetadata($client, $json) ]
});
}
},
sub {
$log->warn("error: $_[1]");
$callback->([ { name => $_[1], type => 'text' } ]);
},
)->get($queryUrl, Plugins::SqueezeCloud::Oauth2::getAuthenticationHeaders());
};
$fetch->();
$log->debug('urlHandler ended.');
}
# Get the tracks data from the JSON array and passes it to the parseTracks
# method which will then add a menu item for each track
sub _parsePlaylistTracks {
$log->debug('_parsePlaylistTracks started.');
my ($client, $json) = @_;
my $menuEntries = [];
$log->debug('sizesize: ' . scalar @{$json->{'tracks'}});
for my $entry (@{$json->{'tracks'}}) {
if ($entry->{'streamable'}) {
push @$menuEntries, _makeMetadata($client, $entry);
}
}
return $menuEntries;
$log->debug('_parsePlaylistTracks ended.');
}
# Gets more information for the given playlist from the passed data and
# returns the menu item for it.
sub _parsePlaylist {
$log->debug('_parsePlaylist started.');
my ($client, $entry) = @_;
my $menuEntry = [];
# Add information about the track count to the playlist menu item
my $numTracks = 0;
my $titleInfo = "";
# Add year
my $year;
if (int($entry->{'release_year'}) > 0) {
$year = int($entry->{'release_year'});
} elsif ($entry->{'created_at'}) {
$year = substr $entry->{'created_at'}, 0, 4;
}
if ($year) {
$titleInfo .= $year . ', ';
}
if (exists $entry->{'tracks'} || exists $entry->{'track_count'}) {
$numTracks = exists $entry->{'track_count'} ? scalar($entry->{'track_count'}) : scalar(@{$entry->{'tracks'}});
if ($numTracks eq 1) {
$titleInfo .= "$numTracks " . lc(string('PLUGIN_SQUEEZECLOUD_TRACK'));
} else {
$titleInfo .= "$numTracks " . lc(string('PLUGIN_SQUEEZECLOUD_TRACKS'));
}
}
# Add information about the playlist play time
my $totalSeconds = Slim::Utils::DateTime::timeFormat($entry->{'duration'} / 1000);
if ($totalSeconds ne '0:00:00') {
$titleInfo .= ', ' . $totalSeconds;
}
# Get the icon from the artwork_url. If no url is defined, set the default icon.
# Shared playlists have a null artwork_url which SoundCloud might fix at some future date.
my $icon = "";
if (defined $entry->{'artwork_url'}) {
$icon = $entry->{'artwork_url'};
$icon =~ s/-large/-t500x500/g;
}
# Get the title and add the additional information to it
my $title = $entry->{'title'};
if ($titleInfo) {
$title .= " ($titleInfo)";
}
# Create the menu array
$menuEntry = {
name => $title,
type => 'playlist',
icon => $icon,
url => \&tracksHandler,
passthrough => [ { type => (exists $entry->{'tracks_uri'} ? 'playlisttracks' : 'playlists'), pid => $entry->{'id'}, parser => (exists $entry->{'tracks_uri'} ? \&_parseTracks : \&_parsePlaylistTracks) }],
};
$log->debug('_parsePlaylist ended.');
return $menuEntry;
}
# Parses the available playlists from the JSON array and gets the information
# for each playlist. Each playlist will be added as a separate menu entry.
sub _parsePlaylists {
$log->debug('_parsePlaylists started.');
my ($client, $json) = @_;
my $menuEntries = [];
for my $entry (@{$json->{'collection'}}) {
push @$menuEntries, _parsePlaylist($client, $entry);
};
return $menuEntries;
$log->debug('_parsePlaylists ended.');
}
# Shows the three available menu entries favorites, tracks and playlists
# with the received count information for a selected friend.
sub _parseFriend {
$log->debug('_parseFriend started.');
my ($client, $entry) = @_;
my $menuEntries = [];
my $image = $entry->{'avatar_url'};
my $name = $entry->{'username'} || $entry->{'full_name'};
my $favorite_count = $entry->{'public_favorites_count'};
my $track_count = $entry->{'track_count'};
my $playlist_count = $entry->{'playlist_count'};
my $id = $entry->{'id'};
if ($favorite_count > 0) {
push @$menuEntries, {
name => string('PLUGIN_SQUEEZECLOUD_FAVORITES'),
type => 'playlist',
url => \&tracksHandler,
passthrough => [ { type => 'favorites', uid => $id, max => $favorite_count }],
};
}
if ($track_count > 0) {
push @$menuEntries, {
name => string('PLUGIN_SQUEEZECLOUD_TRACKS'),
type => 'playlist',
url => \&tracksHandler,
passthrough => [ { type => 'tracks', uid => $id, max => $track_count }],
};
}
if ($playlist_count > 0) {
push @$menuEntries, {
name => string('PLUGIN_SQUEEZECLOUD_PLAYLISTS'),
type => 'link',
url => \&tracksHandler,
passthrough => [ { type => 'playlists', uid => $id, max => $playlist_count,
parser => \&_parsePlaylists } ]
};
}
$log->debug('_parseFriend ended.');
return $menuEntries;
}
# Goes through the list of available friends from the JSON data and parses the
# information for each friend (which is defined in the parseFriend method).
# Each friend is added as a separate menu entry.
sub _parseFriends {
$log->debug('_parseFriends started.');
my ($client, $json) = @_;
my $menuEntries = [];
for my $entry (@{$json->{'collection'}}) {
my $image = $entry->{'avatar_url'};
my $name = $entry->{'username'} || $entry->{'full_name'};
my $id = $entry->{'id'};
# Add the menu entry with the information for one friend.
push @$menuEntries, {
name => $name,
icon => $image,
image => $image,
type => 'link',
url => \&tracksHandler,
passthrough => [ { type => 'friend', uid => $id, parser => \&_parseFriend} ]
};
}
return $menuEntries;
$log->debug('_parseFriends ended.');
}
# Parses the given data. If the data is a playlist the number of tracks and
# some additional data will be retrieved. The playlist or if the data is a
# track will then be shown as a menu item.
sub _parseActivity {
$log->debug('_parseActivity started.');
my ($client, $entry) = @_;
my $created_at = $entry->{'created_at'};
my $origin = $entry->{'origin'};
my $type = $entry->{'type'};
# If the API returned who reposted then we could add it to the trackentry here.
# if ($type =~ /track\:repost.*/) {
# }
# The .* after playlist in the regex is needed to catch reposts.
if ($type =~ /playlist.*/) {
my $playlistItem = _parsePlaylist($client, $origin);
$log->debug('_parseActivity ended.');
return $playlistItem;
} else {
my $track = $origin->{'track'} || $origin;
my $trackentry = _makeMetadata($client, $track);
$log->debug('_parseActivity ended.');
return $trackentry;
}
}
# Parses all available items in the collection.
# Each item can either be a playlist or a track.
sub _parseActivities {
$log->debug('_parseActivities started.');
my ($client, $json) = @_;
my $menuEntries = [];
my $collection = $json->{'collection'};
for my $entry (@$collection) {
push @$menuEntries, _parseActivity($client, $entry);
}
return $menuEntries;
$log->debug('_parseActivities ended.');
}
sub track_key {
my $url = shift;
if ($url =~ /soundcloud\:\/\/(.*)/i) {
return $1;
}
return '';
}
sub playerMenu { shift->can('nonSNApps') ? undef : 'RADIO' }
# First method that is called after the plugin has been initialized.
# Creates the top level menu items that the plugin provides.
sub toplevel {
$log->debug('toplevel started.');
my ($client, $callback, $args) = @_;
# These are the available main menus. The variable type defines the menu
# type (search allows text input, link opens another menu), the url defines
# the method that shall be called when the user has selected the menu entry.
# The array passthrough holds additional parameters that is passed to the
# method defined by the url variable.
my $callbacks = [];
# Add the following menu items only when the user is logged in
if (Plugins::SqueezeCloud::Oauth2::isLoggedIn()) {
# Menu entry to show all activities (Stream)
push(@$callbacks,
{ name => string('PLUGIN_SQUEEZECLOUD_ACTIVITIES'), type => 'link',
url => \&tracksHandler, passthrough => [ { type => 'activities', parser => \&_parseActivities} ] }
);
# Menu entry to show the 'friends' the user is following
push(@$callbacks,
{ name => string('PLUGIN_SQUEEZECLOUD_FRIENDS'), type => 'link',
url => \&tracksHandler, passthrough => [ { type => 'friends', parser => \&_parseFriends} ] },
);
# Menu entry to show the 'my playlists' the user is following
push(@$callbacks,
{ name => string('PLUGIN_SQUEEZECLOUD_MY_PLAYLISTS'), type => 'link',
url => \&tracksHandler, passthrough => [ { type => 'playlists', parser => \&_parsePlaylists} ] },
);
# Menu entry to show the 'liked playlists' the user is following
push(@$callbacks,
{ name => string('PLUGIN_SQUEEZECLOUD_LIKED_PLAYLISTS'), type => 'link',
url => \&tracksHandler, passthrough => [ { type => 'liked_playlists', parser => \&_parsePlaylists} ] },
);
# Menu entry to show the users favorites
push(@$callbacks,