Skip to content

Commit

Permalink
refactor: Reducing code and simplifying some logical statements
Browse files Browse the repository at this point in the history
  • Loading branch information
pabera committed Apr 7, 2024
1 parent b4e849f commit dffc3fe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 43 deletions.
39 changes: 15 additions & 24 deletions src/jukebox/components/playermpd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,40 +524,31 @@ def get_single_coverart(self, song_url):
"""
Saves the album art image to a cache and returns the filename.
"""
base_filename = slugify(song_url)
metadata_list = self.mpd_client.listallinfo(song_url)
metadata = metadata_list[0] if metadata_list else {}

try:
metadata_list = self.mpd_client.listallinfo(song_url)
metadata = {}
if metadata_list:
metadata = metadata_list[0]

if 'albumartist' in metadata and 'album' in metadata:
base_filename = slugify(f"{metadata['albumartist']}-{metadata['album']}")

cache_filename = self.coverart_cache_manager.find_file_by_hash(base_filename)
if 'albumartist' in metadata and 'album' in metadata:
base_filename = slugify(f"{metadata.get('albumartist', '')}-{metadata.get('album', '')}")
else:
base_filename = slugify(song_url)

if cache_filename:
return cache_filename
cache_filename = self.coverart_cache_manager.find_file_by_hash(base_filename)

if cache_filename is NO_CONTENT:
return ''
if cache_filename or cache_filename is NO_CONTENT:
return cache_filename or ''

# Cache file does not exist
try:
# Fetch cover art binary
album_art_data = self.mpd_client.readpicture(song_url)

# Save to cache
cache_filename = self.coverart_cache_manager.save_to_cache(base_filename, album_art_data)

return cache_filename
# Save to cache and return the filename
return self.coverart_cache_manager.save_to_cache(base_filename, album_art_data)

except mpd.base.CommandError as e:
logger.error(f"{e.__class__.__qualname__}: {e} at uri {song_url}")
logger.error(f'{e.__class__.__qualname__}: {e} at uri {song_url}')
except Exception as e:
logger.error(f"{e.__class__.__qualname__}: {e} at uri {song_url}")
logger.error(f'{e.__class__.__qualname__}: {e} at uri {song_url}')

return ""
return ''

@plugs.tag
def get_album_coverart(self, albumartist: str, album: str):
Expand Down
36 changes: 17 additions & 19 deletions src/jukebox/components/playermpd/coverart_cache_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path
import jukebox.cfghandler

NO_COVER_ART_EXTENSION = 'no-art'
Expand All @@ -7,34 +7,32 @@

cfg = jukebox.cfghandler.get_handler('jukebox')


class CoverartCacheManager:
def __init__(self):
coverart_cache_path = cfg.setndefault('webapp', 'coverart_cache_path', value='../../src/webapp/build/cover-cache')
self.cache_folder_path = os.path.expanduser(coverart_cache_path)
self.cache_folder_path = Path(coverart_cache_path).expanduser()

def find_file_by_hash(self, hash_value):
for filename in os.listdir(self.cache_folder_path):
if filename.startswith(hash_value):
if filename.endswith(f'.{NO_COVER_ART_EXTENSION}'):
def find_file_by_hash(self, hash_value: str) -> str:
for path in self.cache_folder_path.iterdir():
if path.name.startswith(hash_value):
if path.suffix == f'.{NO_COVER_ART_EXTENSION}':
return NO_CONTENT
return filename
return path.name
return NOT_CACHED

def save_to_cache(self, base_filename, album_art_data):
def save_to_cache(self, base_filename: str, album_art_data: dict) -> str:
file_extension = NO_COVER_ART_EXTENSION
data = b''

if album_art_data:
mime_type = album_art_data['type']
mime_type = album_art_data.get('type', 'image/jpeg')
file_extension = 'jpg' if mime_type == 'image/jpeg' else mime_type.split('/')[-1]
data = album_art_data['binary']
else:
file_extension = NO_COVER_ART_EXTENSION
data = b''
data = album_art_data.get('binary', b'')

cache_filename = f"{base_filename}.{file_extension}"
full_path = self.cache_folder_path / cache_filename

with open(os.path.join(self.cache_folder_path, cache_filename), 'wb') as file:
with full_path.open('wb') as file:
file.write(data)

if data:
return cache_filename

return NO_CONTENT
return cache_filename if data else NO_CONTENT

0 comments on commit dffc3fe

Please sign in to comment.