Skip to content

Commit 4ec245e

Browse files
authored
fixes #17 substitute 'z' for missing fields (#18)
1 parent d654d29 commit 4ec245e

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

ytmusic_deleter/cli.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from ytmusic_deleter import constants as const
1212
from ytmusicapi import YTMusic
1313

14-
1514
manager = enlighten.get_manager()
1615
progress_bar = None
1716

@@ -392,6 +391,20 @@ def delete_all(ctx):
392391
ctx.invoke(delete_playlists)
393392

394393

394+
def mk_track_key(track):
395+
try:
396+
artists = track["artists"]
397+
artist = artists[0]["name"].lower() if artists else "z"
398+
album = track["album"]
399+
album_title = album["name"] if album else "z"
400+
return (re.sub(r"^(the |a )", "", artist),
401+
album_title,
402+
track["title"])
403+
except TypeError:
404+
print(track)
405+
raise
406+
407+
395408
@cli.command()
396409
@click.argument("playlist_title")
397410
@click.option(
@@ -427,11 +440,7 @@ def sort_playlist(ctx, shuffle, playlist_title):
427440
t
428441
for t in sorted(
429442
playlist["tracks"],
430-
key=lambda t: (
431-
re.sub(r"^(the |a )", "", t["artists"][0]["name"].lower()),
432-
t["album"]["name"],
433-
t["title"],
434-
),
443+
key=lambda t: mk_track_key(t)
435444
)
436445
]
437446

0 commit comments

Comments
 (0)