forked from michaelherger/lms-plugin-tidal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.pm
192 lines (154 loc) · 5.21 KB
/
API.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
package Plugins::TIDAL::API;
use strict;
use Exporter::Lite;
use Slim::Utils::Cache;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
our @EXPORT_OK = qw(AURL BURL KURL SCOPES GRANT_TYPE_DEVICE DEFAULT_LIMIT MAX_LIMIT PLAYLIST_LIMIT DEFAULT_TTL DYNAMIC_TTL USER_CONTENT_TTL);
use constant AURL => 'https://auth.tidal.com';
use constant BURL => 'https://api.tidal.com/v1';
use constant KURL => 'https://gist.githubusercontent.com/yaronzz/48d01f5a24b4b7b37f19443977c22cd6/raw/5a91ced856f06fe226c1c72996685463393a9d00/tidal-api-key.json';
use constant IURL => 'http://resources.tidal.com/images/';
use constant SCOPES => 'r_usr+w_usr';
use constant GRANT_TYPE_DEVICE => 'urn:ietf:params:oauth:grant-type:device_code';
use constant DEFAULT_LIMIT => 100;
use constant PLAYLIST_LIMIT => 50;
use constant MAX_LIMIT => 5000;
use constant DEFAULT_TTL => 86400;
use constant DYNAMIC_TTL => 3600;
use constant USER_CONTENT_TTL => 300;
use constant IMAGE_SIZES => {
album => '1280x1280',
track => '1280x1280',
artist => '750x750',
user => '600x600',
mood => '684x684',
genre => '640x426',
playlist => '1080x720',
playlistSquare => '1080x1080',
};
use constant SOUND_QUALITY => {
LOW => 'mp4',
HIGH => 'mp4',
LOSSLESS => 'flc',
HI_RES => 'flc',
};
my $cache = Slim::Utils::Cache->new;
my $log = logger('plugin.tidal');
my $prefs = preferences('plugin.tidal');
sub getSomeUserId {
my $accounts = $prefs->get('accounts');
my ($account) = keys %$accounts;
return $account;
}
sub getUserdata {
my ($class, $userId) = @_;
return unless $userId;
my $accounts = $prefs->get('accounts') || return;
return $accounts->{$userId};
}
sub getCountryCode {
my ($class, $userId) = @_;
my $userdata = $class->getUserdata($userId) || {};
return uc($prefs->get('countryCode') || $userdata->{countryCode} || 'US');
}
sub getFormat {
return SOUND_QUALITY->{$prefs->get('quality')};
}
sub getImageUrl {
my ($class, $data, $usePlaceholder, $type) = @_;
if ( my $coverId = $data->{cover} || $data->{image} || $data->{squareImage} || $data->{picture} || ($data->{album} && $data->{album}->{cover}) ) {
return $data->{cover} = $coverId if $coverId =~ /^https?:/;
$type ||= $class->typeOfItem($data);
my $iconSize;
if ($type eq 'playlist' && $data->{squareImage}) {
$coverId = $data->{squareImage};
$iconSize ||= IMAGE_SIZES->{playlistSquare};
}
$iconSize ||= IMAGE_SIZES->{$type};
if ($iconSize) {
$coverId =~ s/-/\//g;
$data->{cover} = IURL . $coverId . "/$iconSize.jpg";
}
else {
delete $data->{cover};
}
}
elsif (my $images = $data->{mixImages}) {
my $image = $images->{L} || $images->{M} || $images->{S};
$data->{cover} = $image->{url} if $image;
}
elsif (my $images = $data->{images}) {
my $image = $images->{MEDIUM} || $images->{SMALL} || $images->{LARGE};
$data->{cover} = $image->{url} if $image;
}
return $data->{cover} || (!main::SCANNER && $usePlaceholder && Plugins::TIDAL::Plugin->_pluginDataFor('icon'));
}
sub typeOfItem {
my ($class, $item) = @_;
if ( $item->{type} && $item->{type} =~ /(?:EXTURL|VIDEO)/ ) {}
elsif ( defined $item->{hasPlaylists} && $item->{path} ) {
return 'category';
}
elsif ( ($item->{type} && $item->{type} =~ /(?:ALBUM|EP|SINGLE)/) || ($item->{releaseDate} && defined $item->{numberOfTracks}) ) {
return 'album';
}
# playlist items can be of various types: USER, EDITORIAL etc., but they should have a numberOfTracks element
elsif ( $item->{type} && defined $item->{numberOfTracks} && ($item->{created} || $item->{creator} || $item->{creators} || $item->{publicPlaylist} || $item->{lastUpdated}) ) {
return 'playlist';
}
elsif ( (defined $item->{mixNumber} && $item->{artists}) || defined $item->{mixType} ) {
return 'mix'
}
# only artists have names? Others have titles?
elsif ( $item->{name} ) {
return 'artist';
}
# tracks?
elsif ( !$item->{type} || defined $item->{duration}) {
return 'track';
}
elsif ( main::INFOLOG ) {
$log->warn('unknown tidal item type: ' . Data::Dump::dump($item));
Slim::Utils::Log::logBacktrace('');
}
return '';
}
sub cacheTrackMetadata {
my ($class, $tracks) = @_;
return [] unless $tracks;
return [ map {
my $entry = $_;
$entry = $entry->{item} if $entry->{item};
my $oldMeta = $cache->get( 'tidal_meta_' . $entry->{id}) || {};
my $icon = $class->getImageUrl($entry, 'usePlaceholder', 'track');
my $artist = $entry->{artist};
($artist) = grep { $_->{type} eq 'MAIN'} @{$entry->{artists}} unless $artist;
# consolidate metadata in case parsing of stream came first (huh?)
my $meta = {
%$oldMeta,
id => $entry->{id},
title => $entry->{title},
artist => $artist,
artists => $entry->{artists},
album => $entry->{album}->{title},
album_id => $entry->{album}->{id},
duration => $entry->{duration},
icon => $icon,
cover => $icon,
replay_gain => $entry->{replayGain} || 0,
peak => $entry->{peak},
disc => $entry->{volumeNumber},
tracknum => $entry->{trackNumber},
url => $entry->{url},
};
# cache track metadata aggressively
$cache->set( 'tidal_meta_' . $entry->{id}, $meta, time() + 90 * 86400);
$meta;
} @$tracks ];
}
sub getHumanReadableName {
my ($class, $profile) = @_;
return $profile->{nickname} || $profile->{firstName} || $profile->{fullName} || $profile->{username};
}
1;