diff --git a/spotify_dl/spotify.py b/spotify_dl/spotify.py index a3511b5..e54ab2c 100644 --- a/spotify_dl/spotify.py +++ b/spotify_dl/spotify.py @@ -4,7 +4,7 @@ from rich.progress import Progress -def fetch_tracks(sp, item_type, url): +def fetch_tracks(sp, item_type, url, reverse_order=False): """ Fetches tracks from the provided URL. :param sp: Spotify client @@ -73,6 +73,10 @@ def fetch_tracks(sp, item_type, url): genre = genres[0] else: genre = "" + if reverse_order: + playlist_num = str(total_songs - offset).zfill(len(str(total_songs))) + else: + playlist_num = str(offset + 1).zfill(len(str(total_songs))) songs_list.append( { "name": track_name, @@ -81,7 +85,7 @@ def fetch_tracks(sp, item_type, url): "year": track_year, "num_tracks": album_total, "num": track_num, - "playlist_num": offset + 1, + "playlist_num": playlist_num, "cover": cover, "genre": genre, "spotify_id": spotify_id, @@ -134,6 +138,10 @@ def fetch_tracks(sp, item_type, url): ][0] else: genre = "" + if reverse_order: + playlist_num = str(total_songs - offset).zfill(len(str(total_songs))) + else: + playlist_num = str(offset + 1).zfill(len(str(total_songs))) for item in items["items"]: track_name = item.get("name") track_artist = ", ".join( @@ -150,7 +158,7 @@ def fetch_tracks(sp, item_type, url): "num_tracks": album_total, "num": track_num, "track_url": None, - "playlist_num": offset + 1, + "playlist_num": playlist_num, "cover": cover, "genre": genre, "spotify_id": spotify_id, diff --git a/spotify_dl/spotify_dl.py b/spotify_dl/spotify_dl.py index d4e3d13..ff288b7 100755 --- a/spotify_dl/spotify_dl.py +++ b/spotify_dl/spotify_dl.py @@ -76,6 +76,13 @@ def spotify_dl(): action="store_true", help="Whether to keep original playlist ordering or not.", ) + parser.add_argument( + "-R", + "--reverse_order", + default=False, + action="store_true", + help="Reverse the playlist order if --keep_playlist_order is set.", + ) parser.add_argument( "-m", "--skip_mp3", @@ -198,7 +205,7 @@ def spotify_dl(): ) url_dict["save_path"].mkdir(parents=True, exist_ok=True) log.info("Saving songs to %s directory", directory_name) - url_dict["songs"] = fetch_tracks(sp, item_type, url) + url_dict["songs"] = fetch_tracks(sp, item_type, url, args.reverse_order) url_data["urls"].append(url_dict.copy()) if args.dump_json is True: dump_json(url_dict["songs"])