feat: musify ArtworkCollection simple function
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful

This commit is contained in:
2024-06-17 14:50:17 +02:00
parent dd99e60afd
commit 17c28722fb
4 changed files with 52 additions and 56 deletions

View File

@@ -16,6 +16,7 @@ from .metadata import Mapping as id3Mapping
from .metadata import Metadata
from .parents import OuterProxy as Base
from .target import Target
from PIL import Image
artwork_connection: Connection = Connection(module="artwork")
@@ -24,7 +25,7 @@ artwork_connection: Connection = Connection(module="artwork")
class ArtworkVariant:
url: str
width: Optional[int] = None
height: Optional[int] = None
heigth: Optional[int] = None
image_format: Optional[str] = None
def __hash__(self) -> int:
@@ -51,7 +52,7 @@ class ArtworkVariant:
def fetch(self) -> None:
global artwork_connection
r = artwork_connection.get(self.url, name=hash_url(url))
r = artwork_connection.get(self.url, name=hash_url(self.url))
if r is None:
return
@@ -167,19 +168,52 @@ class ArtworkCollection:
for value in values:
self.append(value, **kwargs)
def compile(self) -> None:
def compile(self, target: Target, **kwargs) -> None:
"""
This will make the artworks ready for download
"""
for artwork in self._data:
artwork.fetch()
for artwork_variant in artwork.variants:
r = artwork_connection.get(
url=artwork_variant.url,
name=artwork_variant.url,
)
temp_target: Target = Target.temp()
with temp_target.open("wb") as f:
f.write(r.content)
converted_target: Target = Target.temp(file_extension=main_settings["image_format"])
with Image.open(temp_target.file_path) as img:
# crop the image if it isn't square in the middle with minimum data loss
width, height = img.size
if width != height:
if width > height:
img = img.crop((width // 2 - height // 2, 0, width // 2 + height // 2, height))
else:
img = img.crop((0, height // 2 - width // 2, width, height // 2 + width // 2))
# resize the image to the preferred resolution
img.thumbnail((main_settings["preferred_artwork_resolution"], main_settings["preferred_artwork_resolution"]))
# https://stackoverflow.com/a/59476938/16804841
if img.mode != 'RGB':
img = img.convert('RGB')
if target is not None:
img.save(target.file_path, main_settings["image_format"])
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
for other_artwork in other._data:
for other_variant in other_artwork.variants:
if len(self._data) != 0:
for artwork in self._data:
for variant in artwork.variants:
variant.__merge__(other_variant)
else:
self.add_data(other_variant.url)
def __hash__(self) -> int:
return id(self)

View File

@@ -185,7 +185,7 @@ class Song(Base):
return
def _compile(self):
self.artwork.compile()
self.artwork.compile(self.target_collection.get(0))
INDEX_DEPENDS_ON = ("title", "isrc", "source_collection")