mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 13:03:02 +00:00
use cloud mix images
This commit is contained in:
@@ -32,7 +32,7 @@ def homepage_items():
|
|||||||
return {
|
return {
|
||||||
"artist_mixes": {
|
"artist_mixes": {
|
||||||
"title": "Artist mixes for you",
|
"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(),
|
"items": HomepageStore.get_artist_mixes(),
|
||||||
"extra": {},
|
"extra": {},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -140,3 +140,21 @@ def send_playlist_image(path: PlaylistImagePath):
|
|||||||
"""
|
"""
|
||||||
folder = Paths.get_playlist_img_path()
|
folder = Paths.get_playlist_img_path()
|
||||||
return send_file_or_fallback(folder, path.imgpath, "playlist.svg")
|
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
@@ -2,12 +2,15 @@ import json
|
|||||||
import string
|
import string
|
||||||
import requests
|
import requests
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
from app.db.userdata import SimilarArtistTable
|
from app.db.userdata import SimilarArtistTable
|
||||||
|
from app.lib.colorlib import get_image_colors
|
||||||
from app.models.artist import Artist
|
from app.models.artist import Artist
|
||||||
from app.models.mix import Mix
|
from app.models.mix import Mix
|
||||||
from app.models.track import Track
|
from app.models.track import Track
|
||||||
from app.plugins import Plugin, plugin_method
|
from app.plugins import Plugin, plugin_method
|
||||||
|
from app.settings import Paths
|
||||||
from app.store.artists import ArtistStore
|
from app.store.artists import ArtistStore
|
||||||
from app.store.tracks import TrackStore
|
from app.store.tracks import TrackStore
|
||||||
from app.utils.dates import get_date_range
|
from app.utils.dates import get_date_range
|
||||||
@@ -194,6 +197,14 @@ class MixesPlugin(Plugin):
|
|||||||
if len(mix_tracks) < self.MIN_TRACK_MIX_LENGTH:
|
if len(mix_tracks) < self.MIN_TRACK_MIX_LENGTH:
|
||||||
return None
|
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(
|
return Mix(
|
||||||
# the a prefix indicates that this is an artist mix
|
# the a prefix indicates that this is an artist mix
|
||||||
id=f"a{artist['artisthash']}",
|
id=f"a{artist['artisthash']}",
|
||||||
@@ -203,13 +214,40 @@ class MixesPlugin(Plugin):
|
|||||||
extra={
|
extra={
|
||||||
"type": "artist",
|
"type": "artist",
|
||||||
"artisthash": artist["artisthash"],
|
"artisthash": artist["artisthash"],
|
||||||
"image": {
|
"image": mix_image,
|
||||||
"image": _artist.image,
|
|
||||||
"color": _artist.color,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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(
|
def fallback_create_artist_mix(
|
||||||
self,
|
self,
|
||||||
artist: dict[str, str],
|
artist: dict[str, str],
|
||||||
|
|||||||
@@ -103,6 +103,26 @@ class Paths:
|
|||||||
def get_config_file_path(cls):
|
def get_config_file_path(cls):
|
||||||
return join(cls.get_app_dir(), "settings.json")
|
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
|
# defaults
|
||||||
class Defaults:
|
class Defaults:
|
||||||
|
|||||||
@@ -62,6 +62,12 @@ def create_config_dir() -> None:
|
|||||||
|
|
||||||
playlist_img_path = os.path.join("images", "playlists")
|
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 = [
|
dirs = [
|
||||||
"", # creates the config folder
|
"", # creates the config folder
|
||||||
sm_thumb_path,
|
sm_thumb_path,
|
||||||
@@ -73,6 +79,10 @@ def create_config_dir() -> None:
|
|||||||
md_artist_img_path,
|
md_artist_img_path,
|
||||||
small_artist_img_path,
|
small_artist_img_path,
|
||||||
large_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:
|
for _dir in dirs:
|
||||||
|
|||||||
Reference in New Issue
Block a user