use cloud mix images

This commit is contained in:
cwilvx
2024-10-29 22:40:30 +03:00
parent fe09a8a8ae
commit f6f66c571c
5 changed files with 91 additions and 5 deletions
+1 -1
View File
@@ -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": {},
},
+18
View File
@@ -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/<imgpath>")
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/<imgpath>")
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")
+42 -4
View File
@@ -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],
+20
View File
@@ -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:
+10
View File
@@ -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: