fixed save_cover

This commit is contained in:
Xoconoch
2025-06-03 16:44:56 -06:00
parent 932f27711e
commit f1fab1257b
6 changed files with 114 additions and 149 deletions

View File

@@ -588,18 +588,13 @@ class EASY_DW:
"artist": self.__song_metadata.get("artist", ""),
"status": "progress"
}
# Use Spotify URL if available, otherwise use Deezer link
spotify_url = getattr(self.__preferences, 'spotify_url', None)
progress_data["url"] = spotify_url if spotify_url else self.__link
# Add parent info if present
if self.__parent == "playlist" and hasattr(self.__preferences, "json_data"):
playlist_data = self.__preferences.json_data
playlist_name = playlist_data.get('title', 'unknown')
total_tracks = getattr(self.__preferences, 'total_tracks', 0)
current_track = getattr(self.__preferences, 'track_number', 0)
progress_data.update({
"current_track": current_track,
"total_tracks": total_tracks,
@@ -616,7 +611,6 @@ class EASY_DW:
album_artist = self.__song_metadata.get('album_artist', self.__song_metadata.get('album_artist', ''))
total_tracks = getattr(self.__preferences, 'total_tracks', 0)
current_track = getattr(self.__preferences, 'track_number', 0)
progress_data.update({
"current_track": current_track,
"total_tracks": total_tracks,
@@ -628,30 +622,31 @@ class EASY_DW:
"url": f"https://deezer.com/album/{self.__preferences.song_metadata.get('album_id', '')}"
}
})
Download_JOB.report_progress(progress_data)
try:
# Decrypt the file using the utility function
decryptfile(c_crypted_audio, self.__fallback_ids, self.__song_path)
logger.debug(f"Successfully decrypted track using {encryption_type} encryption")
except Exception as decrypt_error:
# Detailed error logging for debugging
logger.error(f"Decryption error ({encryption_type}): {str(decrypt_error)}")
if "Data must be padded" in str(decrypt_error):
logger.error("This appears to be a padding issue with Blowfish decryption")
raise
self.__add_more_tags()
# Start of processing block (decryption, tagging, cover, conversion)
# Decrypt the file using the utility function
decryptfile(c_crypted_audio, self.__fallback_ids, self.__song_path)
logger.debug(f"Successfully decrypted track using {encryption_type} encryption")
self.__add_more_tags() # self.__song_metadata is updated here
self.__c_track.tags = self.__song_metadata # IMPORTANT: Update track object's tags
# Save cover image if requested
if self.__preferences.save_cover and self.__song_metadata.get('image'):
try:
track_directory = os.path.dirname(self.__song_path)
save_cover_image(self.__song_metadata['image'], track_directory, "cover.jpg")
logger.info(f"Saved cover image for track in {track_directory}")
except Exception as e_img_save:
logger.warning(f"Failed to save cover image for track: {e_img_save}")
# Apply audio conversion if requested
if self.__convert_to:
format_name, bitrate = parse_format_string(self.__convert_to)
if format_name:
# Register and unregister functions for tracking downloads
from deezspot.deezloader.__download__ import register_active_download, unregister_active_download
from deezspot.deezloader.__download__ import register_active_download, unregister_active_download # Ensure these are available or handle differently
try:
# Update the path with the converted file path
converted_path = convert_audio(
self.__song_path,
format_name,
@@ -660,97 +655,52 @@ class EASY_DW:
unregister_active_download
)
if converted_path != self.__song_path:
# Update path in track object if conversion happened
self.__song_path = converted_path
self.__c_track.song_path = converted_path
except Exception as conv_error:
# Log conversion error but continue with original file
logger.error(f"Audio conversion error: {str(conv_error)}")
# Decide if this is a fatal error for the track or if we proceed with original
# Write tags to the final file (original or converted)
write_tags(self.__c_track)
except Exception as e:
self.__c_track.success = True # Mark as successful only after all steps including tags
except Exception as e: # Handles errors from __write_track, decrypt, add_tags, save_cover, convert, write_tags
if isfile(self.__song_path):
os.remove(self.__song_path)
# Improve error message formatting
error_msg = str(e)
if "Data must be padded" in error_msg:
error_msg = "Decryption error (padding issue) - Try a different quality setting or download format"
elif isinstance(e, ConnectionError) or "Connection" in error_msg:
error_msg = "Connection error - Check your internet connection"
elif "timeout" in error_msg.lower():
error_msg = "Request timed out - Server may be busy"
elif "403" in error_msg or "Forbidden" in error_msg:
error_msg = "Access denied - Track might be region-restricted or premium-only"
elif "404" in error_msg or "Not Found" in error_msg:
error_msg = "Track not found - It might have been removed"
if "Data must be padded" in error_msg: error_msg = "Decryption error (padding issue) - Try a different quality setting or download format"
elif isinstance(e, ConnectionError) or "Connection" in error_msg: error_msg = "Connection error - Check your internet connection"
elif "timeout" in error_msg.lower(): error_msg = "Request timed out - Server may be busy"
elif "403" in error_msg or "Forbidden" in error_msg: error_msg = "Access denied - Track might be region-restricted or premium-only"
elif "404" in error_msg or "Not Found" in error_msg: error_msg = "Track not found - It might have been removed"
# Create formatted error report
progress_data = {
"type": "track",
"status": "error",
"song": self.__song_metadata.get('music', ''),
"artist": self.__song_metadata.get('artist', ''),
"error": error_msg,
"url": getattr(self.__preferences, 'spotify_url', None) or self.__link,
# (Error reporting code as it exists)
error_progress_data = {
"type": "track", "status": "error",
"song": self.__song_metadata.get('music', ''), "artist": self.__song_metadata.get('artist', ''),
"error": error_msg, "url": getattr(self.__preferences, 'spotify_url', None) or self.__link,
"convert_to": self.__convert_to
}
# Add parent info based on parent type
if self.__parent == "playlist" and hasattr(self.__preferences, "json_data"):
playlist_data = self.__preferences.json_data
playlist_name = playlist_data.get('title', 'unknown')
total_tracks = getattr(self.__preferences, 'total_tracks', 0)
current_track = getattr(self.__preferences, 'track_number', 0)
progress_data.update({
"current_track": current_track,
"total_tracks": total_tracks,
"parent": {
"type": "playlist",
"name": playlist_name,
"owner": playlist_data.get('creator', {}).get('name', 'unknown'),
"total_tracks": total_tracks,
"url": f"https://deezer.com/playlist/{playlist_data.get('id', '')}"
}
})
playlist_data = self.__preferences.json_data; playlist_name = playlist_data.get('title', 'unknown')
total_tracks = getattr(self.__preferences, 'total_tracks', 0); current_track = getattr(self.__preferences, 'track_number', 0)
error_progress_data.update({"current_track": current_track, "total_tracks": total_tracks, "parent": {"type": "playlist", "name": playlist_name, "owner": playlist_data.get('creator', {}).get('name', 'unknown'), "total_tracks": total_tracks, "url": f"https://deezer.com/playlist/{playlist_data.get('id', '')}"}})
elif self.__parent == "album":
album_name = self.__song_metadata.get('album', '')
album_artist = self.__song_metadata.get('album_artist', self.__song_metadata.get('album_artist', ''))
total_tracks = getattr(self.__preferences, 'total_tracks', 0)
current_track = getattr(self.__preferences, 'track_number', 0)
progress_data.update({
"current_track": current_track,
"total_tracks": total_tracks,
"parent": {
"type": "album",
"title": album_name,
"artist": album_artist,
"total_tracks": total_tracks,
"url": f"https://deezer.com/album/{self.__preferences.song_metadata.get('album_id', '')}"
}
})
# Report the error
Download_JOB.report_progress(progress_data)
album_name = self.__song_metadata.get('album', ''); album_artist = self.__song_metadata.get('album_artist', self.__song_metadata.get('album_artist', ''))
total_tracks = getattr(self.__preferences, 'total_tracks', 0); current_track = getattr(self.__preferences, 'track_number', 0)
error_progress_data.update({"current_track": current_track, "total_tracks": total_tracks, "parent": {"type": "album", "title": album_name, "artist": album_artist, "total_tracks": total_tracks, "url": f"https://deezer.com/album/{self.__preferences.song_metadata.get('album_id', '')}"}})
Download_JOB.report_progress(error_progress_data)
logger.error(f"Failed to process track: {error_msg}")
# Still raise the exception to maintain original flow
# Add the original exception e to the message for more context
self.__c_track.success = False # Mark as failed
self.__c_track.error_message = error_msg # Store the refined error message
self.__c_track.success = False
self.__c_track.error_message = error_msg
raise TrackNotFound(f"Failed to process {self.__song_path}. Error: {error_msg}. Original Exception: {str(e)}")
# If download and processing (like decryption, tagging) were successful before conversion
if not self.__convert_to: # Or if conversion was successful
self.__c_track.success = True
return self.__c_track
except Exception as e:
# Add more context to this exception
except Exception as e: # Outer exception for initial media checks, etc.
song_title = self.__song_metadata.get('music', 'Unknown Song')
artist_name = self.__song_metadata.get('artist', 'Unknown Artist')
error_message = f"Download failed for '{song_title}' by '{artist_name}' (Link: {self.__link}). Error: {str(e)}"
@@ -859,6 +809,9 @@ class EASY_DW:
if self.__infos_dw.get('LYRICS_ID', 0) != 0:
need = API_GW.get_lyric(self.__ids)
if "LYRICS_TEXT" in need:
self.__song_metadata['lyric'] = need["LYRICS_TEXT"]
if "LYRICS_SYNC_JSON" in need:
self.__song_metadata['lyric_sync'] = trasform_sync_lyric(
need['LYRICS_SYNC_JSON']
@@ -1079,7 +1032,7 @@ class DW_ALBUM:
tracks.append(track)
# Save album cover image
if album.image and album_base_directory:
if self.__preferences.save_cover and album.image and album_base_directory:
save_cover_image(album.image, album_base_directory, "cover.jpg")
if self.__make_zip:
@@ -1342,14 +1295,15 @@ class DW_EPISODE:
Download_JOB.report_progress(progress_data)
# Save cover image for the episode
episode_image_md5 = infos_dw.get('EPISODE_IMAGE_MD5', '')
episode_image_data = None
if episode_image_md5:
episode_image_data = API.choose_img(episode_image_md5, size="1200x1200")
if episode_image_data:
episode_directory = os.path.dirname(output_path)
save_cover_image(episode_image_data, episode_directory, "cover.jpg")
if self.__preferences.save_cover:
episode_image_md5 = infos_dw.get('EPISODE_IMAGE_MD5', '')
episode_image_data = None
if episode_image_md5:
episode_image_data = API.choose_img(episode_image_md5, size="1200x1200")
if episode_image_data:
episode_directory = os.path.dirname(output_path)
save_cover_image(episode_image_data, episode_directory, "cover.jpg")
return episode