music-kraken-core/music_kraken/objects/artwork.py

63 lines
2.0 KiB
Python
Raw Permalink Normal View History

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
)
2024-04-26 14:04:48 +00:00
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:
2024-04-10 15:06:29 +00:00
def __init__(self, *variants: List[ArtworkVariant]) -> None:
self._variant_mapping: Dict[str, ArtworkVariant] = {}
2024-04-10 15:06:29 +00:00
for variant in variants:
self.append(**variant)
@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 = main_settings["preferred_artwork_resolution"], height: int = main_settings["preferred_artwork_resolution"], **kwargs) -> None:
if url is None:
return
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:
2024-04-10 16:53:36 +00:00
if len(self._variant_mapping.keys()) <= 0:
2024-04-10 16:18:52 +00:00
return None
return min(self._variant_mapping.values(), key=lambda x: x["deviation"])
2024-04-10 14:47:38 +00:00
2024-04-26 14:04:48 +00:00
def get_variant_name(self, variant: ArtworkVariant) -> str:
return f"artwork_{variant['width']}x{variant['height']}_{hash_url(variant['url']).replace('/', '_')}"
2024-04-26 13:56:13 +00:00
2024-04-29 20:37:07 +00:00
def __merge__(self, other: Artwork, **kwargs) -> None:
2024-04-10 14:47:38 +00:00
for key, value in other._variant_mapping.items():
2024-04-29 20:37:07 +00:00
if key not in self._variant_mapping:
2024-04-10 14:47:38 +00:00
self._variant_mapping[key] = value
def __eq__(self, other: Artwork) -> bool:
return any(a == b for a, b in zip(self._variant_mapping.keys(), other._variant_mapping.keys()))