adapt to new cloud endpoints

+ export artists to json
This commit is contained in:
cwilvx
2024-12-11 14:22:20 +03:00
parent 8ff283cbcb
commit 77485dd0a7
3 changed files with 50 additions and 27 deletions
+34 -26
View File
@@ -1,4 +1,5 @@
from gettext import ngettext from gettext import ngettext
from io import BytesIO
import json import json
import random import random
import time import time
@@ -6,8 +7,6 @@ import requests
from PIL import Image from PIL import Image
from app.db.userdata import MixTable from app.db.userdata import MixTable
from app.lib.colorlib import get_image_colors
from app.lib.playlistlib import get_first_4_images
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
@@ -42,9 +41,10 @@ class MixesPlugin(Plugin):
def __init__(self): def __init__(self):
super().__init__("mixes", "Mixes") super().__init__("mixes", "Mixes")
self.server = "https://smcloud.mungaist.com" self.server = "https://smcloud.mungaist.com"
# self.server = "http://localhost:1956"
server_online = self.ping_server() # server_online = self.ping_server()
self.set_active(server_online) self.set_active(True)
def ping_server(self): def ping_server(self):
max_retries = 3 max_retries = 3
@@ -224,7 +224,9 @@ class MixesPlugin(Plugin):
artist["tracks"], key=lambda x: artist["tracks"][x], reverse=True artist["tracks"], key=lambda x: artist["tracks"][x], reverse=True
) )
mix = self.create_artist_mix(artist, trackhashes[:self.MAX_TRACKS_TO_FETCH]) mix = self.create_artist_mix(
artist, trackhashes[: self.MAX_TRACKS_TO_FETCH]
)
if mix: if mix:
mixes.append(mix) mixes.append(mix)
@@ -274,7 +276,9 @@ class MixesPlugin(Plugin):
# sourcetracks = tracks[: self.MAX_TRACKS_TO_FETCH] # sourcetracks = tracks[: self.MAX_TRACKS_TO_FETCH]
# INFO: Sort the trackhashes when creating the sourcehash # INFO: Sort the trackhashes when creating the sourcehash
sourcehash = create_hash(*sorted(trackhashes, key=lambda x: trackhashes.index(x))) sourcehash = create_hash(
*sorted(trackhashes, key=lambda x: trackhashes.index(x))
)
db_mix = MixTable.get_by_sourcehash(sourcehash) db_mix = MixTable.get_by_sourcehash(sourcehash)
if db_mix: if db_mix:
@@ -289,11 +293,10 @@ class MixesPlugin(Plugin):
# try downloading artist image # try downloading artist image
mix_image = {"image": _artist.artist.image, "color": _artist.artist.color} mix_image = {"image": _artist.artist.image, "color": _artist.artist.color}
downloaded_img_color = self.download_artist_image(_artist.artist) image = self.download_artist_image(_artist.artist)
if downloaded_img_color: if image:
mix_image["image"] = f"{_artist.artist.artisthash}.jpg" mix_image["image"] = image
mix_image["color"] = downloaded_img_color[0]
mix = Mix( mix = Mix(
# the a prefix indicates that this is an artist mix # the a prefix indicates that this is an artist mix
@@ -321,30 +324,35 @@ class MixesPlugin(Plugin):
def download_artist_image(self, artist: Artist): def download_artist_image(self, artist: Artist):
try: try:
res = requests.get(f"{self.server}/image?artist={artist.name}") res = requests.get(
f"{self.server}/mix/image?artist={artist.name}&type=Artist"
)
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
return None return None
if res.status_code == 200: if res.status_code == 200:
# save to file filename = f"{artist.artisthash}_{int(time.time())}.webp"
with open( path = Paths.get_md_mixes_img_path() + "/" + filename
f"{Paths.get_md_mixes_img_path()}/{artist.artisthash}.jpg", "wb"
) as f:
f.write(res.content)
# resize to 256px width while maintaining aspect ratio image = Image.open(BytesIO(res.content))
img = Image.open(f"{Paths.get_md_mixes_img_path()}/{artist.artisthash}.jpg") aspect_ratio = image.width / image.height
aspect_ratio = img.width / img.height
newwidth = 256 # resize to 512px
newheight = int(256 / aspect_ratio) md_width = 512
md_height = int(md_width / aspect_ratio)
img = img.resize((newwidth, newheight), Image.LANCZOS) image = image.resize((md_width, md_height), Image.LANCZOS)
img.save(f"{Paths.get_sm_mixes_img_path()}/{artist.artisthash}.jpg") image.save(path, "webp")
return get_image_colors( # resize to 256px
f"{Paths.get_sm_mixes_img_path()}/{artist.artisthash}.jpg" sm_width = 256
) sm_height = int(sm_width / aspect_ratio)
image = image.resize((sm_width, sm_height), Image.LANCZOS)
small_path = Paths.get_sm_mixes_img_path() + "/" + filename
image.save(small_path, "webp")
return filename
return None return None
+13
View File
@@ -162,3 +162,16 @@ class ArtistStore:
return TrackStore.get_tracks_by_trackhashes(entry.trackhashes) return TrackStore.get_tracks_by_trackhashes(entry.trackhashes)
return [] return []
@classmethod
def export(cls):
path = "artists.json"
with open(path, "w") as f:
data = [
{
"name": a.name,
}
for a in cls.get_flat_list()
]
json.dump(data, f)
+2
View File
@@ -27,6 +27,7 @@ from app.plugins.register import register_plugins
from app.settings import FLASKVARS, TCOLOR, Info from app.settings import FLASKVARS, TCOLOR, Info
from app.setup import load_into_mem, run_setup from app.setup import load_into_mem, run_setup
from app.start_info_logger import log_startup_info from app.start_info_logger import log_startup_info
from app.store.artists import ArtistStore
from app.store.tracks import TrackStore from app.store.tracks import TrackStore
from app.utils.filesystem import get_home_res_path from app.utils.filesystem import get_home_res_path
from app.utils.paths import getClientFilesExtensions from app.utils.paths import getClientFilesExtensions
@@ -229,6 +230,7 @@ if __name__ == "__main__":
load_into_mem() load_into_mem()
run_swingmusic() run_swingmusic()
TrackStore.export() TrackStore.export()
ArtistStore.export()
host = FLASKVARS.get_flask_host() host = FLASKVARS.get_flask_host()
port = FLASKVARS.get_flask_port() port = FLASKVARS.get_flask_port()