forked from squeezebox-googlemusic/squeezebox-googlemusic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllAccess.pm
668 lines (515 loc) · 16.6 KB
/
AllAccess.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package Plugins::GoogleMusic::AllAccess;
use strict;
use warnings;
use Readonly;
use Slim::Utils::Prefs;
use Slim::Utils::Log;
use Slim::Utils::Cache;
use Plugins::GoogleMusic::GoogleAPI;
use Inline::Python qw(py_eval);
Readonly my $CACHE_TIME => 3600;
# Cache track information for one day because get_album_info() would
# be used too often
Readonly my $CACHE_TIME_LONG => 24 * 3600;
# Cache user modifiable data shorlty
Readonly my $CACHE_TIME_SHORT => 30;
my $log = logger('plugin.googlemusic');
my $prefs = preferences('plugin.googlemusic');
my $googleapi = Plugins::GoogleMusic::GoogleAPI::get();
my $cache;
sub init {
$cache = shift;
return;
}
# Convert an All Access Google Music Song dictionary to a consistent
# robust track representation
sub to_slim_track {
my $song = shift;
my $uri = 'googlemusic:track:' . $song->{storeId};
if (my $track = $cache->get($uri)) {
return $track;
}
my $cover = '/html/images/cover.png';
if (exists $song->{albumArtRef}) {
$cover = $song->{albumArtRef}[0]{url};
$cover = Plugins::GoogleMusic::Image->uri($cover);
}
# Get/create the album for this song
my $album = to_slim_album($song);
# Build track info
my $track = {
uri => $uri,
id => $song->{storeId},
title => $song->{title},
album => $album,
artist => to_slim_artist($song),
year => $song->{year} || 0,
cover => $cover,
secs => $song->{durationMillis} / 1000,
bitrate => 320,
genre => $song->{genre},
filesize => $song->{estimatedSize},
trackNumber => $song->{trackNumber} || 1,
discNumber => $song->{discNumber} || 1,
rating => $song->{rating} || 0,
};
# Add to the cache
$cache->set($uri, $track, $CACHE_TIME_LONG);
return $track;
}
# Convert an All Access Google Music Song dictionary to a consistent
# robust album representation
sub to_slim_album {
my $song = shift;
my $artist = to_slim_album_artist($song);
my $name = $song->{album};
my $year = $song->{year} || 0;
my $uri = 'googlemusic:album:' . $song->{albumId};
my $cover = '/html/images/cover.png';
if (exists $song->{albumArtRef}) {
$cover = $song->{albumArtRef}[0]{url};
$cover = Plugins::GoogleMusic::Image->uri($cover);
}
my $album = {
uri => $uri,
id => $song->{albumId},
name => $name,
artist => $artist,
year => $year,
cover => $cover,
tracks => [],
};
return $album;
}
# Convert an All Access Google Music Song dictionary to a consistent
# robust artist representation
sub to_slim_artist {
my $song = shift;
my $id = scalar $song->{artistId} ? $song->{artistId}[0] : 'unknown';
my $uri = 'googlemusic:artist:' . $id;
my $name = $song->{artist};
my $image = Plugins::GoogleMusic::Image::best($song, 'artistArtRef', 'artistArtRefs', '/html/images/artists.png');
my $artist = {
uri => $uri,
id => $id,
name => $name,
image => $image,
};
return $artist;
}
# Convert an All Access Google Music Song dictionary to a consistent
# robust album artist representation
sub to_slim_album_artist {
my $song = shift;
# In one test case (the band 'Poliça') GoogleMusic messed up the
# 'artist' is sometime lowercase, where the 'albumArtist' is
# uppercase the albumArtist is the most consistent so take that or
# else we will see multiple entries in the Artists listing (lower
# + upper case)
my $name = $song->{albumArtist} || $song->{artist};
# TODO: No album artist ID in tracks. Should we fetch the album info?
my $uri = 'googlemusic:artist:unknown';
# Check to see if this album is a compilation from various
# artists. The Google Music webinterface also shows a 'Various
# artists' in my library instead of all seperate artists.. which
# should justify this functionality
my $various = (index(lc($song->{artist}), lc($song->{albumArtist} || '')) == -1) ? 1 : 0;
my $image;
if ($various) {
$image = '/html/images/artists.png';
}
else {
$image = Plugins::GoogleMusic::Image::best($song, 'artistArtRef', 'artistArtRefs', '/html/images/artists.png');
}
my $artist = {
uri => $uri,
id => 'unknown',
name => $name,
various => $various,
image => $image,
};
return $artist;
}
sub get_track {
my $uri = shift;
return unless $prefs->get('all_access_enabled');
if (my $track = $cache->get($uri)) {
return $track;
}
my ($id) = $uri =~ m{^googlemusic:track:(.*)$}x;
my $song;
eval {
# For some reasons Google only knows this does not return the
# rating of the track. Surprisingly, fetching the whole album
# returns the rating. Thus, we get the track info and after
# that the album info for that track.
$song = $googleapi->get_track_info($id);
};
if ($@) {
$log->error("Not able to get the track info for track ID $id: $@");
return;
}
# Get the album and process all tracks to get them into the cache
my $album = get_album_info('googlemusic:album:' . $song->{albumId});
# Get the track from the cache including the rating field :-)
if (my $track = $cache->get($uri)) {
return $track;
}
$log->error("Not able to get the track info for track ID $id from the album information");
return;
}
sub get_track_by_id {
my $id = shift;
return get_track('googlemusic:track:' . $id);
}
# Search All Access
sub search {
my $query = shift;
return unless $prefs->get('all_access_enabled');
my $uri = 'googlemusic:search:' . $query;
if (my $result = $cache->get($uri)) {
return $result;
}
my $googleResult;
my $result = {
tracks => [],
albums => [],
artists => [],
};
eval {
$googleResult = $googleapi->search($query, $prefs->get('max_search_items'));
};
if ($@) {
$log->error("Not able to search All Access for \"$query\": $@");
return;
}
# These seemingly useless list comprehensions make this work!
# It seems that Inline Python doesn't deal with newlist as used in gmusicapi
# so this list comprehension creates a plain old list, which can be used in Perl
# See http://stackoverflow.com/questions/37937897/how-do-i-use-or-convert-or-view-inline-python-objects-in-perl
my $song_hits = py_eval("[x for x in $googleResult->{song_hits}]", 0);
for my $hit (@$song_hits) {
push @{$result->{tracks}}, to_slim_track($hit->{track});
}
my $album_hits = py_eval("[x for x in $googleResult->{album_hits}]", 0);
for my $hit (@$album_hits) {
push @{$result->{albums}}, album_to_slim_album($hit->{album});
}
my $artist_hits = py_eval("[x for x in $googleResult->{artist_hits}]", 0);
for my $hit (@$artist_hits) {
push @{$result->{artists}}, artist_to_slim_artist($hit->{artist});
}
# Add to the cache
$cache->set($uri, $result, $CACHE_TIME);
return $result;
}
# Search All Access tracks only. Do not cache these searches for now.
sub searchTracks {
my ($query, $maxResults) = @_;
my $googleResult;
my $tracks = [];
return $tracks unless $prefs->get('all_access_enabled');
eval {
$googleResult = $googleapi->search($query, $maxResults);
};
if ($@) {
$log->error("Not able to search All Access for \"$query\": $@");
} else {
for my $hit (@{$googleResult->{song_hits}}) {
push @$tracks, to_slim_track($hit->{track});
}
}
return $tracks;
}
# Get information for an artist
sub get_artist_info {
my $uri = shift;
return unless $prefs->get('all_access_enabled');
if (my $artist = $cache->get($uri)) {
return $artist;
}
my ($id) = $uri =~ m{^googlemusic:artist:(.*)$}x;
my $googleArtist;
my $artist;
eval {
$googleArtist = $googleapi->get_artist_info($id, $Inline::Python::Boolean::true, $prefs->get('max_artist_tracks'), $prefs->get('max_related_artists'));
};
if ($@) {
$log->error("Not able to get the artist info for artist ID $id: $@");
return;
}
$artist = artist_to_slim_artist($googleArtist);
# Add to the cache
$cache->set($uri, $artist, $CACHE_TIME);
return $artist;
}
sub get_artist_image {
my $uri = shift;
return unless $prefs->get('all_access_enabled');
# First try to get the image from the artist cache
if (my $artist = $cache->get($uri)) {
return $artist->{image};
}
my ($id) = $uri =~ m{^googlemusic:artist:(.*)$}x;
my $imageuri = 'googlemusic:artistimage:' . $id;
if (my $artistImage = $cache->get($imageuri)) {
return $artistImage;
}
my $googleArtist;
eval {
$googleArtist = $googleapi->get_artist_info($id, $Inline::Python::Boolean::false, 0, 0);
};
if ($@) {
$log->error("Not able to get the artist image for artist ID $id: $@");
}
my $image = Plugins::GoogleMusic::Image::best($googleArtist, 'artistArtRef', 'artistArtRefs', '/html/images/artists.png');
# Add to the cache
$cache->set($imageuri, $image, $CACHE_TIME);
return $image;
}
# Get information for an album
sub get_album_info {
my $uri = shift;
return unless $prefs->get('all_access_enabled');
if (my $album = $cache->get($uri)) {
return $album;
}
my ($id) = $uri =~ m{^googlemusic:album:(.*)$}x;
my $googleAlbum;
my $album;
eval {
$googleAlbum = $googleapi->get_album_info($id);
};
if ($@) {
$log->error("Not able to get the album info for album ID $id: $@");
return;
}
$album = album_to_slim_album($googleAlbum);
# Add to the cache
$cache->set($uri, $album, $CACHE_TIME);
return $album;
}
# Convert an All Access Google album dictionary to a consistent
# robust album representation
sub album_to_slim_album {
my $googleAlbum = shift;
my $artistId = scalar $googleAlbum->{artistId} ? $googleAlbum->{artistId}[0] : 'unknown';
# TODO: Sometimes the array has multiple entries
my $artist = {
uri => 'googlemusic:artist:' . $artistId,
id => $artistId,
name => $googleAlbum->{albumArtist} || $googleAlbum->{artist},
various => 0,
};
my $name = $googleAlbum->{name};
my $year = $googleAlbum->{year} || 0;
my $uri = 'googlemusic:album:' . $googleAlbum->{albumId};
my $cover = '/html/images/cover.png';
if (exists $googleAlbum->{albumArtRef}) {
$cover = $googleAlbum->{albumArtRef};
$cover = Plugins::GoogleMusic::Image->uri($cover);
}
my $album = {
uri => $uri,
id => $googleAlbum->{albumId},
name => $name,
artist => $artist,
year => $year,
cover => $cover,
tracks => [],
};
if (exists $googleAlbum->{tracks}) {
foreach (@{$googleAlbum->{tracks}}) {
my $track = to_slim_track($_);
if ($track->{album}->{artist}->{various}) {
$artist->{various} = 1;
}
push @{$album->{tracks}}, $track;
}
}
if (exists $googleAlbum->{description}) {
$album->{description} = $googleAlbum->{description};
}
return $album;
}
# Convert an All Access Google Music artist dictionary to a consistent
# robust artist representation
sub artist_to_slim_artist {
my $googleArtist = shift;
my $name = $googleArtist->{name};
my $uri = 'googlemusic:artist:' . $googleArtist->{artistId};
my $image = Plugins::GoogleMusic::Image::best($googleArtist, 'artistArtRef', 'artistArtRefs', '/html/images/artists.png');
my $artist = {
uri => $uri,
id => $googleArtist->{artistId},
name => $name,
image => $image,
tracks => [],
albums => [],
related => [],
};
if (exists $googleArtist->{topTracks}) {
for my $track (@{$googleArtist->{topTracks}}) {
push @{$artist->{tracks}}, to_slim_track($track);
}
}
if (exists $googleArtist->{albums}) {
for my $album (@{$googleArtist->{albums}}) {
push @{$artist->{albums}}, album_to_slim_album($album);
}
}
if (exists $googleArtist->{related_artists}) {
for my $related (@{$googleArtist->{related_artists}}) {
push @{$artist->{related}}, artist_to_slim_artist($related);
}
}
if (exists $googleArtist->{artistBio}) {
$artist->{artistBio} = $googleArtist->{artistBio};
}
return $artist;
}
# Get Google Music genres, either parents or child genres
sub getGenres {
my $uri = shift;
return unless $prefs->get('all_access_enabled');
my ($parent) = $uri =~ m{^googlemusic:genres:(.*)$}x;
my $genres;
if ($genres = $cache->get($uri)) {
return $genres;
}
my $googleGenres;
$genres = [];
eval {
$googleGenres = $googleapi->get_genres($parent);
};
if ($@) {
$log->error("Not able to get genres: $@");
return $genres;
}
for my $genre (@{$googleGenres}) {
push @{$genres}, genreToSlimGenre($genre);
}
$cache->set($uri, $genres, $CACHE_TIME);
return $genres;
}
# Convert an All Access Google Music genre dictionary to a consistent
# robust genre representation
sub genreToSlimGenre {
my $googleGenre = shift;
my $uri = 'googlemusic:genre:' . $googleGenre->{id};
my $image = '/html/images/genres.png';
if (exists $googleGenre->{images}) {
$image = $googleGenre->{images}[0]{url};
$image = Plugins::GoogleMusic::Image->uri($image);
}
my $genre = {
uri => $uri,
id => $googleGenre->{id},
name => $googleGenre->{name},
image => $image,
};
if (exists $googleGenre->{children}) {
$genre->{children} = $googleGenre->{children};
}
if (exists $googleGenre->{parentId}) {
$genre->{parent} = $googleGenre->{parentId};
}
$cache->set($uri, $genre, $CACHE_TIME);
return $genre;
}
# Get all user-created radio stations
sub getStations {
return [] unless $prefs->get('all_access_enabled');
my $stations;
my $uri = 'googlemusic:stations';
if ($stations = $cache->get($uri)) {
return $stations;
}
eval {
$stations = $googleapi->get_all_stations();
};
if ($@) {
$log->error("Not able to get user created radio stations: $@");
return [];
}
$cache->set($uri, $stations, $CACHE_TIME_SHORT);
return $stations;
}
sub deleteStation {
my $id = shift;
eval {
$googleapi->delete_stations($id);
};
if ($@) {
$log->error("Not able to delete radio station $id: $@");
return;
}
# Remove from cache to force reload
$cache->remove('googlemusic:stations');
# Return the ID on success
return $id;
}
# Get a specific genre (from the cache)
sub getGenre {
my $uri = shift;
my $genre;
if ($genre = $cache->get($uri)) {
return $genre;
}
# Not found in the cache. Refresh parent genres.
my $genres = getGenres('googlemusic:genres');
# Try again
if ($genre = $cache->get($uri)) {
return $genre;
}
# Still not found. Must be a child genre.
my ($id) = $uri =~ m{^googlemusic:genre:(.*)$}x;
# Search a matching parent and get its childs
for my $parent (@{$genres}) {
if ( grep { $_ eq $id } @{$parent->{children}} ) {
$genres = getGenres($parent->{uri});
last;
}
}
# Try again
if ($genre = $cache->get($uri)) {
return $genre;
}
$log->error("Not able to get genre: $uri");
return;
}
# Change the rating of a track
sub changeRating {
my ($uri, $rating) = @_;
return unless $prefs->get('all_access_enabled');
my ($id) = $uri =~ m{^googlemusic:track:(.*)$}x;
my $song;
# Get the Google track info first
eval {
$song = $googleapi->get_track_info($id);
};
if ($@) {
$log->error("Not able to get the track info for track ID $id: $@");
return;
}
# Now change the rating value
$song->{rating} = $rating;
# And apply it
eval {
$song = $googleapi->change_song_metadata($song);
};
if ($@) {
$log->error("Not able to change the song metadata for track ID $id: $@");
return;
}
# TBD: This only updates the track in the cach NOT albums, search
# results etc.
# Also need to update our cache. Get it from the cache first.
my $track = get_track($uri);
# Change the rating
$track->{rating} = $rating;
# And update the cache
$cache->set($uri, $track, $CACHE_TIME_LONG);
return;
}
1;