Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse_order and leading zeros #353

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
14 changes: 11 additions & 3 deletions spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion spotify_dl/spotify_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"])
Expand Down