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:
Luna 2024-06-17 14:50:17 +02:00
parent dd99e60afd
commit 17c28722fb
4 changed files with 52 additions and 56 deletions

View File

@ -67,13 +67,14 @@ def write_metadata_to_target(metadata: Metadata, target: Target, song: Song):
id3_object = AudioMetadata(file_location=target.file_path)
LOGGER.info(str(metadata))
if song.artwork.best_variant is not None:
best_variant = song.artwork.best_variant
## REWRITE COMPLETLY !!!!!!!!!!!!
if len(song.artwork._data) != 0:
variants = song.artwork._data.__getitem__(0)
best_variant = variants.variants.__getitem__(0)
r = artwork_connection.get(
url=best_variant["url"],
name=song.artwork.get_variant_name(best_variant),
url=best_variant.url,
name=best_variant.url,
)
temp_target: Target = Target.temp()

View File

@ -36,8 +36,6 @@ from ..connection import Connection
from ..pages import Page, EncyclopaediaMetallum, Musify, YouTube, YoutubeMusic, Bandcamp, Genius, INDEPENDENT_DB_OBJECTS
artwork_connection: Connection = Connection()
ALL_PAGES: Set[Type[Page]] = {
# EncyclopaediaMetallum,
Genius,
@ -167,43 +165,6 @@ class Pages:
return False
def download_artwork_variant_to_target(self, artwork_variant: ArtworkVariant, target: Target):
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')
img.save(target.file_path, main_settings["image_format"])
def remove_artwork_duplicates(self) -> None:
"""
This will eliminate duplicates within the given threshold
"""
pass
def _fetch_artist_artwork(self, artist: Artist, naming: dict):
naming: Dict[str, List[str]] = defaultdict(list, naming)
@ -214,17 +175,17 @@ class Pages:
# https://stackoverflow.com/a/17016257
naming[key] = list(dict.fromkeys(value))
artwork: Artwork = artist.artwork
artwork: ArtworkCollection = artist.artwork
for image_number, variant in enumerate(artwork):
naming["image_number"] = [str(image_number)]
url: str = variant["url"]
url: str = variant.url
target = Target(
relative_to_music_dir=True,
file_path=Path(self._parse_path_template(main_settings["artist_artwork_path"], naming=naming))
)
self.download_artwork_variant_to_target(variant, target)
artwork.compile(target)
def download(self, data_object: DataObject, genre: str, **kwargs) -> DownloadResult:
# fetch the given object

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")