Skip to content

Commit 9f9e04a

Browse files
committed
Replace hasattr() with in because hasattr() does not test for members of a dictionary. Add logging statement for number of songs that were filtered out because they didn't have album data
1 parent 1075d27 commit 9f9e04a

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

ytmusic_deleter/cli.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def delete_uploaded_albums(add_to_library):
6363
progress_bar = manager.counter(total=len(uploaded_albums), desc="Albums Processed", unit="albums")
6464
for album in uploaded_albums:
6565
try:
66-
artist = album["artists"][0]["name"] if hasattr(album, "artists") else const.UNKNOWN_ARTIST
66+
artist = album["artists"][0]["name"] if "artists" in album else const.UNKNOWN_ARTIST
6767
title = album["title"]
6868
logging.info(f"Processing album: {artist} - {title}")
6969
if add_to_library:
@@ -102,7 +102,7 @@ def delete_uploaded_singles():
102102

103103
for single in uploaded_singles:
104104
try:
105-
artist = single["artist"][0]["name"] if hasattr(single, "artist") else const.UNKNOWN_ARTIST
105+
artist = single["artist"][0]["name"] if "artist" in single else const.UNKNOWN_ARTIST
106106
title = single["title"]
107107
response = youtube_auth.delete_upload_entity(single["entityId"])
108108
if response == "STATUS_SUCCEEDED":
@@ -125,7 +125,7 @@ def add_album_to_library(artist, title):
125125
if result["resultType"] == "album" and match_found(result, artist, title):
126126
catalog_album = youtube_auth.get_album(result["browseId"])
127127
logging.info(
128-
f"\tFound matching album \"{catalog_album['artist'][0]['name'] if hasattr(catalog_album, 'artist') else ''} - {catalog_album['title']}\" in YouTube Music. Adding to library..."
128+
f"\tFound matching album \"{catalog_album['artist'][0]['name'] if 'artist' in catalog_album else ''} - {catalog_album['title']}\" in YouTube Music. Adding to library..."
129129
)
130130
success = youtube_auth.rate_playlist(catalog_album["playlistId"], const.LIKE)
131131
if success:
@@ -140,7 +140,7 @@ def match_found(result, artist, title):
140140
try:
141141
resultArtist = str(result["artist"]).lower()
142142
except KeyError:
143-
resultArtist = str(result["artists"][0] if hasattr(result, "artists") else "").lower()
143+
resultArtist = str(result["artists"][0] if "artists" in result else "").lower()
144144
try:
145145
resultTitle = str(result["title"]).lower()
146146
except KeyError:
@@ -181,12 +181,14 @@ def remove_library():
181181
library_songs = []
182182
# Filter out songs where album is None (possible rare occurrence seen here: https://github.com/apastel/ytmusic-deleter/issues/12)
183183
filtered_songs = list(filter(lambda song: song["album"], library_songs))
184+
if len(library_songs) - len(filtered_songs) > 0:
185+
logging.info(f"{len(library_songs) - len(filtered_songs)} songs are not part of an album and won't be deleted.")
184186
# Filter for unique album IDs so that for each song, we can just remove the album it's a part of
185-
filtered_songs = list({v["album"]["id"]: v for v in filtered_songs}.values())
186-
progress_bar = manager.counter(total=len(filtered_songs), desc="Singles Processed", unit="singles")
187-
albums_removed += remove_library_albums_by_song(filtered_songs, progress_bar)
187+
album_unique_songs = list({v["album"]["id"]: v for v in filtered_songs}.values())
188+
progress_bar = manager.counter(total=len(album_unique_songs), desc="Singles Processed", unit="singles")
189+
albums_removed += remove_library_albums_by_song(album_unique_songs, progress_bar)
188190
logging.info(
189-
f"Removed {albums_removed} out of {len(library_albums) + len(filtered_songs)} albums from your library.")
191+
f"Removed {albums_removed} out of {len(library_albums) + len(album_unique_songs)} albums from your library.")
190192

191193

192194
def remove_library_albums(albums, progress_bar):
@@ -214,7 +216,7 @@ def remove_album(browseId):
214216
logging.exception(
215217
f"\tFailed to remove album with ID {browseId} from your library, as it could not be retrieved.")
216218
return False
217-
artist = album["artist"][0]["name"] if hasattr(album, "artist") else const.UNKNOWN_ARTIST
219+
artist = album["artist"][0]["name"] if "artist" in album else const.UNKNOWN_ARTIST
218220
title = album["title"]
219221
logging.info(f"Processing album: {artist} - {title}")
220222
response = youtube_auth.rate_playlist(album["playlistId"], const.INDIFFERENT)
@@ -240,7 +242,7 @@ def unlike_all():
240242
logging.info("Begin unliking songs...")
241243
progress_bar = manager.counter(total=len(your_likes['tracks']), desc="Songs Unliked", unit="songs")
242244
for track in your_likes["tracks"]:
243-
artist = track["artists"][0]["name"] if hasattr(track, "artists") else const.UNKNOWN_ARTIST
245+
artist = track["artists"][0]["name"] if "artists" in track else const.UNKNOWN_ARTIST
244246
title = track["title"]
245247
logging.info(f"Processing track: {artist} - {title}")
246248
if track["album"] is None:

0 commit comments

Comments
 (0)