-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFavorites.pm
126 lines (101 loc) · 3.23 KB
/
Favorites.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
package Plugins::RadioParadise::Favorites;
use strict;
use JSON::XS::VersionOneAndTwo;
use List::Util qw(min);
use URI::Escape qw(uri_escape_utf8);
use Slim::Networking::SimpleAsyncHTTP;
use Slim::Utils::Log;
use constant AUTH_URL => 'https://api.radioparadise.com/api/auth?username=%s&passwd=%s';
use constant RATING_URL => 'http://api.radioparadise.com/api/rating?song_id=%s&rating=%s';
my $log = logger('plugin.radioparadise');
sub init {
my ($class) = @_;
$class->refresh();
}
sub signIn {
my ($class, $username, $password, $cb) = @_;
if (!$username || !$password) {
$cb->();
return;
}
Slim::Networking::SimpleAsyncHTTP->new(sub {
if (!isSignedIn()) {
$log->warn("Failed to sign in? " . (main::INFOLOG && Data::Dump::dump(@_)));
}
elsif (main::DEBUGLOG && $log->is_debug) {
$log->debug(Data::Dump::dump(@_));
}
$cb->(isSignedIn());
}, sub {
$log->warn("Failed to sign in? " . (main::INFOLOG && Data::Dump::dump(@_)));
$cb->(isSignedIn());
})->get(sprintf(AUTH_URL, uri_escape_utf8($username), uri_escape_utf8($password)));
}
sub signOut {
Slim::Networking::Async::HTTP->cookie_jar->clear('.radioparadise.com');
Slim::Networking::Async::HTTP->cookie_jar->clear('api.radioparadise.com');
}
sub refresh {
my ($class) = @_;
if ($class->isSignedIn()) {
# call auth endpoint to refresh the token if possible
Slim::Networking::SimpleAsyncHTTP->new(sub {}, sub {})->get(AUTH_URL);
}
}
sub rate {
my ($class, $songId, $rating, $cb) = @_;
if (!$songId || !defined $rating || $rating < 0 || $rating > 10) {
$log->warn(sprintf('Invalid rating (%s) or song ID (%s)', defined $rating ? $rating : 'null', $songId || 0));
$cb->() if $cb;
return;
}
main::INFOLOG && $log->is_info && $log->info(sprintf('Rating song %s: %s', $songId, $rating));
Slim::Networking::SimpleAsyncHTTP->new(
sub {
my $http = shift;
my $result = eval { from_json($http->content) };
$@ && $log->error($@);
if (!$result || !$result->{status} || $result->{status} ne 'success') {
$log->error('Failed to submit rating: ' . $http->content);
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump($http));
$cb->() if $cb;
}
elsif ($cb) {
$cb->(1);
}
},
sub {
$log->error('Failed to submit rating');
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump(@_));
$cb->() if $cb;
}
)->get(sprintf(RATING_URL, $songId, $rating));
}
sub isSignedIn {
my %required = (
c_validated => 1,
c_user_id => 1,
c_passwd => 1,
);
my $expiresTS = time() + 720 * 86400;
Slim::Networking::Async::HTTP->cookie_jar->scan(sub {
my (undef, $name, undef, undef, $domain, undef, undef, undef, $expires) = @_;
if ($domain =~ /\bradioparadise\.com/) {
$expiresTS = min($expiresTS, $expires) if $required{lc($name)};
delete $required{lc($name)} if $expires > time();
}
});
main::INFOLOG && $log->is_info && !keys %required && $log->info(sprintf('Session still valid for %s seconds', $expiresTS - time()));
return keys %required ? 0 : $expiresTS;
}
sub getUserId {
my $userId = '';
Slim::Networking::Async::HTTP->cookie_jar->scan(sub {
my (undef, $name, $value, undef, $domain) = @_;
if ($domain =~ /\bradioparadise\.com/ && lc($name) eq 'c_user_id') {
$userId = $value;
}
});
return $userId;
}
1;