forked from yciabaud/triode-spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecent.pm
More file actions
222 lines (161 loc) · 5.61 KB
/
Recent.pm
File metadata and controls
222 lines (161 loc) · 5.61 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
215
216
217
218
219
220
221
222
package Plugins::Spotify::Recent;
use strict;
tie my %recentArtists, 'Tie::Cache::LRU', 50;
tie my %recentAlbums, 'Tie::Cache::LRU', 50;
tie my %recentSearches, 'Tie::Cache::LRU', 20;
tie my %recentPlaylists, 'Tie::Cache::LRU', 20;
use Slim::Utils::Strings qw(string);
use Slim::Utils::Prefs;
use Slim::Utils::Log;
use constant MENU => \&Plugins::Spotify::Plugin::level;
my $prefs = preferences('plugin.spotify');
my $log = logger('plugin.spotify');
$prefs->init({ recentartists => [], recentalbums => [], recentsearches => [], recentplaylists => [] });
# keep a cache of responses so we maintain the order as we decend into browse trees including recent entries
my %browseCache;
sub level {
my ($client, $callback, $args, $type) = @_;
my @menu;
my $hash;
if (defined $args->{'quantity'} && $args->{'quantity'} == 1 && $type ne 'searches') {
$log->info("returning cached $type index $args->{index}");
$callback->($browseCache{ $type });
return;
}
$log->info("recent: $type");
if ($type eq 'artists') {
$hash = \%recentArtists;
}
if ($type eq 'albums') {
$hash = \%recentAlbums;
}
if ($type eq 'searches') {
$hash = \%recentSearches;
}
# do in reverse to maintain LRU order
for my $uri (reverse keys %{$hash}) {
my $entry = {
'name' => $hash->{$uri}->{'name'},
'url' => MENU,
};
if ($type eq 'artists') {
$entry->{'passthrough'} = [ 'ArtistBrowse', { artist => $uri } ];
$entry->{'itemActions'} = Plugins::Spotify::ParserBase->actions({ info => 1, play => 1, uri => $uri });
}
if ($type eq 'albums') {
if ($hash->{$uri}->{'artist'} && $hash->{$uri}->{'album'}) {
$entry->{'name'} = $hash->{$uri}->{'album'} . " " . string('BY') . " " . $hash->{$uri}->{'artist'};
$entry->{'line1'} = $hash->{$uri}->{'album'};
$entry->{'line2'} = $hash->{$uri}->{'artist'}
};
$entry->{'passthrough'} = [ 'AlbumBrowse', { album => $uri } ];
$entry->{'play'} = $uri;
$entry->{'hasMetadata'} = 'album';
$entry->{'itemActions'} = Plugins::Spotify::ParserBase->actions({ info => 1, play => 1, uri => $uri });
}
if ($type eq 'searches') {
$entry->{'passthrough'} = [ 'Search', { query => $uri, search => 1, recent => 1 } ];
}
$entry->{'image'} = Plugins::Spotify::Image->uri($hash->{$uri}->{'cover'}) if exists $hash->{$uri}->{'cover'};
unshift @menu, $entry;
}
$browseCache{ $type } = \@menu;
$callback->(\@menu);
}
sub load {
my $class = shift;
# read in reverse to maintain LRU order
for my $artist (reverse @{$prefs->get('recentartists')}) {
$recentArtists{ $artist->{'uri'} } = { name => $artist->{'name'} };
}
for my $album (reverse @{$prefs->get('recentalbums')}) {
$recentAlbums{ $album->{'uri'} } = { artist => $album->{'artist'}, album => $album->{'album'}, cover => $album->{'cover'} };
$recentAlbums{ $album->{'uri'} }->{'name'} = $album->{'name'} if $album->{'name'};
}
for my $search (reverse @{$prefs->get('recentsearches')}) {
$recentSearches{ $search } = { name => $search };
}
for my $playlist (reverse @{$prefs->get('recentplaylists')}) {
$recentPlaylists{ $playlist->{'uri'} } = { name => $playlist->{'name'} };
}
}
sub save {
my $class = shift;
my $savenow = shift;
if (!$savenow) {
Slim::Utils::Timers::killTimers($class, \&save);
Slim::Utils::Timers::setTimer($class, Time::HiRes::time() + 10, \&save, 'now');
return;
}
my @artists;
my @albums;
my @searches;
my @playlists;
# read in reverse to maintain LRU order
for my $uri (reverse keys %recentArtists) {
unshift @artists, { name => $recentArtists{ $uri }->{'name'}, uri => $uri };
}
for my $uri (reverse keys %recentAlbums) {
my $entry = { uri => $uri, cover => $recentAlbums{ $uri }->{'cover'},
artist => $recentAlbums{ $uri }->{'artist'}, album => $recentAlbums{ $uri }->{'album'} };
$entry->{'name'} = $recentAlbums{ $uri }->{'name'} if $recentAlbums{ $uri }->{'name'};
unshift @albums, $entry;
}
for my $name (reverse keys %recentSearches) {
unshift @searches, $name;
}
for my $uri (reverse keys %recentPlaylists) {
unshift @playlists, { name => $recentPlaylists{ $uri }->{'name'}, uri => $uri };
}
$log->info("updating recent prefs");
$prefs->set('recentartists', \@artists);
$prefs->set('recentalbums', \@albums);
$prefs->set('recentsearches', \@searches);
$prefs->set('recentplaylists', \@playlists);
}
sub updateRecentArtists {
my ($class, $name, $uri) = @_;
$log->info("recent artist: $name -> $uri");
$recentArtists{ $uri } = { name => $name };
$class->save;
}
sub updateRecentAlbums {
my ($class, $artist, $album, $uri, $cover) = @_;
$log->info("recent album: $artist, $album -> $uri, $cover");
$recentAlbums{ $uri } = { artist => $artist, album => $album, cover => $cover };
$class->save;
}
sub updateRecentSearches {
my ($class, $name) = @_;
$log->info("recent search: $name");
$recentSearches{ $name } = { name => $name };
$class->save;
}
sub updateRecentPlaylists {
my ($class, $name, $uri) = @_;
$log->info("recent playlist: $name -> $uri");
$recentPlaylists{ $uri } = { name => $name };
$class->save;
}
sub removeRecentPlaylists {
my ($class, $uri) = @_;
$log->info("removing recent playlist: $uri");
delete $recentPlaylists{ $uri };
}
sub recentPlaylists {
my $class = shift;
my $session = shift;
my @menu;
for my $uri (reverse keys %recentPlaylists) {
unshift @menu, {
'name' => $recentPlaylists{$uri}->{'name'},
'url' => MENU,
'uri' => $uri,
'passthrough' => [ 'SinglePlaylist', { %$session, uri => $uri, recent => 1 } ],
'type' => 'playlist',
'itemActions' => Plugins::Spotify::ParserBase->actions({ info => 1, play => 1, uri => $uri }),
};
}
return \@menu;
}
1;