-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.pm
247 lines (204 loc) · 7.29 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
package Plugins::SomaFM::Plugin;
# Plugin to stream audio from SomaFM channels
#
# Released under the MIT Licence
# Written by Daniel Vijge
# See file LICENSE for full licence details
use strict;
use utf8;
use vars qw(@ISA);
use base qw(Slim::Plugin::OPMLBased);
use feature qw(fc);
use JSON::XS::VersionOneAndTwo;
use Slim::Utils::Strings qw(string);
use Slim::Utils::Prefs;
use Slim::Utils::Log;
use constant HTTP_TIMEOUT => 15;
use constant HTTP_CACHE => 1;
use constant HTTP_EXPIRES => '1h';
use constant CHANNEL_API => 'http://api.somafm.com/channels.json';
my $log;
# Get the data related to this plugin and preset certain variables with
# default values in case they are not set
my $prefs = preferences('plugin.somafm');
$prefs->init({ menuLocation => 'radio', orderBy => 'popular', groupByGenre => 0, streamingQuality => 'highest:aac', descriptionInTitle => 0, secondLineText => 'description' });
# This is the entry point in the script
BEGIN {
# Initialize the logging
$log = Slim::Utils::Log->addLogCategory({
'category' => 'plugin.somafm',
'defaultLevel' => 'ERROR',
'description' => string('PLUGIN_SOMAFM'),
});
}
# This is called when squeezebox server loads the plugin.
# It is used to initialize variables and the like.
sub initPlugin {
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 'somafm'.
$class->SUPER::initPlugin(
feed => \&_feedHandler,
tag => 'somafm',
menu => 'radios',
is_app => $class->can('nonSNApps') && ($prefs->get('menuLocation') eq 'apps') ? 1 : undef,
weight => 10,
);
if (!$::noweb) {
require Plugins::SomaFM::Settings;
Plugins::SomaFM::Settings->new;
}
}
# Called when the plugin is stopped
sub shutdownPlugin {
my $class = shift;
}
# Returns the name to display on the squeezebox
sub getDisplayName { 'PLUGIN_SOMAFM' }
sub playerMenu { undef }
sub _feedHandler {
my ($client, $callback, $args, $passDict) = @_;
my $queryUrl = CHANNEL_API;
my $menu = [];
my $fetch;
$fetch = sub {
$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) };
if ($prefs->get('groupByGenre')) {
_parseChannelsWithGroupByGenre($json->{'channels'}, $menu);
}
else {
_parseChannels(_sortChannels($json->{'channels'}), $menu);
}
$callback->({
items => $menu
});
},
# Called when no response was received or an error occurred.
sub {
$log->error("error: $_[1]");
$callback->([ { name => $_[1], type => 'text' } ]);
},
{
timeout => HTTP_TIMEOUT,
cache => HTTP_CACHE,
expires => HTTP_EXPIRES,
}
)->get($queryUrl);
};
$fetch->();
}
sub _parseChannels {
my ($channels, $menu) = @_;
for my $channel (@$channels) {
push @$menu, _parseChannel($channel);
}
}
sub _parseChannelsWithGroupByGenre {
my ($channels, $menu) = @_;
my %menu_items;
# Create submenus for each genre.
# First check if the genre menu doesn't exist yet. If if doesn't,
# create the menu item and let `items` reference to a (yet) empty
# array. Then for each genre, parse the channel and add it to the
# array. As this works by reference it can all be done in one loop.
for my $channel (@$channels) {
for my $genre (split('\|', $channel->{'genre'})) {
if (!exists($menu_items{$genre})) {
$menu_items{ $genre } = ();
push @$menu, {
name => ucfirst($genre),
items => \@{$menu_items{$genre}}
};
}
push @{ $menu_items{ $genre } }, _parseChannel($channel);
}
}
# Sort items within the submenus
foreach ( @$menu ) {
$_->{'items'} = _sortChannels($_->{'items'});
}
# Sort the genres themselves alphabetically
@$menu = sort { $a->{name} cmp $b->{name} } @$menu;
}
sub _parseChannel {
my ($channel) = @_;
return {
name => _getFirstLineText($channel, 0),
description => $channel->{'description'},
listeners => $channel->{'listeners'},
current_track => $channel->{'lastPlaying'},
genre => (join ', ', map ucfirst, split '\|', $channel->{'genre'}), # split genre and capitalise the first letter, so 'ambient|electronic' becomes 'Ambient, Electronic'
line1 => _getFirstLineText($channel, 1),
line2 => _getSecondLineText($channel),
type => 'audio',
url => _getStream($channel),
image => $channel->{'largeimage'}
};
}
sub _getStream {
my ($channel) = shift;
my ($quality, $format) = split(':', $prefs->get('streamingQuality'));
my $playlists = $channel->{'playlists'};
for my $stream (@$playlists) {
if ($stream->{'quality'} eq $quality && $stream->{'format'} eq $format) {
$log->debug("Using stream url $stream->{'url'}");
return $stream->{'url'};
}
}
$log->warn("Could not find preferred streaming quality. Returning first result as fallback: $playlists->[0]->{'quality'}:$playlists->[0]->{'format'}");
return $playlists->[0]->{'url'};
}
sub _sortChannels {
my ($channels) = shift;
my @sorted_channels;
my $orderBy = $prefs->get('orderBy');
if ($orderBy eq 'popular') {
# sort by number of listeners descending
@sorted_channels = sort { $b->{listeners} <=> $a->{listeners} } @$channels;
}
elsif ($orderBy eq 'title') {
# sort alphabetically but case-insensitive
@sorted_channels = sort { fc($a->{title}) cmp fc($b->{title}) } @$channels;
}
else {
# do not sort, use order as provided in channel feed
@sorted_channels = @$channels;
}
return \@sorted_channels;
}
sub _getFirstLineText {
my ($channel, $isFirstLine) = @_;
# Display the channel description in the title. If a skin/app supports line1/line2
# the description is added to the title if the description is not already shown on
# line2.
if ($prefs->get('descriptionInTitle') && (
($prefs->get('secondLineText') eq 'description' && !$isFirstLine) ||
($prefs->get('secondLineText') ne 'description' && $isFirstLine)
)) {
return "$channel->{'title'}: $channel->{'description'}";
}
else {
return $channel->{'title'};
}
}
sub _getSecondLineText {
my ($channel) = shift;
my $secondLineText = $prefs->get('secondLineText');
if ($secondLineText eq 'lastPlayed') {
return $channel->{'lastPlaying'};
}
elsif ($secondLineText eq 'listeners') {
return sprintf(string('PLUGIN_SOMAFM_SECOND_LINE_TEXT_LISTENERS_SPRINTF', $channel->{'listeners'}));
}
else {
return $channel->{'description'};
}
}
# Always end with a 1 to make Perl happy
1;