-
Notifications
You must be signed in to change notification settings - Fork 4
/
Stations.pm
200 lines (169 loc) · 4.98 KB
/
Stations.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
package Plugins::RadioParadise::Stations;
use strict;
use Async::Util;
use Slim::Networking::SimpleAsyncHTTP;
use Slim::Utils::Log;
use Slim::Utils::Timers;
use Plugins::RadioParadise::API;
use Plugins::RadioParadise::MetadataProvider;
use constant REFRESH_INTERVAL => 86400;
use constant CHANNEL_LIST_URL => 'http://api.radioparadise.com/api/list_chan';
use constant STATION_URL_LIST_URL => 'http://img.radioparadise.com/content/prod/listen/streams/template.html';
my $log = logger('plugin.radioparadise');
my $stations = { map {
$_->{tag} => $_;
} (
{
tag => 'main-mix',
name => 'PLUGIN_RADIO_PARADISE_MAIN_MIX',
flac_interactive => 'radioparadise://4.flac',
flac => 'http://stream.radioparadise.com/flac',
aac_320 => 'http://stream.radioparadise.com/aac-320',
aac_128 => 'http://stream.radioparadise.com/aac-128',
mp3 => 'http://stream.radioparadise.com/mp3-192'
},
{
tag => 'mellow-mix',
id => 1,
name => 'PLUGIN_RADIO_PARADISE_MELLOW_MIX',
flac_interactive => 'radioparadise://4-1.flac',
flac => 'http://stream.radioparadise.com/mellow-flac',
aac_320 => 'http://stream.radioparadise.com/mellow-320',
aac_128 => 'http://stream.radioparadise.com/mellow-128',
mp3 => 'http://stream.radioparadise.com/mellow-192'
},
{
tag => 'rock-mix',
id => 2,
name => 'PLUGIN_RADIO_PARADISE_ROCK_MIX',
flac_interactive => 'radioparadise://4-2.flac',
flac => 'http://stream.radioparadise.com/rock-flac',
aac_320 => 'http://stream.radioparadise.com/rock-320',
aac_128 => 'http://stream.radioparadise.com/rock-128',
mp3 => 'http://stream.radioparadise.com/rock-192'
},
{
tag => 'global-mix',
id => 3,
name => 'PLUGIN_RADIO_PARADISE_ECLECTIC_MIX',
flac_interactive => 'radioparadise://4-3.flac',
flac => 'http://stream.radioparadise.com/eclectic-flac',
aac_320 => 'http://stream.radioparadise.com/eclectic-320',
aac_128 => 'http://stream.radioparadise.com/eclectic-128',
mp3 => 'http://stream.radioparadise.com/eclectic-192'
},
) };
sub init {
Slim::Utils::Timers::killTimers(undef, \&init);
Async::Util::achain(
steps => [
sub {
my (undef, $acb) = @_;
Plugins::RadioParadise::API->auth($acb);
},
sub {
my ($userId, $acb) = @_;
Plugins::RadioParadise::API->getRadioStationList($acb);
},
sub {
my ($stationInfo, $acb) = @_;
_gotChannelList($stationInfo, $acb);
},
sub {
my ($stationInfo, $acb) = @_;
return _getStationURLList($acb) if $stationInfo;
$acb->();
}
],
cb => sub {
main::INFOLOG && $log->is_info && $log->info("Complete RP Station Information: " . Data::Dump::dump($stations));
}
);
Slim::Utils::Timers::setTimer(undef, time + REFRESH_INTERVAL, \&init);
}
sub getChannelList {
return [ sort {
$a->{id} <=> $b->{id}
} values %$stations ];
}
sub getChannelMap {
return { map {
$_->{tag} => $_->{id};
} grep {
$_->{id};
} values %$stations };
}
sub _gotChannelList {
my ($stationInfo, $cb) = @_;
if ($stationInfo && ref $stationInfo && ref $stationInfo eq 'ARRAY' && grep { !$stations->{$_->{stream_name}} } @$stationInfo) {
main::INFOLOG && $log->is_info && $log->info("Found new station information: " . Data::Dump::dump(grep { !$stations->{$_->{stream_name}} } @$stationInfo));
foreach (@$stationInfo) {
next unless ref $_ and ref $_ eq 'HASH';
if ($_->{chan} && $_->{stream_name} && $_->{title}) {
my $station = $stations->{$_->{stream_name}} ||= {
tag => $_->{stream_name}
};
$station->{name} ||= $_->{title};
$station->{id} = $_->{chan};
$station->{flac_interactive} = sprintf('radioparadise://4-%s.flac', $_->{chan});
}
}
return $cb->($stationInfo);
}
$cb->();
}
sub _getStationURLList {
my ($cb) = @_;
Slim::Networking::SimpleAsyncHTTP->new(
sub {
my ($http) = @_;
_gotStationURLList($http);
$cb->();
},
sub {
my ($http, $error) = @_;
$log->error("Failed to look up new URL list: $error" );
$cb->();
}
)->get(STATION_URL_LIST_URL);
}
sub _gotStationURLList {
my $http = shift;
my $content = $http->content;
my @snippets;
while ($content =~ m|class="topic_indent">(?<snippet>.*?)</div|sig) {
push @snippets, $+{snippet}
}
my $aac320regex = qr/\/((?!mp3|aac)\w+?)-320/;
my $aac128regex = qr/\/((?!mp3|aac)\w+?)-128/;
my $mp3192regex = qr/\/((?!mp3|aac)\w+?)-192/;
my $flacRegex = qr/\/((?!mp3|aac)\w+?)-flac/;
my %urls;
foreach (@snippets) {
while (m|href="(?<url>.+?)".+?>(?<name>.+?)</a|sig) {
$urls{$+{url}} = $+{name};
}
}
while (my ($url, $name) = each(%urls)) {
if ($url =~ $aac128regex) {
_createStation($url, $1, $name, 'aac_128');
}
elsif ($url =~ $aac320regex) {
_createStation($url, $1, $name, 'aac_320');
}
elsif ($url =~ $mp3192regex) {
_createStation($url, $1, $name, 'mp3');
}
elsif ($url =~ $flacRegex) {
_createStation($url, $1, $name, 'flac');
}
}
Plugins::RadioParadise::MetadataProvider->init();
}
sub _createStation {
my ($url, $tag, $name, $quality) = @_;
my $station = $stations->{"$tag-mix"} ||= {};
$station->{name} ||= $name;
$station->{$quality} = $url;
};
1;