diff --git a/app/api/home/__init__.py b/app/api/home/__init__.py index 9c50a5ec..4c5a865e 100644 --- a/app/api/home/__init__.py +++ b/app/api/home/__init__.py @@ -32,7 +32,7 @@ def homepage_items(): return { "artist_mixes": { "title": "Artist mixes for you", - "description": "Curated based on artists you have played in the past", + "description": "Based on artists you have been listening to", "items": HomepageStore.get_artist_mixes(), "extra": {}, }, diff --git a/app/api/imgserver.py b/app/api/imgserver.py index c5e2db49..a3aa8d48 100644 --- a/app/api/imgserver.py +++ b/app/api/imgserver.py @@ -140,3 +140,21 @@ def send_playlist_image(path: PlaylistImagePath): """ folder = Paths.get_playlist_img_path() return send_file_or_fallback(folder, path.imgpath, "playlist.svg") + +# MIXES +@api.get("/mix/medium/") +def send_md_mix_image(path: ImagePath): + """ + Get medium mix image + """ + folder = Paths.get_md_mixes_img_path() + return send_file_or_fallback(folder, path.imgpath, "playlist.svg") + + +@api.get("/mix/small/") +def send_sm_mix_image(path: ImagePath): + """ + Get small mix image + """ + folder = Paths.get_sm_mixes_img_path() + return send_file_or_fallback(folder, path.imgpath, "playlist.svg") diff --git a/app/plugins/mixes.py b/app/plugins/mixes.py index 5adac0b7..f1a47d66 100644 --- a/app/plugins/mixes.py +++ b/app/plugins/mixes.py @@ -2,12 +2,15 @@ import json import string import requests from urllib.parse import quote +from PIL import Image from app.db.userdata import SimilarArtistTable +from app.lib.colorlib import get_image_colors from app.models.artist import Artist from app.models.mix import Mix from app.models.track import Track from app.plugins import Plugin, plugin_method +from app.settings import Paths from app.store.artists import ArtistStore from app.store.tracks import TrackStore from app.utils.dates import get_date_range @@ -194,6 +197,14 @@ class MixesPlugin(Plugin): if len(mix_tracks) < self.MIN_TRACK_MIX_LENGTH: return None + # try downloading artist image + mix_image = {"image": _artist.image, "color": _artist.color} + downloaded_img_color = self.download_artist_image(_artist) + + if downloaded_img_color: + mix_image["image"] = f"{_artist.artisthash}.jpg" + mix_image["color"] = downloaded_img_color[0] + return Mix( # the a prefix indicates that this is an artist mix id=f"a{artist['artisthash']}", @@ -203,13 +214,40 @@ class MixesPlugin(Plugin): extra={ "type": "artist", "artisthash": artist["artisthash"], - "image": { - "image": _artist.image, - "color": _artist.color, - }, + "image": mix_image, }, ) + def download_artist_image(self, artist: Artist): + res = requests.get(f"{self.server}/image?artist={artist.name}") + + if res.status_code == 200: + # save to file + with open( + f"{Paths.get_md_mixes_img_path()}/{artist.artisthash}.jpg", "wb" + ) as f: + f.write(res.content) + + # resize to 256x256 + img = Image.open(f"{Paths.get_md_mixes_img_path()}/{artist.artisthash}.jpg") + aspect_ratio = img.width / img.height + + newwidth = 256 + + if aspect_ratio > 1: + newheight = int(256 / aspect_ratio) + else: + newheight = 256 + + img = img.resize((newwidth, newheight), Image.LANCZOS) + img.save(f"{Paths.get_sm_mixes_img_path()}/{artist.artisthash}.jpg") + + return get_image_colors( + f"{Paths.get_sm_mixes_img_path()}/{artist.artisthash}.jpg" + ) + + return None + def fallback_create_artist_mix( self, artist: dict[str, str], diff --git a/app/settings.py b/app/settings.py index 3577ca4f..df06e3d9 100644 --- a/app/settings.py +++ b/app/settings.py @@ -103,6 +103,26 @@ class Paths: def get_config_file_path(cls): return join(cls.get_app_dir(), "settings.json") + @classmethod + def get_mixes_img_path(cls): + return join(cls.get_img_path(), "mixes") + + @classmethod + def get_artist_mixes_img_path(cls): + return join(cls.get_mixes_img_path(), "artists") + + @classmethod + def get_og_mixes_img_path(cls): + return join(cls.get_mixes_img_path(), "original") + + @classmethod + def get_md_mixes_img_path(cls): + return join(cls.get_mixes_img_path(), "medium") + + @classmethod + def get_sm_mixes_img_path(cls): + return join(cls.get_mixes_img_path(), "small") + # defaults class Defaults: diff --git a/app/setup/files.py b/app/setup/files.py index 4e875750..72de4383 100644 --- a/app/setup/files.py +++ b/app/setup/files.py @@ -62,6 +62,12 @@ def create_config_dir() -> None: playlist_img_path = os.path.join("images", "playlists") + + mixes_img_path = settings.Paths.get_mixes_img_path() + og_mixes_img_path = settings.Paths.get_og_mixes_img_path() + md_mixes_img_path = settings.Paths.get_md_mixes_img_path() + sm_mixes_img_path = settings.Paths.get_sm_mixes_img_path() + dirs = [ "", # creates the config folder sm_thumb_path, @@ -73,6 +79,10 @@ def create_config_dir() -> None: md_artist_img_path, small_artist_img_path, large_artist_img_path, + mixes_img_path, + og_mixes_img_path, + md_mixes_img_path, + sm_mixes_img_path, ] for _dir in dirs: