Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions API/Sync.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) = @_;

Expand Down
108 changes: 108 additions & 0 deletions Importer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sub startScan { if (main::SCANNER) {

if (!$playlistsOnly) {
$class->scanAlbums($accounts);
$class->scanFavoriteTracks($accounts);
$class->scanArtists($accounts);
}

Expand Down Expand Up @@ -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) = @_;

Expand Down
12 changes: 12 additions & 0 deletions strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down