From a23b6200eb8ef6fee1d733001df3213275082833 Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Thu, 30 Jun 2022 14:01:48 +0300 Subject: [PATCH] add colors to album page header - add colors attribute to the album class - render color gradient in the album page --- server/app/db/mongodb/albums.py | 10 ++++++ server/app/functions.py | 20 ++++++----- server/app/lib/colorlib.py | 53 ++++++++++++++--------------- server/app/models.py | 8 +++++ src/components/AlbumView/Header.vue | 14 ++++++-- src/interfaces.ts | 6 ++-- src/views/AlbumView.vue | 1 - 7 files changed, 70 insertions(+), 42 deletions(-) diff --git a/server/app/db/mongodb/albums.py b/server/app/db/mongodb/albums.py index 030c900c..4999c146 100644 --- a/server/app/db/mongodb/albums.py +++ b/server/app/db/mongodb/albums.py @@ -2,6 +2,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 @@ -59,3 +60,12 @@ class Albums(MongoAlbums): """ album = self.collection.find_one({"hash": hash}) return convert_one(album) + + def set_album_colors(self, colors: List[str], album_id: str) -> None: + """ + Sets the colors for an album. + """ + self.collection.update_one( + {"_id": ObjectId(album_id)}, + {"$set": {"colors": colors}}, + ) diff --git a/server/app/functions.py b/server/app/functions.py index e62128a9..4bccdd64 100644 --- a/server/app/functions.py +++ b/server/app/functions.py @@ -15,6 +15,7 @@ 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 @helpers.background @@ -23,17 +24,20 @@ def run_checks(): Checks for new songs every 5 minutes. """ - # while True: - trackslib.validate_tracks() + while True: + trackslib.validate_tracks() - Populate() - CreateAlbums() + Populate() + CreateAlbums() - if helpers.Ping()(): - CheckArtistImages()() + if helpers.Ping()(): + CheckArtistImages()() - ValidateAlbumThumbs() - ValidatePlaylistThumbs() + ValidateAlbumThumbs() + ValidatePlaylistThumbs() + ProcessAlbumColors() + + time.sleep(300) @helpers.background diff --git a/server/app/lib/colorlib.py b/server/app/lib/colorlib.py index 5882461f..e410d870 100644 --- a/server/app/lib/colorlib.py +++ b/server/app/lib/colorlib.py @@ -1,17 +1,19 @@ -from io import BytesIO - import colorgram -from app import api from app import instances -from app.lib.taglib import return_album_art -from PIL import Image -from progress.bar import Bar -def get_image_colors(image) -> list: +from app.helpers import Get +from app import settings +from app.models import Album +from app.logger import get_logger + +log = get_logger() + + +def get_image_colors(image: str) -> list: """Extracts 2 of the most dominant colors from an image.""" try: - colors = sorted(colorgram.extract(image, 2), key=lambda c: c.hsl.h) + colors = sorted(colorgram.extract(image, 4), key=lambda c: c.hsl.h) except OSError: return [] @@ -24,30 +26,25 @@ def get_image_colors(image) -> list: return formatted_colors -def save_track_colors(img, filepath) -> None: - """Saves the track colors to the database""" +class ProcessAlbumColors: + def __init__(self) -> None: + log.info("Processing album colors") + all_albums = Get.get_all_albums() - track_colors = get_image_colors(img) + all_albums = [a for a in all_albums if len(a.colors) == 0] - tc_dict = { - "filepath": filepath, - "colors": track_colors, - } + for a in all_albums: + self.process_color(a) - instances.track_color_instance.insert_track_color(tc_dict) + log.info("Processing album colors ... ✅") + @staticmethod + def process_color(album: Album): + img = settings.THUMBS_PATH + "/" + album.image -def save_t_colors(): - _bar = Bar("Processing image colors", max=len(api.DB_TRACKS)) + colors = get_image_colors(img) - for track in api.DB_TRACKS: - filepath = track["filepath"] - album_art = return_album_art(filepath) + if len(colors) > 0: + instances.album_instance.set_album_colors(colors, album.albumid) - if album_art is not None: - img = Image.open(BytesIO(album_art)) - save_track_colors(img, filepath) - - _bar.next() - - _bar.finish() + return colors diff --git a/server/app/models.py b/server/app/models.py index 915e225c..3cb4d42b 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -82,6 +82,7 @@ class Album: Creates an album object """ + albumid: str title: str artist: str hash: str @@ -92,14 +93,21 @@ class Album: is_soundtrack: bool = False is_compilation: bool = False is_single: bool = False + colors: List[str] = field(default_factory=list) def __init__(self, tags): + self.albumid = tags["_id"]["$oid"] self.title = tags["title"] self.artist = tags["artist"] self.date = tags["date"] self.image = tags["image"] self.hash = tags["hash"] + try: + self.colors = tags["colors"] + except KeyError: + self.colors = [] + @property def is_soundtrack(self) -> bool: keywords = ["motion picture", "soundtrack"] diff --git a/src/components/AlbumView/Header.vue b/src/components/AlbumView/Header.vue index 5c41c3e0..7a016d2d 100644 --- a/src/components/AlbumView/Header.vue +++ b/src/components/AlbumView/Header.vue @@ -1,6 +1,13 @@