From 25c20a7ef3e952805af565bfbcb42ad5cf9fd66d Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Wed, 10 Apr 2024 16:39:46 +0200 Subject: [PATCH] feat: added base functionality of artwork class --- music_kraken/objects/artwork.py | 43 +++++++++++++++++++ music_kraken/objects/source.py | 3 +- .../utils/config/config_files/main_config.py | 7 +++ music_kraken/utils/string_processing.py | 3 ++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 music_kraken/objects/artwork.py diff --git a/music_kraken/objects/artwork.py b/music_kraken/objects/artwork.py new file mode 100644 index 0000000..c7eed38 --- /dev/null +++ b/music_kraken/objects/artwork.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +from typing import List, Optional, Dict, Tuple, Type, Union, TypedDict + +from .collection import Collection +from .metadata import ( + Mapping as id3Mapping, + ID3Timestamp, + Metadata +) +from ..utils.string_processing import unify, hash_url + +from .parents import OuterProxy as Base + +from ..utils.config import main_settings + + +class ArtworkVariant(TypedDict): + url: str + width: int + height: int + deviation: float + + +class Artwork: + def __init__(self, variants: List[ArtworkVariant] = None) -> None: + self._variant_mapping: Dict[str, ArtworkVariant] = {} + + @staticmethod + def _calculate_deviation(*dimensions: List[int]) -> float: + return sum(abs(d - main_settings["preferred_artwork_resolution"]) for d in dimensions) / len(dimensions) + + def append(self, url: str, width: int, height: int) -> None: + self._variant_mapping[hash_url(url=url)] = { + "url": url, + "width": width, + "height": height, + "deviation": self._calculate_deviation(width, height), + } + + @property + def best_variant(self) -> ArtworkVariant: + return min(self._variant_mapping.values(), key=lambda x: x["deviation"]) diff --git a/music_kraken/objects/source.py b/music_kraken/objects/source.py index a60ce6d..3a1fec4 100644 --- a/music_kraken/objects/source.py +++ b/music_kraken/objects/source.py @@ -5,6 +5,7 @@ from urllib.parse import urlparse from ..utils.enums.source import SourcePages, SourceTypes from ..utils.config import youtube_settings +from ..utils.string_processing import hash_url from .metadata import Mapping, Metadata from .parents import OuterProxy @@ -88,7 +89,7 @@ class Source(OuterProxy): @property def hash_url(self) -> str: - return self.url.strip().lower().lstrip("https://").lstrip("http://") + return hash_url(self.url) @property def metadata(self) -> Metadata: diff --git a/music_kraken/utils/config/config_files/main_config.py b/music_kraken/utils/config/config_files/main_config.py index 8de212b..6a55a0a 100644 --- a/music_kraken/utils/config/config_files/main_config.py +++ b/music_kraken/utils/config/config_files/main_config.py @@ -27,6 +27,10 @@ The further you choose to be able to go back, the higher the memory usage. EmptyLine(), + Attribute(name="preferred_artwork_resolution", default_value=100), + + EmptyLine(), + Attribute(name="sort_by_date", default_value=True, description="If this is set to true, it will set the albumsort attribute such that,\nthe albums are sorted by date"), Attribute(name="sort_album_by_type", default_value=True, description="""If this is set to true, it will set the albumsort attribute such that, the albums are put into categories before being sorted. @@ -146,6 +150,9 @@ class SettingsStructure(TypedDict): language: str user_agent: str + # artwork + preferred_artwork_resolution: int + # paths music_directory: Path temp_directory: Path diff --git a/music_kraken/utils/string_processing.py b/music_kraken/utils/string_processing.py index 39963e9..f499f9c 100644 --- a/music_kraken/utils/string_processing.py +++ b/music_kraken/utils/string_processing.py @@ -96,6 +96,9 @@ def unify_punctuation(to_unify: str) -> str: to_unify = to_unify.replace(char, UNIFY_TO) return to_unify +def hash_url(url: str) -> int: + return url.strip().lower().lstrip("https://").lstrip("http://") + def remove_feature_part_from_track(title: str) -> str: if ")" != title[-1]: