-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAPI.pm
More file actions
192 lines (151 loc) · 4.19 KB
/
Copy pathAPI.pm
File metadata and controls
192 lines (151 loc) · 4.19 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
package Plugins::Phishin::API;
use strict;
use Digest::MD5 qw(md5_hex);
use JSON::XS qw(decode_json);
use Slim::Networking::SimpleAsyncHTTP;
use Slim::Utils::Cache;
use Slim::Utils::Log;
use constant API_URL => 'http://phish.in/api/v1/';
use constant CACHE_TTL => 3600;
my $log = logger('plugin.phishin');
my $cache = Slim::Utils::Cache->new();
sub getEras {
my ($class, $cb) = @_;
_call('/eras', $cb);
}
sub getYear {
my ($class, $year, $cb) = @_;
# ??? - sorting doesn't work?
_call("/years/$year", sub {
my ($shows) = @_;
foreach (@$shows) {
$cache->set('phishin_show_' . $_->{id}, $_, CACHE_TTL)
}
$cb->($shows);
}, {
sort_attr => 'date',
sort_dir => 'desc'
});
}
sub getVenues {
my ($class, $cb) = @_;
_call('/venues', sub {
# unfortunately sorting seems broken - let's do it ourselves
my $venues = [
sort {
$a->{slug} cmp $b->{slug}
} @{shift || []}
];
$cb->($venues, @_);
},{
sort_attr => 'name'
});
}
sub getVenue {
my ($class, $id, $cb) = @_;
_call("/venues/$id", $cb);
}
sub getSongs {
my ($class, $cb) = @_;
_call('/songs', sub {
# unfortunately sorting seems broken - let's do it ourselves
my $songs = [
sort {
$a->{slug} cmp $b->{slug}
} @{shift || []}
];
$cb->($songs, @_);
},{
sort_attr => 'title'
});
}
sub getSong {
my ($class, $id, $cb) = @_;
_call("/songs/$id", $cb);
}
sub getShow {
my ($class, $id, $cb) = @_;
if ( my $cached = $cache->get('phishin_show_' . $id) ) {
main::INFOLOG && $log->is_info && $log->info("Returning cached data for show $id");
main::DEBUGLOG && $log->is_debug && $log->debug(Data::Dump::dump($cached));
$cb->($cached);
return;
}
_call("/shows/$id", $cb);
}
sub search {
my ($class, $query, $cb) = @_;
_call("/search/$query", $cb);
}
sub _call {
my ( $url, $cb, $params ) = @_;
# $uri must not have a leading slash
$url =~ s/^\///;
$url = API_URL . $url . '.json';
$params->{per_page} ||= 9999;
if ( my @keys = sort keys %{$params}) {
my @params;
foreach my $key ( @keys ) {
next if $key =~ /^_/;
push @params, $key . '=' . $params->{$key};
# push @params, $key . '=' . uri_escape_utf8( $params->{$key} );
}
$url .= '?' . join( '&', sort @params ) if scalar @params;
}
my $cached;
my $cache_key;
if (!$params->{_nocache}) {
$cache_key = md5_hex($url);
}
main::INFOLOG && $log->is_info && $cache_key && $log->info("Trying to read from cache for $url");
if ( $cache_key && ($cached = $cache->get($cache_key)) ) {
main::INFOLOG && $log->is_info && $log->info("Returning cached data for $url");
main::DEBUGLOG && $log->is_debug && $log->debug(Data::Dump::dump($cached));
$cb->($cached);
return;
}
elsif ( main::INFOLOG && $log->is_info ) {
$log->info("API call: $url");
}
my $http = Slim::Networking::SimpleAsyncHTTP->new(
sub {
my $response = shift;
my $params = $response->params('params');
my $result;
if ( $response->headers->content_type =~ /json/i ) {
$result = decode_json(
$response->content,
);
}
else {
$log->error("phish.in didn't return JSON data? " . $response->content);
}
main::DEBUGLOG && $log->is_debug && $log->debug(Data::Dump::dump($result));
if ($result && $result->{success} && $result->{data}) {
$result = $result->{data};
if ( $cache_key ) {
if ( my $cache_control = $response->headers->header('Cache-Control') ) {
my ($ttl) = $cache_control =~ /max-age=(\d+)/;
$ttl ||= CACHE_TTL; # XXX - we're going to always cache for a while, as we often do follow up calls while navigating
if ($ttl) {
main::INFOLOG && $log->is_info && $log->info("Caching result for $ttl using max-age (" . $response->url . ")");
$cache->set($cache_key, $result, $ttl);
main::INFOLOG && $log->is_info && $log->info("Data cached (" . $response->url . ")");
}
}
}
}
$cb->($result, $response);
},
sub {
my ($http, $error, $response) = @_;
$log->error("Got error from phish.in': $error");
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump($response));
$cb->({
error => 'Unexpected error: ' . $error,
}, $response);
},
);
$http->get($url, 'Authorization' => 'Bearer ' . Plugins::Phishin::Plugin->_pluginDataFor('id2'));
}
1;