create albums from pre-albums instead of individual tracks

This commit is contained in:
geoffrey45
2022-04-25 11:59:36 +03:00
parent c6e3bd9f94
commit 6527b3abc5
6 changed files with 106 additions and 45 deletions
+26 -10
View File
@@ -13,6 +13,9 @@ from app import models
from app.lib import trackslib
from progress.bar import Bar
from app.lib import taglib
from app import settings
def get_all_albums() -> List[models.Album]:
"""
@@ -51,7 +54,7 @@ def create_everything() -> List[models.Track]:
api.TRACKS.sort(key=lambda x: x.title)
def find_album(albumtitle: str, artist: str) -> models.Album:
def find_album(albumtitle: str, artist: str) -> int or None:
"""
Finds an album by album title and artist.
"""
@@ -63,8 +66,7 @@ def find_album(albumtitle: str, artist: str) -> models.Album:
iter += 1
mid = (left + right) // 2
if api.ALBUMS[mid].title == albumtitle and api.ALBUMS[
mid].artist == artist:
if api.ALBUMS[mid].title == albumtitle and api.ALBUMS[mid].artist == artist:
return mid
if api.ALBUMS[mid].title < albumtitle:
@@ -96,20 +98,33 @@ def use_defaults() -> str:
return path
def gen_random_path() -> str:
"""
Generates a random image file path for an album image.
"""
choices = "abcdefghijklmnopqrstuvwxyz0123456789"
path = "".join(random.choice(choices) for i in range(20))
path += ".webp"
return path
def get_album_image(album: list) -> str:
"""
Gets the image of an album.
"""
uri = settings.IMG_THUMB_URI
for track in album:
img_p = (track["album"] + track["albumartist"] + ".webp").replace(
"/", "::")
img = functions.extract_thumb(track["filepath"], webp_path=img_p)
img_p = gen_random_path()
if img is not None:
return img
exists = taglib.extract_thumb(track["filepath"], webp_path=img_p)
return use_defaults()
if exists:
return uri + img_p
return uri + use_defaults()
def get_album_tracks(album: str, artist: str) -> List:
@@ -142,7 +157,8 @@ def create_album(track) -> models.Album:
album["date"] = album_tracks[0]["date"]
album["artistimage"] = urllib.parse.quote_plus(
album_tracks[0]["albumartist"] + ".webp")
album_tracks[0]["albumartist"] + ".webp"
)
album["image"] = get_album_image(album_tracks)
+5 -6
View File
@@ -1,5 +1,4 @@
import os
import urllib
from io import BytesIO
import mutagen
@@ -29,14 +28,14 @@ def return_album_art(filepath: str):
return None
def extract_thumb(audio_file_path: str, webp_path: str) -> str:
def extract_thumb(audio_file_path: str, webp_path: str) -> bool:
"""
Extracts the thumbnail from an audio file. Returns the path to the thumbnail.
"""
img_path = os.path.join(settings.THUMBS_PATH, webp_path)
if os.path.exists(img_path):
return urllib.parse.quote(webp_path)
return True
album_art = return_album_art(audio_file_path)
@@ -52,11 +51,11 @@ def extract_thumb(audio_file_path: str, webp_path: str) -> str:
small_img = png.resize((250, 250), Image.ANTIALIAS)
small_img.save(webp_path, format="webp")
except:
return None
return False
return urllib.parse.quote(webp_path)
return True
else:
return None
return False
def parse_artist_tag(audio):
+1 -1
View File
@@ -63,7 +63,7 @@ def add_track(filepath: str) -> None:
if folder not in api.VALID_FOLDERS:
api.VALID_FOLDERS.add(folder)
f = folderslib.create_folder(folder)
api.FOLDERS.add(f)
api.FOLDERS.append(f)
def remove_track(filepath: str) -> None: