From 5067b84f483f838543332c18c41befac213d072d Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Thu, 30 Mar 2023 16:50:27 +0200 Subject: [PATCH] changed the way stuff is downloaded --- src/music_kraken/objects/__init__.py | 1 + src/music_kraken/pages/abstract.py | 18 +++++++++++------- src/music_kraken/pages/musify.py | 22 +++++++++++++++++++--- src/music_kraken/tagging/__init__.py | 13 +++++-------- src/music_kraken/tagging/id3.py | 19 +++++++++++++++---- 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/music_kraken/objects/__init__.py b/src/music_kraken/objects/__init__.py index fa7ba07..9a229db 100644 --- a/src/music_kraken/objects/__init__.py +++ b/src/music_kraken/objects/__init__.py @@ -11,6 +11,7 @@ from . import ( DatabaseObject = parents.DatabaseObject +Metadata = metadata.Metadata ID3Mapping = metadata.Mapping ID3Timestamp = metadata.ID3Timestamp diff --git a/src/music_kraken/pages/abstract.py b/src/music_kraken/pages/abstract.py index 2eb08b4..c0f74b1 100644 --- a/src/music_kraken/pages/abstract.py +++ b/src/music_kraken/pages/abstract.py @@ -17,7 +17,7 @@ from ..objects import ( Collection, Label ) -from ..tagging import write_metadata +from ..tagging import write_metadata_to_target LOGGER = logging.getLogger("this shouldn't be used") @@ -330,11 +330,16 @@ class Page: if len(sources) == 0: return - cls._download_song_to_targets(source=sources[0], target_list=song.target_collection.shallow_list) + temp_target = cls._download_song_to_targets(source=sources[0], target_list=song.target_collection.shallow_list) + cls._post_process_targets(song, temp_target) @classmethod - def _post_process_targets(cls, song: Song): - write_metadata(song) + def _post_process_targets(cls, song: Song, temp_target: Target): + write_metadata_to_target(song.metadata, temp_target) + + target: Target + for target in song.target_collection: + temp_target.copy_content(target) @classmethod @@ -358,6 +363,5 @@ class Page: return None @classmethod - def _download_song_to_targets(cls, source: Source, target_list: List[Target]): - for target in target_list: - print(f"downloading {source} to {target}") + def _download_song_to_targets(cls, source: Source) -> Target: + return Target() diff --git a/src/music_kraken/pages/musify.py b/src/music_kraken/pages/musify.py index fb3f74b..1a25629 100644 --- a/src/music_kraken/pages/musify.py +++ b/src/music_kraken/pages/musify.py @@ -6,9 +6,12 @@ import pycountry from urllib.parse import urlparse from enum import Enum from dataclasses import dataclass +from pathlib import Path +import random from ..utils.shared import ( - ENCYCLOPAEDIA_METALLUM_LOGGER as LOGGER + ENCYCLOPAEDIA_METALLUM_LOGGER as LOGGER, + TEMP_FOLDER ) from .abstract import Page @@ -894,8 +897,21 @@ class Musify(Page): return None @classmethod - def _download_song_to_targets(cls, source: Source, target_list: List[Target]): + def _download_song_to_targets(cls, source: Source) -> Path: """ https://musify.club/track/im-in-a-coffin-life-never-was-waste-of-skin-16360302 + https://musify.club/track/dl/16360302/im-in-a-coffin-life-never-was-waste-of-skin.mp3 """ - pass + url: MusifyUrl = cls.parse_url(source.url) + if url.source_type != MusifyTypes.SONG: + return + + target: Target = Target( + path=TEMP_FOLDER, + file=str(random.randint(0, 999999)) + ) + + endpoint = f"https://musify.club/track/dl/{url.musify_id}/{url.name_without_id}.mp3" + print(endpoint) + + return target diff --git a/src/music_kraken/tagging/__init__.py b/src/music_kraken/tagging/__init__.py index 2a2c527..bec0412 100644 --- a/src/music_kraken/tagging/__init__.py +++ b/src/music_kraken/tagging/__init__.py @@ -1,9 +1,6 @@ -from .id3 import ( - AudioMetadata, - write_many_metadata, - write_metadata -) +from . import id3 -AudioMetadata = AudioMetadata -write_many_metadata = write_many_metadata -write_metadata = write_metadata +AudioMetadata = id3.AudioMetadata +write_many_metadata = id3.write_many_metadata +write_metadata = id3.write_metadata +write_metadata_to_target = id3.write_metadata_to_target diff --git a/src/music_kraken/tagging/id3.py b/src/music_kraken/tagging/id3.py index 9266859..20d02e1 100644 --- a/src/music_kraken/tagging/id3.py +++ b/src/music_kraken/tagging/id3.py @@ -7,7 +7,7 @@ import logging from ..utils.shared import ( TAGGING_LOGGER as logger ) -from ..objects import Song, Target +from ..objects import Song, Target, Metadata class AudioMetadata: @@ -18,14 +18,18 @@ class AudioMetadata: if file_location is not None: self.file_location = file_location - - def add_song_metadata(self, song: Song): - for value in song.metadata: + + def add_metadata(self, metadata: Metadata): + for value in metadata: """ https://www.programcreek.com/python/example/84797/mutagen.id3.ID3 """ self.frames.add(value) + def add_song_metadata(self, song: Song): + self.add_metadata(song.metadata) + + def save(self, file_location: str = None): print("saving") print(self.frames.pprint()) @@ -47,6 +51,13 @@ class AudioMetadata: file_location = property(fget=lambda self: self._file_location, fset=set_file_location) +def write_metadata_to_target(metadata: Metadata, target: Target): + if not target.exists: + return + + id3_object = AudioMetadata(file_location=target.file_path) + id3_object.add_metadata(metadata) + id3_object.save() def write_metadata(song: Song, ignore_file_not_found: bool = True): target: Target