From ec1365641bfd8ceeb0310f6c1940318211fa5858 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Tue, 4 Feb 2025 22:27:05 -0500 Subject: [PATCH] Add saving User Library as well Fortunately the api shape was more or less compatable! --- cli.pl | 15 +++++++++++++-- get-token.pl | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cli.pl b/cli.pl index 5cf6170..a33645c 100755 --- a/cli.pl +++ b/cli.pl @@ -58,6 +58,9 @@ ${desc}By $p->{owner}->{display_name} EOM my $url = "$API/playlists/$p->{id}?fields=tracks.next,tracks.items(added_at,track.name,track.artists(name))"; + if ($p->{id} eq "LIBRARY") { + $url = "$API/me/tracks"; + } my $i = 1; while (1) { my $res = $ua->get($url); @@ -68,7 +71,7 @@ EOM } # NOTE: after paging, the shape changes.. my $j = decode_json($res->decoded_content); - my $tracks = $url =~ /offset/ ? $j : $j->{tracks}; + my $tracks = $j->{tracks} || $j; my $n = scalar @{$tracks->{items}}; print("$n Tracks\n"); foreach my $t (@{$tracks->{items}}) { @@ -100,8 +103,16 @@ sub writep { close $fh; } +my %library = ( + id => "LIBRARY", + name => "My Saved Songs", + owner => { + display_name => $user + } +); + my @playlists = get_playlists(); -foreach my $p (@playlists) { +foreach my $p (\%library, @playlists) { print("Processing $p->{name}\n"); writep($p); } diff --git a/get-token.pl b/get-token.pl index 86dea23..497d97c 100644 --- a/get-token.pl +++ b/get-token.pl @@ -17,7 +17,7 @@ my $AUTH_URL = "https://accounts.spotify.com/authorize" . "?client_id=$CLIENT_ID" . "&response_type=code" . "&redirect_uri=http://localhost:3000/auth" - . "&scope=playlist-read-private%20playlist-read-collaborative" + . "&scope=user-library-read%20playlist-read-private%20playlist-read-collaborative" . "&state=" . int(rand() * 100); get '/' => sub { -- libgit2 1.8.1