-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParserBase.pm
More file actions
214 lines (166 loc) · 5.63 KB
/
ParserBase.pm
File metadata and controls
214 lines (166 loc) · 5.63 KB
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
package Plugins::Spotify::ParserBase;
use strict;
use JSON::XS::VersionOneAndTwo;
use XML::Simple;
use Tie::Cache::LRU;
use Slim::Utils::Strings qw(string);
use Slim::Utils::Log;
my $log = logger("plugin.spotify");
use constant CACHE_TIME => 300; # cache browse results for 300 secs
sub actions {
my $class = shift;
my $opts = shift || {};
my $base = delete $opts->{'base'};
my $info = delete $opts->{'info'};
my $play = delete $opts->{'play'};
my $items = delete $opts->{'items'};
my %actions = ();
my %params = %$opts;
if ($base) {
$actions{'commonVariables'} = [ uri => 'uri', ind => 'ind', playuri => 'playuri' ];
}
if ($info) {
$actions{'info'} = { command => [ "spotifyinfocmd", 'items' ], fixedParams => { %params } };
}
if ($items) {
$actions{'items'} = { command => [ "spotifyitemcmd", 'items' ], fixedParams => { %params } };
}
if ($play) {
$actions{'play'} = { command => ['spotifyplcmd'], fixedParams => { cmd => 'load', %params }, };
$actions{'add'} = { command => ['spotifyplcmd'], fixedParams => { cmd => 'add', %params }, };
$actions{'insert'}= { command => ['spotifyplcmd'], fixedParams => { cmd => 'insert', %params } };
if ($base) {
$actions{'playall'} = $actions{'play'};
$actions{'addall'} = $actions{'add'};
}
}
return \%actions;
}
tie my %cache, 'Tie::Cache::LRU', 10;
sub get {
my ($class, $args, $session, $callback) = @_;
my $request = $class->request($args, $session);
my $params = $class->params;
my $cacheable = $class->cacheable;
my $raw = delete $params->{'raw'};
my $xml = delete $params->{'xml'};
my $json = !($raw || $xml);
my $direct = delete $params->{'direct'};
my $retry = !exists $params->{'retry'} || $params->{'retry'};
my $timeout = $params->{'timeout'} || 35;
my $url = $direct ? $request : Plugins::Spotify::Spotifyd->uri($request);
if ($cacheable && $cache{$request} && (time() - $cache{$request}->{'time'}) < CACHE_TIME) {
$log->info("query: $url using cached response");
$callback->($class->result($cache{$request}->{'data'}, $args, $session));
return;
}
$log->info("query: $url timeout: $timeout retry: $retry");
$timeout += Time::HiRes::time();
my $try;
$try = sub {
Slim::Networking::SimpleAsyncHTTP->new(
sub {
if ($json) {
my $json = eval { from_json($_[0]->content) };
if ($@) {
$log->warn("bad json: $@");
$callback->($class->error("$@"));
} elsif ($json->{'error'}) {
$log->warn("$json->{type}: $json->{error}");
if ($json->{'error'} eq 'timeout' && $retry) {
Plugins::Spotify::Spotifyd->countError;
$log->warn("retrying...");
my $time = Time::HiRes::time();
if ($retry && $time < $timeout) {
$log->debug("retrying: $url");
Slim::Utils::Timers::setTimer(undef, $time + 1, $try);
return;
}
}
if ($json->{'error'} =~ /permanent|timeout/) {
# try restarting helper if general permanent error or on final timeout
$log->warn("json response error - restarting helper");
# note this blocks the server, try to get the error message out first...
Slim::Utils::Timers::setTimer(undef, Time::HiRes::time() + 1, sub { Plugins::Spotify::Spotifyd->restartD });
}
$callback->($class->error(string('PLUGIN_SPOTIFY_SPOTIFY_ERROR') . $json->{'error'}));
} else {
$cache{$request} = { data => $json, time => time() } if $cacheable;
$callback->($class->result($json, $args, $session));
}
} elsif ($xml) {
my $xml = eval { XMLin($_[0]->content) };
if ($@) {
$log->warn("bad xml: $@");
$callback->($class->error("$@"));
} else {
$cache{$request} = { data => $xml, time => time() } if $cacheable;
$callback->($class->result($xml, $args, $session));
}
} else {
$cache{$request} = { data => $_[0]->content, time => time() } if $cacheable;
$callback->($class->result($_[0]->content, $args, $session));
}
},
sub {
my $error = $_[1];
my $time = Time::HiRes::time();
if ($retry && $time < $timeout) {
$log->debug("retrying: $url");
Slim::Utils::Timers::setTimer(undef, $time + 5, $try);
} else {
$log->warn("error $url: " . $error);
$callback->($class->error($error));
}
},
$params,
)->get($url);
};
$try->();
}
sub cacheable { 1 }
sub error {
my $class = shift;
my $error = shift;
my $error = Slim::Utils::Strings::getString($error);
return { type => 'opml', name => "Error", items => [ { type => 'text', name => $error } ] };
}
sub params { { timeout => 35 } }
sub newSession { isWeb => $_[1]->{'isWeb'}, ipeng => $_[1]->{'ipeng'}, playalbum => $_[1]->{'playalbum'} }
# backwards compatability with parser based xmlbrowser approach used in 1.x.0 versions
sub parse {
my $class = shift;
my $http = shift;
my $params = $http->params('params');
my $url = $params->{'url'};
my $client = $params->{'client'};
my $item = $params->{'item'};
my $pageicon = $params->{'pageicon'};
my $json = eval { from_json($http->content) };
if ($@) {
$log->warn("bad json: $@");
return;
}
if ($json->{'error'}) {
$log->warn("json response error: " . $json->{'error'});
return $class->error("Error: " . $json->{'error'});
}
my $session = {};
if ($pageicon) {
$session->{'isWeb'} = 1;
}
Plugins::Spotify::Plugin::addPlayAlbum($client, $session);
my $ret = $class->result($json, {}, $session);
if (ref $ret eq 'ARRAY') {
$ret = {
name => $item->{'name'},
items => $ret,
type => 'link',
};
}
# ensure Slim::Formats::XML does not try to cache parsed feed as coderefs can't be serialised
$ret->{'nocache'} = 1;
$ret->{'cachetime'} = 0;
return $ret;
}
1;