-
Notifications
You must be signed in to change notification settings - Fork 5
/
MetadataProvider.pm
225 lines (168 loc) · 5.66 KB
/
MetadataProvider.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
package Plugins::RadioParadise::MetadataProvider;
use strict;
use JSON::XS::VersionOneAndTwo;
use Slim::Formats::RemoteMetadata;
use Slim::Music::Info;
use Slim::Networking::SimpleAsyncHTTP;
use Slim::Utils::Cache;
use Slim::Utils::Log;
use Slim::Utils::Strings qw(cstring);
use Slim::Utils::Timers;
use Plugins::RadioParadise::Stations;
use constant ICON => Plugins::RadioParadise::Plugin->_pluginDataFor('icon');
use constant META_URL => 'http://api.radioparadise.com/api/now_playing?chan=%s';
use constant POLLRATE => 60;
my $cache = Slim::Utils::Cache->new();
my $log = logger('plugin.radioparadise');
my $channelMap = {};
sub init {
$channelMap = Plugins::RadioParadise::Stations::getChannelMap();
my $tags = join('-|', map { s/-mix//; $_ } keys %$channelMap) . '-';
my $flacUrlRegex = qr/\.radioparadise\.com\/(?:${tags})?flac/;
my $lossyUrlRegex = qr/\.radioparadise\.com\/(?:${tags}|aac-|mp3-)(?:128|192|320)/;
Slim::Formats::RemoteMetadata->registerProvider(
match => $flacUrlRegex,
func => \&provider,
);
# they seem to have a problem with artwork on the icecast strams right now - let's grab it on our end for now
Slim::Formats::RemoteMetadata->registerParser(
match => $lossyUrlRegex,
func => \&parser,
);
}
sub provider {
my ( $client, $url ) = @_;
if (!$client) {
main::DEBUGLOG && $log->is_debug && $log->debug('No client object provided');
return defaultMeta(undef, $url);
}
main::DEBUGLOG && $log->is_debug && $log->debug("Getting metadata for $url");
$client = $client->master;
if (!$client->pluginData('rpHD')) {
$cache->set( "remote_image_$url", '', 3600 );
}
if ( !$client->isPlaying && !$client->isPaused ) {
main::DEBUGLOG && $log->is_debug && $log->debug('No metadata lookup - player is not playing');
return defaultMeta( $client, $url );
}
if ( my $meta = $client->pluginData('metadata') ) {
if ( $meta->{_url} eq $url ) {
main::INFOLOG && $log->is_info && $log->info("Returning cached data for $url");
if ( !$meta->{title} ) {
$meta->{title} = Slim::Music::Info::getCurrentTitle($url);
}
return _fixHDMetadata($client, $url, $meta);
}
}
if ( !$client->pluginData('fetchingMeta') ) {
# Fetch metadata in the background
fetchMetadata( $client, $url );
}
return defaultMeta( $client, $url );
}
sub parser {
my ( $client, $url, $metadata ) = @_;
if ($metadata !~ /\bstreamUrl\b/i) {
main::DEBUGLOG && $log->is_debug && $log->debug("Metadata is missing artwork - let's look it up: ($metadata)");
provider($client, $url);
}
return 0;
}
sub fetchMetadata {
my ( $client, $url ) = @_;
Slim::Utils::Timers::killTimers( $client, \&fetchMetadata );
return unless $client;
$client = $client->master;
$client->pluginData( fetchingMeta => 1 );
# Make sure client is still playing this station
if ( Slim::Player::Playlist::url($client) ne $url ) {
$client->pluginData( fetchingMeta => 0 );
main::INFOLOG && $log->is_info && $log->info( $client->id . " no longer playing $url, stopping metadata fetch: " . Slim::Player::Playlist::url($client) );
return;
}
my ($channel) = $url =~ m{/(\w*?)?-?(?:flac|64|96|128|192|320)};
$channel ||= '';
($channel) = grep /$channel/i, keys %$channelMap;
main::INFOLOG && $log->is_info && $log->info('This seems to be the ' . ($channel || 'Main') . ' mix');
my $metaUrl = sprintf(META_URL, $channelMap->{$channel});
main::INFOLOG && $log->is_info && $log->info( "Fetching Radio Paradise metadata from $metaUrl" );
Slim::Networking::SimpleAsyncHTTP->new(
\&_gotMetadata,
\&_gotMetadataError,
{
client => $client,
url => $url,
}
)->get( $metaUrl );
}
sub _gotMetadata {
my $http = shift;
my $client = $http->params('client');
my $url = $http->params('url');
my $meta = eval { from_json($http->content) };
if ( $@ ) {
$http->error( $@ );
_gotMetadataError( $http );
return;
}
$client = $client->master if $client;
$client->pluginData( fetchingMeta => 0 );
main::INFOLOG && $log->is_info && $log->info( "Got Radio Paradise metadata: " . Data::Dump::dump($meta) );
my $ttl = defined $meta->{'time'} ? $meta->{'time'} : POLLRATE;
$meta->{_url} = $url;
$meta->{cover} ||= ICON;
if ($url =~ /\bflac\b/) {
$meta->{bitrate} = '850k VBR';
$meta->{type} = 'FLAC';
}
$cache->set( "remote_image_$url", $meta->{cover}, 3600 );
$client->pluginData( metadata => $meta );
Slim::Control::Request::notifyFromArray( $client, [ 'newmetadata' ] );
Slim::Utils::Timers::setTimer(
$client,
time() + $ttl,
\&fetchMetadata,
$url,
);
}
sub _gotMetadataError {
my $http = shift;
my $client = $http->params('client');
my $url = $http->params('url');
my $error = $http->error;
$log->warn( "Error fetching RadioParadise metadata: $error" );
$client = $client->master if $client;
$client->pluginData( fetchingMeta => 0 );
# To avoid flooding the servers in the case of errors, we just ignore further
# metadata for this station if we get an error
my $meta = defaultMeta( $client, $url );
$meta->{_url} = $url;
$client->pluginData( metadata => $meta );
Slim::Utils::Timers::setTimer(
$client,
time() + POLLRATE,
\&fetchMetadata,
$url,
);
}
sub _fixHDMetadata {
my ($client, $url, $meta) = @_;
if ( $client->pluginData('rpHD') && (my $hdImage = $cache->get( "remote_image_$url")) ) {
main::DEBUGLOG && $log->is_debug && $log->debug("Replacing artwork with HD image: $hdImage");
$meta = Storable::dclone($meta);
$meta->{cover} = $meta->{icon} = $hdImage;
}
return $meta;
}
sub defaultMeta {
my ( $client, $url ) = @_;
return _fixHDMetadata($client, $url, {
title => Slim::Music::Info::getCurrentTitle($url),
icon => ICON,
cover => ICON,
type => cstring($client, 'RADIO'),
bitrate => 850_000,
type => 'FLAC'
});
}
1;