From 3a1045d6ce4c4152048db0d83fec5cabcacf3536 Mon Sep 17 00:00:00 2001 From: Andy Pack Date: Wed, 9 Nov 2022 08:56:23 +0000 Subject: [PATCH] fixing a divide by 0, dict instead of list --- music/tasks/run_user_playlist.py | 6 +++--- music/tasks/update_tag.py | 23 +++++++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/music/tasks/run_user_playlist.py b/music/tasks/run_user_playlist.py index 8b3ed83..1377598 100644 --- a/music/tasks/run_user_playlist.py +++ b/music/tasks/run_user_playlist.py @@ -78,9 +78,9 @@ def run_user_playlist(user: User, playlist: Playlist, spotnet: SpotNetwork = Non try: if not playlist.include_spotify_owned: - user_playlists = [(i.name, i.uri) for i in spotnet.playlists() if 'spotify' not in i.owner.display_name.lower()] + user_playlists = {i.name: i.uri for i in spotnet.playlists() if 'spotify' not in i.owner.display_name.lower()} else: - user_playlists = [(i.name, i.uri) for i in spotnet.playlists()] + user_playlists = {i.name: i.uri for i in spotnet.playlists()} except SpotifyNetworkException as e: logger.exception(f'error occured while retrieving playlists {username} / {playlist_name}') raise e @@ -102,7 +102,7 @@ def run_user_playlist(user: User, playlist: Playlist, spotnet: SpotNetwork = Non log_name = uri except ValueError: # is a playlist name - part_playlist = next((i for i in user_playlists if i[0] == part_name), None) + part_playlist = user_playlists.get(part_name) if part_playlist is None: logger.warning(f'playlist {part_name} not found {username} / {playlist_name}') continue diff --git a/music/tasks/update_tag.py b/music/tasks/update_tag.py index e286498..4391758 100644 --- a/music/tasks/update_tag.py +++ b/music/tasks/update_tag.py @@ -61,12 +61,6 @@ def update_tag(user, tag, spotnet=None, fmnet=None): tag.count = 0 tag.total_time_ms = 0 - try: - user_scrobbles = fmnet.user_scrobble_count() - except LastFMNetworkException: - logger.exception(f'error retrieving scrobble count {username} / {tag_id}') - user_scrobbles = 1 - artists = [] for artist in tag.artists: try: @@ -160,8 +154,21 @@ def update_tag(user, tag, spotnet=None, fmnet=None): tag.artists = artists tag.total_time = seconds_to_time_str(milliseconds=tag.total_time_ms) - tag.total_user_scrobbles = user_scrobbles - tag.proportion = (tag.count / user_scrobbles) * 100 + + try: + user_scrobbles = fmnet.user_scrobble_count() + except LastFMNetworkException: + logger.exception(f'error retrieving scrobble count {username} / {tag_id}') + user_scrobbles = 0 + + if user_scrobbles > 0: + tag.total_user_scrobbles = user_scrobbles + tag.proportion = (tag.count / user_scrobbles) * 100 + else: + logger.warning(f'user scrobble count for {username} returned 0') + tag.total_user_scrobbles = 0 + tag.proportion = 0 + tag.last_updated = datetime.utcnow() tag.update()