feat: added base functionality of artwork class
This commit is contained in:
43
music_kraken/objects/artwork.py
Normal file
43
music_kraken/objects/artwork.py
Normal file
@@ -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"])
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user