From 3bf31903f61730be8e80789173eaa9b9e807119f Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Tue, 4 Feb 2025 22:07:48 -0500 Subject: [PATCH] Fix broken track pagination Here I was being a good citizen and using their field filters and who would have thought it'd change the whole api shape?? --- cli.pl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cli.pl b/cli.pl index 3635e09..03f104d 100755 --- a/cli.pl +++ b/cli.pl @@ -68,8 +68,12 @@ EOM my $err = $res->decoded_content; die "Failed to fetch playlist: $err"; } + # NOTE: after paging, the shape changes.. my $j = decode_json($res->decoded_content); - foreach my $t (@{$j->{tracks}->{items}}) { + my $tracks = $url =~ /offset/ ? $j : $j->{tracks}; + my $n = scalar @{$tracks->{items}}; + print("$n Tracks\n"); + foreach my $t (@{$tracks->{items}}) { (my $d = $t->{added_at}) =~ s/T.*Z$//; my $fart = @{$t->{track}->{artists}}[0]->{name}; $s .= sprintf( @@ -78,11 +82,12 @@ EOM ); $i += 1; } - if (!$j->{tracks}->{next}) { + if (!$tracks->{next}) { last; } - print("Paging Tracks: $url\n"); - $url = $j->{tracks}->{next}; + # HACK: the fields filter breaks in the next URL, + # so we have to strip the tracks. prefix + ($url = $tracks->{next}) =~ s/tracks\.//g; } return $s; } -- libgit2 1.8.1