-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathPlugin.pm
339 lines (267 loc) · 10.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
package Plugins::Spotty::Plugin;
use strict;
use base qw(Slim::Plugin::OPMLBased);
use vars qw($VERSION);
use File::Basename;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Slim::Utils::Strings qw(string);
use Plugins::Spotty::AccountHelper;
use Plugins::Spotty::API;
use Plugins::Spotty::Connect;
use Plugins::Spotty::Helper;
use Plugins::Spotty::OPML;
use Plugins::Spotty::ProtocolHandler;
use constant CAN_IMPORTER => (Slim::Utils::Versions->compareVersions($::VERSION, '8.0.0') >= 0);
my $prefs = preferences('plugin.spotty');
my $serverPrefs = preferences('server');
my $cache = Slim::Utils::Cache->new();
my $log = Slim::Utils::Log->addLogCategory( {
category => 'plugin.spotty',
defaultLevel => 'WARN',
description => 'PLUGIN_SPOTTY',
logGroups => 'SCANNER',
} );
sub initPlugin {
my $class = shift;
if ( !main::TRANSCODING ) {
$log->error('You need to enable transcoding in order for Spotty to work');
return;
}
if ( !Slim::Networking::Async::HTTP->hasSSL() ) {
$log->error(string('PLUGIN_SPOTTY_MISSING_SSL'));
}
# some debug code, dumping all locally stored files for further analysis
# my $files = Plugins::Spotty::PlaylistFolders->findAllCachedFiles();
# warn Data::Dump::dump($files);
# foreach my $candidate ( @$files ) {
# next unless $candidate =~ /51d3f5cfc935f7a059410f7b2ba206498815075a.file/;
# my $data = Plugins::Spotty::PlaylistFolders::parse($candidate);
# warn Data::Dump::dump($data, $candidate);
# }
$prefs->init({
country => 'US',
cleanupTags => 1,
bitrate => 320,
iconCode => \&_initIcon,
tracksSincePurge => 0,
ignoreHomeItems => {
'recently-updated-playlists[0]' => -1,
'recently-updated-playlists' => -1,
'recently-played' => -1,
},
accountSwitcherMenu => 0,
disableDiscovery => 0,
checkDaemonConnected => 0,
displayNames => {},
helper => '',
sortSongsAlphabetically => 1,
sortAlbumsAlphabetically => 1,
sortArtistsAlphabetically => 1,
sortPlaylisttracksByAddition => 0,
});
$prefs->setValidate({ 'validator' => sub { $_[1] =~ /^[a-f0-9]{32}$/i } }, 'iconCode');
$prefs->setChange( sub {
Slim::Music::Import->doQueueScanTasks(1);
Slim::Control::Request::executeRequest(undef, ['rescan', 'onlinelibrary']);
Slim::Music::Import->doQueueScanTasks(0);
}, 'cleanupTags');
$prefs->setChange( sub {
$cache->remove('spotty_rate_limit_exceeded');
}, 'iconCode');
# disable spt-flc transcoding on non-x86 platforms - don't transcode unless needed
# this might be premature optimization, as ARM CPUs are getting more and more powerful...
if ( !main::ISWINDOWS && !main::ISMAC
&& Slim::Utils::OSDetect::details()->{osArch} !~ /(?:i[3-6]|x)86/i
) {
$prefs->migrate(1, sub {
my $disabledFormats = $serverPrefs->get('disabledformats');
if (!grep /^spt/, @$disabledFormats) {
# ugly... but there's no API to disable formats
push @$disabledFormats, "spt-flc-*-*";
$serverPrefs->set('disabledformats', $disabledFormats);
}
return 1;
});
}
# we probably turned this on for too many users - let's start over
$prefs->migrate(2, sub {
$prefs->set('checkDaemonConnected', 0);
return 1;
});
# Spotty seems to be disabling all those hosts... use fallback by default now...
$prefs->migrate(3, sub {
$prefs->set('forceFallbackAP', 1);
return 1;
});
Plugins::Spotty::Helper->init();
$VERSION = $class->_pluginDataFor('version');
Slim::Player::ProtocolHandlers->registerHandler('spotify', 'Plugins::Spotty::ProtocolHandler');
if (main::WEBUI) {
require Plugins::Spotty::Settings;
Plugins::Spotty::Settings->new();
}
$class->SUPER::initPlugin(
feed => \&Plugins::Spotty::OPML::handleFeed,
tag => 'spotty',
menu => 'radios',
is_app => 1,
weight => 1,
);
if ( Slim::Utils::Versions->compareVersions($::VERSION, '7.9.1') < 0 ) {
$log->error('Please update to Lyrion Music Server 7.9.1 if you want to use seeking in Spotify tracks.');
}
if (CAN_IMPORTER) {
# tell LMS that we need to run the external scanner
Slim::Music::Import->addImporter('Plugins::Spotty::Importer', { use => 1 });
}
Plugins::Spotty::AccountHelper->purgeCache('init');
Plugins::Spotty::AccountHelper->purgeAudioCache(1);
Plugins::Spotty::AccountHelper->getAllCredentials();
}
sub postinitPlugin { if (main::TRANSCODING) {
my $class = shift;
Plugins::Spotty::OPML->init();
# we're going to hijack the Spotify URI schema
Slim::Player::ProtocolHandlers->registerHandler('spotify', 'Plugins::Spotty::ProtocolHandler');
Plugins::Spotty::Connect->init();
# if user has the Don't Stop The Music plugin enabled, register ourselves
if ( Slim::Utils::PluginManager->isEnabled('Slim::Plugin::DontStopTheMusic::Plugin')
&& Slim::Utils::Versions->compareVersions($::VERSION, '7.9.0') >= 0 )
{
require Plugins::Spotty::DontStopTheMusic;
Plugins::Spotty::DontStopTheMusic->init();
}
# add support for LastMix - if it's installed
if ( Slim::Utils::PluginManager->isEnabled('Plugins::LastMix::Plugin') ) {
eval {
require Plugins::LastMix::Services;
};
if (!$@) {
main::INFOLOG && $log->info("LastMix plugin is available - let's use it!");
require Plugins::Spotty::LastMix;
Plugins::LastMix::Services->registerHandler('Plugins::Spotty::LastMix');
}
}
if ( CAN_IMPORTER && Slim::Utils::PluginManager->isEnabled('Slim::Plugin::OnlineLibrary::Plugin') ) {
Slim::Plugin::OnlineLibrary::Plugin->addLibraryIconProvider('spotify', '/plugins/Spotty/html/images/icon.png');
}
if ( main::WEBUI && Plugins::Spotty::AccountHelper->getTmpDir() ) {
# LMS Settings/File Types is expecting the conversion table entry to start with "[..]".
# If we've added a TMPDIR=... prefix, we'll need to remove it for the settings to work.
my $handler = Slim::Web::Pages->getPageFunction(Slim::Web::Settings::Server::FileTypes->page);
if ( $handler && !ref $handler && $handler eq 'Slim::Web::Settings::Server::FileTypes' ) {
# override the default page handler to remove the TMPDIR prefix
Slim::Web::Pages->addPageFunction(Slim::Web::Settings::Server::FileTypes->page, sub {
my $commandTable = Slim::Player::TranscodingHelper::Conversions();
foreach ( keys %$commandTable ) {
if ( $_ =~ /^spt-/ && $commandTable->{$_} =~ /single-track/ ) {
$commandTable->{$_} =~ s/^[^\[]+//;
}
}
return $handler->handler(@_);
});
}
}
$class->updateTranscodingTable();
} }
sub onlineLibraryNeedsUpdate {
if (CAN_IMPORTER) {
my $class = shift;
require Plugins::Spotty::Importer;
return Plugins::Spotty::Importer->needsUpdate(@_);
}
else {
$log->warn('The library importer feature requires at least Lyrion Music Server 8');
}
my $cb = $_[1];
$cb->() if $cb && ref $cb && ref $cb eq 'CODE';
}
sub getLibraryStats { if (CAN_IMPORTER) {
require Plugins::Spotty::Importer;
my $totals = Plugins::Spotty::Importer->getLibraryStats();
return wantarray ? ('PLUGIN_SPOTTY_NAME', $totals) : $totals;
} }
sub updateTranscodingTable {
my $class = shift || __PACKAGE__;
my $client = shift;
# see whether we want to have a specific player's account
my $id = Plugins::Spotty::AccountHelper->getAccount($client);
# modify the transcoding helper table to inject our cache folder
my $cacheDir = Plugins::Spotty::AccountHelper->cacheFolder($id);
my $bitrate = '';
my ($helper, $helperVersion) = Plugins::Spotty::Helper->get();
if ( Slim::Utils::Versions->checkVersion($helperVersion, '0.8.0', 10) ) {
$bitrate = sprintf('--bitrate %s', $prefs->get('bitrate') || 320);
}
$helper = basename($helper) if $helper;
my $tmpDir = Plugins::Spotty::AccountHelper->getTmpDir();
if ($tmpDir) {
$tmpDir = "TMPDIR=$tmpDir";
}
# default volume normalization to whatever the user chose for his player in LMS
if ($client && !defined $prefs->client($client)->get('replaygain')) {
$prefs->client($client)->set('replaygain', $serverPrefs->client($client)->get('replayGainMode'));
}
my $canOggDirect = Plugins::Spotty::Helper->getCapability('ogg-direct');
my $canReplayGain = Plugins::Spotty::Helper->getCapability('volume-normalisation') && $client && $prefs->client($client)->get('replaygain');
my $forceFallbackAP = $prefs->get('forceFallbackAP') && !Plugins::Spotty::Helper->getCapability('no-ap-port');
my $commandTable = Slim::Player::TranscodingHelper::Conversions();
foreach ( keys %$commandTable ) {
if ( $_ =~ /^spt-/ && $commandTable->{$_} =~ /single-track/ ) {
$commandTable->{$_} =~ s/-c ".*?"/-c "$cacheDir"/g;
$commandTable->{$_} =~ s/(\[spotty.*?\])/$tmpDir $1/g if $tmpDir && $commandTable->{$_} !~ /TMPDIR=/;
$commandTable->{$_} =~ s/^[^\[]+// if !$tmpDir;
$commandTable->{$_} =~ s/--bitrate \d{2,3}/$bitrate/;
$commandTable->{$_} =~ s/\[spotty.*?\]/\[$helper\]/g if $helper;
$commandTable->{$_} =~ s/\[spotty.*?\]/\[$helper\]/g if $helper && $canOggDirect;
$commandTable->{$_} =~ s/\[spotty.*?\]/\[spotty-ogg\]/g if $commandTable->{$_} =~ /--pass.?through/ && (!$canOggDirect || $canReplayGain);
$commandTable->{$_} =~ s/enable-audio-cache/disable-audio-cache/g;
$commandTable->{$_} =~ s/ --enable-volume-normalisation //;
$commandTable->{$_} =~ s/( -n )/ --enable-volume-normalisation $1/ if $canReplayGain;
$commandTable->{$_} =~ s/( -n )/ --ap-port=12321 $1/ if $forceFallbackAP && $commandTable->{$_} !~ /--ap-port/;
$commandTable->{$_} =~ s/--ap-port=\d+ // if !$forceFallbackAP;
main::INFOLOG && $log->is_info && $log->info($commandTable->{$_});
}
}
}
sub getDisplayName { 'PLUGIN_SPOTTY_NAME' }
# don't add this plugin to the Extras menu
sub playerMenu {}
sub _pluginDataFor {
my $class = shift;
my $key = shift;
my $pluginData = Slim::Utils::PluginManager->dataForPlugin($class);
if ($pluginData && ref($pluginData) && $pluginData->{$key}) {
return $pluginData->{$key};
}
return undef;
}
sub _initIcon {
__PACKAGE__->_pluginDataFor('icon') =~ m|.*/(.*?)\.| && return $1;
}
sub hasDefaultIcon {
$prefs->get('iconCode') eq _initIcon() ? 1 : 0;
}
sub getAPIHandler {
my ($class, $client) = @_;
return unless $client;
my $api = $client->pluginData('api');
if ( !$api ) {
$api = $client->pluginData( api => Plugins::Spotty::API->new({
client => $client,
}) ) if Plugins::Spotty::AccountHelper->getAccount($client);
}
return $api;
}
sub canDiscovery { 1 }
# we only run when transcoding is enabled, but shutdown would be called no matter what
sub shutdownPlugin { if (main::TRANSCODING) {
Plugins::Spotty::AccountHelper->purgeAudioCache(1);
# make sure we don't leave our helper app running
if (main::WEBUI) {
Plugins::Spotty::Settings::Auth->shutdownHelper();
}
Plugins::Spotty::Connect->shutdown();
} }
1;