-
Notifications
You must be signed in to change notification settings - Fork 22
/
ProtocolHandler.pm
300 lines (231 loc) · 8.06 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
package Plugins::Spotty::ProtocolHandler;
use strict;
use base qw(Slim::Formats::RemoteStream);
use Scalar::Util qw(blessed);
use Slim::Utils::Cache;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Slim::Utils::Strings qw(cstring);
use Plugins::Spotty::Plugin;
my $cache = Slim::Utils::Cache->new();
my $log = logger('plugin.spotty');
my $prefs = preferences('plugin.spotty');
my $serverPrefs = preferences('server');
use constant IMG_TRACK => '/html/images/cover.png';
# https://open.spotify.com/album/4qpB1EXFCmq0a209JGCsZt
use constant PAGE_URL_REGEXP => qr{open.spotify.com/(.+)/([a-z0-9]+)};
Slim::Player::ProtocolHandlers->registerURLHandler(PAGE_URL_REGEXP, __PACKAGE__) if Slim::Player::ProtocolHandlers->can('registerURLHandler');
sub contentType { 'spt' }
# transcoding needs a fix only available in 7.9.1
sub canSeek { Slim::Utils::Versions->compareVersions($::VERSION, '7.9.1') >= 0 }
sub canTranscodeSeek { Slim::Utils::Versions->compareVersions($::VERSION, '7.9.1') >= 0 }
sub getSeekData {
my ($class, $client, $song, $newtime) = @_;
return { timeOffset => $newtime };
}
sub trackGain {
my ($class, $client, $url) = @_;
return unless $client && blessed $client;
# if spotty's replaygain is enabled, then don't additionally change in LMS
return if $prefs->client($client)->get('replaygain');
# otherwise respect LMS' settings
my $cprefs = $serverPrefs->client($client);
return $cprefs->get('replayGainMode') && $cprefs->get('remoteReplayGain');
}
sub formatOverride {
my ($class, $song) = @_;
# Update the transcoding table with the current player's Spotty ID...
Plugins::Spotty::Plugin->updateTranscodingTable($song->master);
# check if we want/need to purge the audio cache
# this needs to be done from whatever code being run once per track
Plugins::Spotty::AccountHelper->purgeAudioCacheAfterXTracks();
return 'spt';
}
sub canDirectStream { 0 }
# P = Chosen by the user
sub audioScrobblerSource { 'P' }
sub explodePlaylist {
my ( $class, $client, $uri, $cb ) = @_;
if ($uri =~ PAGE_URL_REGEXP) {
my $uriInfo = Plugins::Spotty::OPML::parseUri($uri);
if ($uriInfo && ref $uriInfo && $uriInfo->{uri}) {
$uri = $uriInfo->{uri};
}
}
main::INFOLOG && $log->is_info && $log->info("Explode URI: $uri");
if ($uri =~ m|/connect-\d+|) {
$cb->([$uri]);
}
elsif (my $spotty = Plugins::Spotty::Plugin->getAPIHandler($client)) {
$spotty->tracksFromURI(sub {
my $result = shift || [];
my $firstItem = $result->[0];
# OPML - return everything
if (ref $firstItem) {
$cb->({ items => Plugins::Spotty::OPML::trackList($client, $result) });
}
else {
$cb->([
map {
/((?:track|episode):.*)/;
"spotify://$1";
} @{$result}
]);
}
}, $uri);
}
else {
$cb->([]);
}
}
sub isRepeatingStream {
my ( undef, $song ) = @_;
return $song && Plugins::Spotty::Connect->isSpotifyConnect($song->master());
}
sub canDoAction {
my ( $class, $client, $url, $action ) = @_;
if ( $action eq 'pause' && $prefs->get('optimizePreBuffer') && Plugins::Spotty::Connect->isSpotifyConnect($client) ) {
return 0;
}
return 1;
}
sub getNextTrack {
my ( $class, $song, $successCb, $errorCb ) = @_;
my $client = $song->master();
if (Plugins::Spotty::Connect->isSpotifyConnect($client)) {
Plugins::Spotty::Connect->getNextTrack($song, $successCb, $errorCb);
return;
}
$successCb->();
}
sub getMetadataFor {
my ( $class, $client, $url, undef, $song ) = @_;
my $meta = {
artist => '',
album => '',
title => '',
duration => 0,
cover => IMG_TRACK,
icon => IMG_TRACK,
bitrate => 0,
originalType => 'Ogg Vorbis (Spotify)',
};
$meta->{type} = $meta->{originalType};
if ( !Plugins::Spotty::AccountHelper->hasCredentials() ) {
$meta->{artist} = cstring($client, 'PLUGIN_SPOTTY_NOT_AUTHORIZED_HINT');
$meta->{title} = cstring($client, 'PLUGIN_SPOTTY_NOT_AUTHORIZED_HINT');
return $meta;
}
elsif ( !Slim::Networking::Async::HTTP->hasSSL() ) {
$meta->{artist} = cstring($client, 'PLUGIN_SPOTTY_MISSING_SSL');
$meta->{title} = cstring($client, 'PLUGIN_SPOTTY_MISSING_SSL');
return $meta;
}
$meta = undef;
# sometimes we wouldn't get a song object, and an outdated url. Get latest data instead!
if ((!$url || $url =~ /connect-/) && !$song && Plugins::Spotty::Connect->isSpotifyConnect($client) && ($song = $client->playingSong)) {
$url = $song->streamUrl;
}
if ( $client && ($song ||= $client->currentSongForUrl($url)) ) {
# we store a copy of the metadata in the song object - no need to read from the disk cache
my $info = $song->pluginData('info');
if ( $info->{title} && $info->{duration} && ($info->{url} eq $url) ) {
my $bitrate = $song->streambitrate;
if ($bitrate) {
$info->{bitrate} = Slim::Schema::Track->buildPrettyBitRate( $bitrate );
}
# Append "...converted to [format]" if stream has been transcoded
my $converted = $song->streamformat;
if ($converted && $converted ne 'ogg') {
my $convertedString = Slim::Utils::Strings::getString(uc($converted));
if ( $converted =~ /.{2,4}/ && $converted ne $convertedString ) {
$converted = $convertedString;
}
$info->{type} = sprintf('%s (%s %s)', $info->{originalType}, cstring($client, 'CONVERTED_TO'), $converted);
}
$song->duration($info->{duration});
main::INFOLOG && $log->is_info && $log->info("Returning metadata cached in song object for $url");
main::DEBUGLOG && $log->is_debug && $log->debug(Data::Dump::dump($info));
return $info;
}
}
my $uri = $url;
$uri =~ s/\///g;
my $spotty = Plugins::Spotty::Plugin->getAPIHandler($client);
if ( my $cached = Plugins::Spotty::API->trackCached(undef, $uri, { noLookup => 1 }) ) {
$meta = {
artist => join( ', ', map { $_->{name} } @{ $cached->{artists} } ),
album => $cached->{album}->{name},
title => $cached->{name},
duration => $cached->{duration_ms} / 1000,
cover => $cached->{image} || $cached->{album}->{image},
};
main::INFOLOG && $log->is_info && $log->info("Found cached metadata for $uri");
main::DEBUGLOG && $log->is_debug && $log->debug(Data::Dump::dump($meta));
}
if (!$meta && $uri !~ /^spotify:connect-/) {
# grab missing metadata asynchronously
main::INFOLOG && $log->is_info && $log->info("No metadata found - need to look online: $uri");
$class->getBulkMetadata($client, $song ? undef : $url);
$meta = {};
}
$meta ||= {};
$meta->{bitrate} ||= $prefs->get('bitrate') . 'k VBR';
$meta->{originalType} ||= 'Ogg Vorbis (Spotify)';
$meta->{type} = $meta->{originalType};
$meta->{cover} ||= IMG_TRACK;
$meta->{icon} ||= IMG_TRACK;
if ($prefs->get('cleanupTags')) {
$meta->{title} = Plugins::Spotty::API::Cache->cleanupTags($meta->{title});
$meta->{album} = Plugins::Spotty::API::Cache->cleanupTags($meta->{album});
}
if ($song) {
if ( $meta->{duration} && !($song->duration && $song->duration > 0) ) {
$song->duration($meta->{duration});
}
$meta->{url} = $url;
$song->pluginData( info => $meta );
}
return $meta;
}
my $fetchingMeta;
sub getBulkMetadata {
my ($class, $client, $uri) = @_;
return if $fetchingMeta;
my @uris;
if ($uri) {
@uris = ($uri);
}
else {
@uris = @{ Slim::Player::Playlist::playList($client) };
}
if (scalar @uris) {
# Go fetch metadata for all tracks on the playlist without metadata
my @need;
my $spotty = Plugins::Spotty::Plugin->getAPIHandler($client) || return;
for my $track ( @uris ) {
my $uri = blessed($track) ? $track->url : $track;
$uri =~ s/\///g;
next unless $uri =~ /^spotify:(?:episode|track)/;
if ( !$spotty->trackCached(undef, $uri, { noLookup => 1 }) ) {
push @need, $uri;
}
}
if (scalar @need && !$fetchingMeta) {
$fetchingMeta = 1;
if ( main::INFOLOG && $log->is_info ) {
$log->info( "Need to fetch metadata for: " . Data::Dump::dump(@need) );
}
$spotty->tracks(sub {
# Update the playlist time so the web will refresh, etc
$client->currentPlaylistUpdateTime( Time::HiRes::time() );
Slim::Control::Request::notifyFromArray( $client, [ 'newmetadata' ] );
$fetchingMeta = 0;
}, \@need);
}
}
}
sub getIcon {
return Plugins::Spotty::Plugin->_pluginDataFor('icon');
}
1;