From 714775a67e3c142133e50d72acef14fc420e539f Mon Sep 17 00:00:00 2001 From: "restyled-io[bot]" <32688539+restyled-io[bot]@users.noreply.github.com> Date: Sat, 2 Jul 2022 09:06:14 +0300 Subject: [PATCH] Restyle New album page design (#76) --- server/app/api/album.py | 12 ++++-------- server/app/api/search.py | 12 ++++++------ server/app/db/mongodb/albums.py | 14 +++++++++++--- server/app/functions.py | 30 +++++++++++++++--------------- server/app/helpers.py | 11 +++++++---- server/app/lib/colorlib.py | 7 +++---- server/app/lib/populate.py | 18 +++++++++++------- server/app/lib/searchlib.py | 4 ++++ server/app/logger.py | 1 + server/app/models.py | 8 ++++---- server/app/settings.py | 4 +--- 11 files changed, 67 insertions(+), 54 deletions(-) diff --git a/server/app/api/album.py b/server/app/api/album.py index 1a616a70..78891124 100644 --- a/server/app/api/album.py +++ b/server/app/api/album.py @@ -6,12 +6,12 @@ from typing import List from app import api from app import helpers +from app import instances from app import models +from app.functions import FetchAlbumBio from app.lib import albumslib from flask import Blueprint from flask import request -from app.functions import FetchAlbumBio -from app import instances album_bp = Blueprint("album", __name__, url_prefix="") @@ -64,12 +64,8 @@ def get_album(): except AttributeError: album.duration = 0 - if ( - album.count == 1 - and tracks[0].title == album.title - and tracks[0].tracknumber == 1 - and tracks[0].disknumber == 1 - ): + if (album.count == 1 and tracks[0].title == album.title + and tracks[0].tracknumber == 1 and tracks[0].disknumber == 1): album.is_single = True return {"tracks": tracks, "info": album} diff --git a/server/app/api/search.py b/server/app/api/search.py index b2bd82b4..b745a166 100644 --- a/server/app/api/search.py +++ b/server/app/api/search.py @@ -3,14 +3,14 @@ Contains all the search routes. """ from pprint import pprint from typing import List + from app import helpers +from app import models +from app import serializer from app.lib import searchlib from flask import Blueprint from flask import request -from app import models -from app import serializer - search_bp = Blueprint("search", __name__, url_prefix="/") SEARCH_RESULTS = { @@ -197,20 +197,20 @@ def search_load_more(): if type == "tracks": t = SearchResults.tracks return { - "tracks": t[index : index + 5], + "tracks": t[index:index + 5], "more": len(t) > index + 5, } elif type == "albums": a = SearchResults.albums return { - "albums": a[index : index + 6], + "albums": a[index:index + 6], "more": len(a) > index + 6, } elif type == "artists": a = SearchResults.artists return { - "artists": a[index : index + 6], + "artists": a[index:index + 6], "more": len(a) > index + 6, } diff --git a/server/app/db/mongodb/albums.py b/server/app/db/mongodb/albums.py index eeceb6d0..7e220760 100644 --- a/server/app/db/mongodb/albums.py +++ b/server/app/db/mongodb/albums.py @@ -3,6 +3,7 @@ This file contains the Album class for interacting with album documents in MongoDB. """ from typing import List + from app.db.mongodb import convert_many from app.db.mongodb import convert_one from app.db.mongodb import MongoAlbums @@ -21,8 +22,13 @@ class Albums(MongoAlbums): """ album = album.__dict__ return self.collection.update_one( - {"album": album["title"], "artist": album["artist"]}, - {"$set": album}, + { + "album": album["title"], + "artist": album["artist"] + }, + { + "$set": album + }, upsert=True, ).upserted_id @@ -67,5 +73,7 @@ class Albums(MongoAlbums): """ self.collection.update_one( {"hash": hash}, - {"$set": {"colors": colors}}, + {"$set": { + "colors": colors + }}, ) diff --git a/server/app/functions.py b/server/app/functions.py index 3e8ab7e8..fb4eebe9 100644 --- a/server/app/functions.py +++ b/server/app/functions.py @@ -7,19 +7,21 @@ from concurrent.futures import ThreadPoolExecutor from io import BytesIO import requests -from PIL import Image - -from app import helpers, settings +from app import helpers +from app import settings +from app.lib import trackslib from app.lib import watchdoge from app.lib.albumslib import ValidateAlbumThumbs -from app.lib import trackslib -from app.lib.populate import CreateAlbums, Populate -from app.lib.playlistlib import ValidatePlaylistThumbs from app.lib.colorlib import ProcessAlbumColors +from app.lib.playlistlib import ValidatePlaylistThumbs +from app.lib.populate import CreateAlbums +from app.lib.populate import Populate from app.logger import get_logger +from PIL import Image log = get_logger() + @helpers.background def run_checks(): """ @@ -77,6 +79,7 @@ class getArtistImage: class useImageDownloader: + def __init__(self, url: str, dest: str) -> None: self.url = url self.dest = dest @@ -93,6 +96,7 @@ class useImageDownloader: class CheckArtistImages: + def __init__(self): self.artists: list[str] = [] print("Checking for artist images") @@ -117,12 +121,8 @@ class CheckArtistImages: :param artistname: The artist name """ - img_path = ( - settings.APP_DIR - + "/images/artists/" - + helpers.create_safe_name(artistname) - + ".webp" - ) + img_path = (settings.APP_DIR + "/images/artists/" + + helpers.create_safe_name(artistname) + ".webp") if cls.check_if_exists(img_path): return "exists" @@ -149,8 +149,7 @@ def fetch_album_bio(title: str, albumartist: str) -> str | None: Returns the album bio for a given album. """ last_fm_url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key={}&artist={}&album={}&format=json".format( - settings.LAST_FM_API_KEY, albumartist, title - ) + settings.LAST_FM_API_KEY, albumartist, title) try: response = requests.get(last_fm_url) @@ -159,7 +158,8 @@ def fetch_album_bio(title: str, albumartist: str) -> str | None: return None try: - bio = data["album"]["wiki"]["summary"].split(' None: + def __init__(self, source: List, search_from: str, + queries: List[str]) -> None: self.source_list = source self.queries_list = queries self.attr = search_from @@ -132,6 +134,7 @@ class UseBisection: class Get: + @staticmethod def get_all_tracks() -> List[models.Track]: """ diff --git a/server/app/lib/colorlib.py b/server/app/lib/colorlib.py index 38f8e481..5936759b 100644 --- a/server/app/lib/colorlib.py +++ b/server/app/lib/colorlib.py @@ -1,11 +1,9 @@ import colorgram from app import instances - - -from app.helpers import Get from app import settings -from app.models import Album +from app.helpers import Get from app.logger import get_logger +from app.models import Album log = get_logger() @@ -27,6 +25,7 @@ def get_image_colors(image: str) -> list: class ProcessAlbumColors: + def __init__(self) -> None: log.info("Processing album colors") all_albums = Get.get_all_albums() diff --git a/server/app/lib/populate.py b/server/app/lib/populate.py index 81c8774f..c6ee9400 100644 --- a/server/app/lib/populate.py +++ b/server/app/lib/populate.py @@ -1,20 +1,22 @@ -from dataclasses import dataclass import time from concurrent.futures import ThreadPoolExecutor +from dataclasses import dataclass from typing import List +from app import instances from app import settings -from app.logger import logg -from app.helpers import Get, UseBisection, create_album_hash +from app.helpers import create_album_hash +from app.helpers import Get from app.helpers import run_fast_scandir +from app.helpers import UseBisection from app.instances import tracks_instance from app.lib.albumslib import create_album from app.lib.taglib import get_tags -from app.models import Album, Track +from app.logger import logg +from app.models import Album +from app.models import Track from tqdm import tqdm -from app import instances - class Populate: """ @@ -76,6 +78,7 @@ class PreAlbum: class CreateAlbums: + def __init__(self) -> None: self.db_tracks = Get.get_all_tracks() self.db_albums = Get.get_all_albums() @@ -119,7 +122,8 @@ class CreateAlbums: return prealbums @staticmethod - def filter_processed(albums: List[Album], prealbums: List[PreAlbum]) -> List[dict]: + def filter_processed(albums: List[Album], + prealbums: List[PreAlbum]) -> List[dict]: to_process = [] for p in tqdm(prealbums, desc="Filtering processed albums"): diff --git a/server/app/lib/searchlib.py b/server/app/lib/searchlib.py index e3cb77dc..adf77b1e 100644 --- a/server/app/lib/searchlib.py +++ b/server/app/lib/searchlib.py @@ -37,6 +37,7 @@ class Limit: class SearchTracks: + def __init__(self, tracks: List[models.Track], query: str) -> None: self.query = query self.tracks = tracks @@ -59,6 +60,7 @@ class SearchTracks: class SearchArtists: + def __init__(self, artists: set[str], query: str) -> None: self.query = query self.artists = artists @@ -88,6 +90,7 @@ class SearchArtists: class SearchAlbums: + def __init__(self, albums: List[models.Album], query: str) -> None: self.query = query self.albums = albums @@ -118,6 +121,7 @@ class SearchAlbums: class SearchPlaylists: + def __init__(self, playlists: List[models.Playlist], query: str) -> None: self.playlists = playlists self.query = query diff --git a/server/app/logger.py b/server/app/logger.py index ae6dffb3..cd030110 100644 --- a/server/app/logger.py +++ b/server/app/logger.py @@ -1,5 +1,6 @@ import logging + class CustomFormatter(logging.Formatter): grey = "\x1b[38;20m" diff --git a/server/app/models.py b/server/app/models.py index 4f406ee1..974e4b36 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -1,8 +1,9 @@ """ Contains all the models for objects generation and typing. """ -from dataclasses import dataclass, field import random +from dataclasses import dataclass +from dataclasses import field from typing import List from app import helpers @@ -48,9 +49,8 @@ class Track: self.image = tags["albumhash"] + ".webp" self.tracknumber = int(tags["tracknumber"]) - self.uniq_hash = self.create_unique_hash( - "".join(self.artists), self.album, self.title - ) + self.uniq_hash = self.create_unique_hash("".join(self.artists), + self.album, self.title) @staticmethod def create_unique_hash(*args): diff --git a/server/app/settings.py b/server/app/settings.py index bfdad247..740f2c9a 100644 --- a/server/app/settings.py +++ b/server/app/settings.py @@ -1,9 +1,8 @@ """ Contains default configs """ -import os import multiprocessing - +import os # paths CONFIG_FOLDER = ".alice" @@ -28,7 +27,6 @@ LAST_FM_API_KEY = "762db7a44a9e6fb5585661f5f2bdf23a" CPU_COUNT = multiprocessing.cpu_count() - THUMB_SIZE: int = 400 """ The size of extracted in pixels