2024-04-10 14:39:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-06-06 15:53:17 +00:00
|
|
|
from copy import copy
|
|
|
|
from dataclasses import dataclass, field
|
2024-06-11 12:54:36 +00:00
|
|
|
from functools import cached_property
|
2024-06-04 08:09:17 +00:00
|
|
|
from typing import Dict, List, Optional, Set, Tuple, Type, TypedDict, Union
|
2024-04-10 14:39:46 +00:00
|
|
|
|
2024-06-11 12:54:36 +00:00
|
|
|
from ..connection import Connection
|
2024-06-06 15:53:17 +00:00
|
|
|
from ..utils import create_dataclass_instance, custom_hash
|
2024-06-04 08:09:17 +00:00
|
|
|
from ..utils.config import main_settings
|
2024-06-06 15:53:17 +00:00
|
|
|
from ..utils.enums import PictureType
|
2024-06-04 08:09:17 +00:00
|
|
|
from ..utils.string_processing import hash_url, unify
|
2024-04-10 14:39:46 +00:00
|
|
|
from .collection import Collection
|
2024-06-04 08:09:17 +00:00
|
|
|
from .metadata import ID3Timestamp
|
|
|
|
from .metadata import Mapping as id3Mapping
|
|
|
|
from .metadata import Metadata
|
2024-04-10 14:39:46 +00:00
|
|
|
from .parents import OuterProxy as Base
|
2024-06-11 12:54:36 +00:00
|
|
|
from .target import Target
|
|
|
|
|
|
|
|
artwork_connection: Connection = Connection(module="artwork")
|
2024-04-10 14:39:46 +00:00
|
|
|
|
2024-06-05 10:05:38 +00:00
|
|
|
|
2024-06-06 15:53:17 +00:00
|
|
|
@dataclass
|
|
|
|
class ArtworkVariant:
|
2024-04-10 14:39:46 +00:00
|
|
|
url: str
|
2024-06-06 15:53:17 +00:00
|
|
|
width: Optional[int] = None
|
|
|
|
height: Optional[int] = None
|
2024-06-11 12:34:58 +00:00
|
|
|
image_format: Optional[str] = None
|
2024-06-06 15:53:17 +00:00
|
|
|
|
|
|
|
def __hash__(self) -> int:
|
|
|
|
return custom_hash(self.url)
|
|
|
|
|
|
|
|
def __eq__(self, other: ArtworkVariant) -> bool:
|
|
|
|
return hash(self) == hash(other)
|
|
|
|
|
|
|
|
def __contains__(self, other: str) -> bool:
|
|
|
|
return custom_hash(other) == hash(self.url)
|
|
|
|
|
2024-06-11 12:34:58 +00:00
|
|
|
def __merge__(self, other: ArtworkVariant) -> None:
|
|
|
|
for key, value in other.__dict__.items():
|
|
|
|
if value is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if getattr(self, key) is None:
|
|
|
|
setattr(self, key, value)
|
2024-06-06 15:53:17 +00:00
|
|
|
|
2024-06-11 12:54:36 +00:00
|
|
|
@cached_property
|
|
|
|
def target(self) -> Target:
|
|
|
|
return Target.temp()
|
|
|
|
|
|
|
|
def fetch(self) -> None:
|
|
|
|
global artwork_connection
|
|
|
|
|
|
|
|
r = artwork_connection.get(self.url, name=hash_url(url))
|
|
|
|
if r is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.target.raw_content = r.content
|
|
|
|
|
2024-06-06 15:53:17 +00:00
|
|
|
@dataclass
|
|
|
|
class Artwork:
|
|
|
|
variants: List[ArtworkVariant] = field(default_factory=list)
|
|
|
|
|
|
|
|
artwork_type: PictureType = PictureType.OTHER
|
|
|
|
|
|
|
|
def search_variant(self, url: str) -> Optional[ArtworkVariant]:
|
|
|
|
if url is None:
|
|
|
|
return None
|
2024-06-07 09:15:23 +00:00
|
|
|
|
2024-06-06 15:53:17 +00:00
|
|
|
for variant in self.variants:
|
|
|
|
if url in variant:
|
|
|
|
return variant
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __contains__(self, other: str) -> bool:
|
|
|
|
return self.search_variant(other) is not None
|
|
|
|
|
|
|
|
def add_data(self, **kwargs) -> None:
|
|
|
|
variant = self.search_variant(kwargs.get("url"))
|
|
|
|
|
|
|
|
if variant is None:
|
2024-06-11 12:34:58 +00:00
|
|
|
variant, kwargs = create_dataclass_instance(ArtworkVariant, kwargs)
|
2024-06-06 15:53:17 +00:00
|
|
|
self.variants.append(variant)
|
|
|
|
|
|
|
|
variant.__dict__.update(kwargs)
|
2024-04-10 14:39:46 +00:00
|
|
|
|
2024-06-07 09:15:23 +00:00
|
|
|
@property
|
|
|
|
def url(self) -> Optional[str]:
|
|
|
|
if len(self.variants) <= 0:
|
|
|
|
return None
|
|
|
|
return self.variants[0].url
|
|
|
|
|
2024-06-11 12:54:36 +00:00
|
|
|
def fetch(self) -> None:
|
|
|
|
for variant in self.variants:
|
|
|
|
variant.fetch()
|
|
|
|
|
2024-04-10 14:39:46 +00:00
|
|
|
|
2024-06-05 10:05:38 +00:00
|
|
|
class ArtworkCollection:
|
2024-06-06 15:53:17 +00:00
|
|
|
"""
|
|
|
|
Stores all the images/artworks for one data object.
|
|
|
|
|
|
|
|
There could be duplicates before calling ArtworkCollection.compile()
|
|
|
|
_this is called before one object is downloaded automatically._
|
|
|
|
"""
|
|
|
|
|
|
|
|
artwork_type: PictureType = PictureType.OTHER
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2024-06-10 10:23:12 +00:00
|
|
|
*data: List[Artwork],
|
2024-06-06 15:53:17 +00:00
|
|
|
parent_artworks: Set[ArtworkCollection] = None,
|
2024-06-10 10:23:12 +00:00
|
|
|
crop_images: bool = True,
|
2024-06-06 15:53:17 +00:00
|
|
|
) -> None:
|
|
|
|
# this is used for the song artwork, to fall back to the song artwork
|
2024-06-05 10:05:38 +00:00
|
|
|
self.parent_artworks: Set[ArtworkCollection] = parent_artworks or set()
|
2024-06-06 15:53:17 +00:00
|
|
|
self.crop_images: bool = crop_images
|
2024-06-04 08:09:17 +00:00
|
|
|
|
2024-06-06 15:53:17 +00:00
|
|
|
self._data = []
|
2024-06-07 09:17:47 +00:00
|
|
|
self.extend(data)
|
2024-06-06 15:53:17 +00:00
|
|
|
|
|
|
|
def search_artwork(self, url: str) -> Optional[ArtworkVariant]:
|
|
|
|
for artwork in self._data:
|
|
|
|
if url in artwork:
|
|
|
|
return artwork
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __contains__(self, other: str) -> bool:
|
|
|
|
return self.search_artwork(other) is not None
|
|
|
|
|
|
|
|
def _create_new_artwork(self, **kwargs) -> Tuple[Artwork, dict]:
|
|
|
|
kwargs["artwork_type"] = kwargs.get("artwork_type", self.artwork_type)
|
|
|
|
|
2024-06-11 12:34:58 +00:00
|
|
|
return create_dataclass_instance(Artwork, dict(**kwargs))
|
2024-04-10 14:39:46 +00:00
|
|
|
|
2024-06-11 12:34:58 +00:00
|
|
|
def add_data(self, url: str, **kwargs) -> Artwork:
|
2024-06-06 15:53:17 +00:00
|
|
|
kwargs["url"] = url
|
2024-04-10 15:06:29 +00:00
|
|
|
|
2024-06-06 15:53:17 +00:00
|
|
|
artwork = self.search_artwork(url)
|
2024-04-10 14:39:46 +00:00
|
|
|
|
2024-06-06 15:53:17 +00:00
|
|
|
if artwork is None:
|
2024-06-11 12:34:58 +00:00
|
|
|
artwork, kwargs = self._create_new_artwork(**kwargs)
|
2024-06-06 15:53:17 +00:00
|
|
|
self._data.append(artwork)
|
|
|
|
|
2024-06-11 12:34:58 +00:00
|
|
|
artwork.add_data(**kwargs)
|
|
|
|
return artwork
|
2024-06-06 15:53:17 +00:00
|
|
|
|
|
|
|
def append(self, value: Union[Artwork, ArtworkVariant, dict], **kwargs):
|
2024-06-10 10:23:12 +00:00
|
|
|
"""
|
|
|
|
You can append the types Artwork, ArtworkVariant or dict
|
|
|
|
the best option would be to use Artwork and avoid the other options.
|
|
|
|
"""
|
2024-06-06 15:53:17 +00:00
|
|
|
if isinstance(value, dict):
|
|
|
|
kwargs.update(value)
|
|
|
|
value, kwargs = create_dataclass_instance(ArtworkVariant, kwargs)
|
|
|
|
|
|
|
|
if isinstance(value, ArtworkVariant):
|
|
|
|
kwargs["variants"] = [value]
|
|
|
|
value, kwargs = create_dataclass_instance(Artwork, kwargs)
|
|
|
|
|
|
|
|
if isinstance(value, Artwork):
|
|
|
|
self._data.append(value)
|
2024-04-11 18:29:05 +00:00
|
|
|
return
|
2024-06-07 09:17:47 +00:00
|
|
|
|
|
|
|
def extend(self, values: List[Union[Artwork, ArtworkVariant, dict]], **kwargs):
|
|
|
|
for value in values:
|
|
|
|
self.append(value, **kwargs)
|
2024-04-10 14:39:46 +00:00
|
|
|
|
2024-06-07 09:27:55 +00:00
|
|
|
def compile(self) -> None:
|
2024-06-10 10:23:12 +00:00
|
|
|
"""
|
|
|
|
This will make the artworks ready for download
|
|
|
|
"""
|
|
|
|
for artwork in self._data:
|
2024-06-11 12:54:36 +00:00
|
|
|
artwork.fetch()
|
2024-06-07 09:27:55 +00:00
|
|
|
|
2024-06-07 09:15:23 +00:00
|
|
|
def __merge__(self, other: ArtworkCollection, **kwargs) -> None:
|
|
|
|
self.parent_artworks.update(other.parent_artworks)
|
|
|
|
|
|
|
|
for key, value in other._variant_mapping.items():
|
|
|
|
if key not in self._variant_mapping:
|
|
|
|
self._variant_mapping[key] = value
|
|
|
|
|
|
|
|
def __hash__(self) -> int:
|
|
|
|
return id(self)
|
|
|
|
|
|
|
|
def __iter__(self) -> Generator[Artwork, None, None]:
|
|
|
|
yield from self._data
|
|
|
|
|
|
|
|
def get_urls(self) -> Generator[str, None, None]:
|
|
|
|
yield from (artwork.url for artwork in self._data if artwork.url is not None)
|
|
|
|
|
|
|
|
"""
|
2024-06-04 08:09:17 +00:00
|
|
|
@property
|
|
|
|
def flat_empty(self) -> bool:
|
|
|
|
return len(self._variant_mapping.keys()) <= 0
|
|
|
|
|
|
|
|
def _get_best_from_list(self, artwork_variants: List[ArtworkVariant]) -> Optional[ArtworkVariant]:
|
|
|
|
return min(artwork_variants, key=lambda x: x["deviation"])
|
|
|
|
|
2024-04-10 14:39:46 +00:00
|
|
|
@property
|
|
|
|
def best_variant(self) -> ArtworkVariant:
|
2024-06-04 08:09:17 +00:00
|
|
|
if self.flat_empty:
|
|
|
|
return self._get_best_from_list([parent.best_variant for parent in self.parent_artworks])
|
|
|
|
return self._get_best_from_list(self._variant_mapping.values())
|
2024-04-10 14:47:38 +00:00
|
|
|
|
2024-04-26 14:04:48 +00:00
|
|
|
def get_variant_name(self, variant: ArtworkVariant) -> str:
|
2024-04-26 16:42:39 +00:00
|
|
|
return f"artwork_{variant['width']}x{variant['height']}_{hash_url(variant['url']).replace('/', '_')}"
|
2024-06-07 09:15:23 +00:00
|
|
|
"""
|
2024-06-05 06:34:37 +00:00
|
|
|
|