diff --git a/API/Sync.pm b/API/Sync.pm index d619832..5e44168 100644 --- a/API/Sync.pm +++ b/API/Sync.pm @@ -103,6 +103,69 @@ sub myAlbums { return $albums; } +sub myFavoriteTracks { + my ($class, $userId) = @_; + + my $offset = 0; + my $tracks = []; + + my $args = { + type => 'tracks', + limit => QOBUZ_LIMIT, + _ttl => QOBUZ_USER_DATA_EXPIRY, + _user_cache => 1, + _use_token => 1, + }; + + do { + $args->{offset} = $offset; + + my $response = $class->_get( + 'favorite/getUserFavorites', + $userId, + $args + ); + + $offset = 0; + + if ( + $response && + ref $response && + $response->{tracks} && + ref $response->{tracks} && + $response->{tracks}->{items} && + ref $response->{tracks}->{items} + ) { + my $items = $response->{tracks}->{items}; + + # Les tracks retournés par favorite/getUserFavorites + # contiennent leur album sous forme brute. + # On le précache dans le même format que myAlbums(). + foreach my $track (@$items) { + next unless $track && ref $track; + next unless $track->{album} && ref $track->{album}; + + my ($album) = @{ _precacheAlbum([ $track->{album} ]) }; + + $track->{album} = $album if $album; + } + + push @$tracks, @{ _precacheTracks($items) }; + + if ( + $response->{tracks}->{total} > QOBUZ_LIMIT && + $response->{tracks}->{offset} < $response->{tracks}->{total} + ) { + $offset = $response->{tracks}->{offset} + QOBUZ_LIMIT; + } + } + + } while $offset && $offset < QOBUZ_USERDATA_LIMIT; + + return $tracks; +} + + sub getAlbum { my ($class, $userId, $albumId) = @_; diff --git a/Importer.pm b/Importer.pm index d0fce5f..8c2a6d0 100644 --- a/Importer.pm +++ b/Importer.pm @@ -58,6 +58,7 @@ sub startScan { if (main::SCANNER) { if (!$playlistsOnly) { $class->scanAlbums($accounts); + $class->scanFavoriteTracks($accounts); $class->scanArtists($accounts); } @@ -148,6 +149,113 @@ sub scanAlbums { main::SCANNER && Slim::Schema->forceCommit; } +sub scanFavoriteTracks { + my ($class, $accounts) = @_; + + my $progress = Slim::Utils::Progress->new({ + 'type' => 'importer', + 'name' => 'plugin_qobuz_favorite_tracks', + 'total' => 1, + 'every' => 1, + }); + + foreach my $account (@$accounts) { + + my $accountName = $account->[0] || ''; + my $userId = $account->[1]; + + $log->warn( + "Reading favorite tracks... account=$accountName userId=$userId" + ); + + $progress->update( + string('PLUGIN_QOBUZ_PROGRESS_READ_ALBUMS', $accountName) + ); + + my $tracks = Plugins::Qobuz::API::Sync->myFavoriteTracks($userId); + $tracks ||= []; + + $tracks = _filterStreamables($tracks); + + $progress->total(scalar @$tracks); + + $log->warn( + "Qobuz favorite tracks: " . + scalar(@$tracks) . + " streamable tracks found for $accountName" + ); + + # Regrouper les tracks par album. + my %albums; + + foreach my $track (@$tracks) { + next unless $track && ref $track; + next unless $track->{album} && ref $track->{album}; + next unless $track->{album}->{id}; + + push @{$albums{$track->{album}->{id}}}, $track; + } + + # Traiter chaque album, comme dans scanAlbums(). + foreach my $albumId (keys %albums) { + + my $albumTracks = $albums{$albumId}; + next unless $albumTracks && @$albumTracks; + + my $album = $albumTracks->[0]->{album}; + next unless $album && ref $album; + + my $albumArtists = { + required => 0, + ids => undef, + names => undef, + }; + + my @attributes; + + foreach my $track (@$albumTracks) { + + $progress->update( + $track->{title} || $track->{id} || '' + ); + + my $attribute = _prepareTrack( + $album, + $track, + $albumArtists + ); + + next unless $attribute; + + # Pour un titre favori individuel, le timestamp est celui + # du track et non celui de l'album. + $attribute->{TIMESTAMP} = $track->{favorited_at} + if $track->{favorited_at}; + + push @attributes, $attribute; + } + + next unless @attributes; + + _checkAlbumArtists(\@attributes, $albumArtists); + + $class->storeTracks( + \@attributes, + undef, + $accountName + ); + + main::SCANNER && Slim::Schema->forceCommit; + } + } + + $progress->final(); + + main::SCANNER && Slim::Schema->forceCommit; +} + + + sub scanArtists { my ($class, $accounts) = @_; diff --git a/strings.txt b/strings.txt index 369e804..ffb5696 100644 --- a/strings.txt +++ b/strings.txt @@ -456,6 +456,18 @@ PLUGIN_QOBUZ_PROGRESS_READ_ALBUMS FR Récupération des albums (%s)... NL Albums ophalen (%s)... +PLUGIN_QOBUZ_FAVORITE_TRACKS_PROGRESS + DE Qobuz Favoriten-Titel + EN Qobuz Favorite Tracks + FR Titres favoris Qobuz + NL Qobuz Favoriete nummers + +PLUGIN_QOBUZ_PROGRESS_READ_FAVORITE_TRACKS + DE Lese Favoriten-Titel (%s)... + EN Fetching favorite tracks (%s)... + FR Récupération des titres favoris (%s)... + NL Favoriete nummers ophalen (%s)... + PLUGIN_QOBUZ_ARTISTS_PROGRESS DE Qobuz Interpreten EN Qobuz Artists