diff --git a/src/goof.py b/src/goof.py index 4313320..0e51dad 100644 --- a/src/goof.py +++ b/src/goof.py @@ -8,6 +8,12 @@ from music_kraken import ( Artist ) +from music_kraken.tagging import ( + AudioMetadata, + write_metadata, + write_many_metadata +) + import music_kraken.database.new_database as db @@ -45,7 +51,7 @@ song_input = Song( length=666, isrc="US-S1Z-99-00001", tracksort=2, - target=Target(file="~/Music/genre/artist/album/song.mp3", path="~/Music/genre/artist/album"), + target=Target(file="~/Music/test/Linkin Park/Hybrid Theory/Cure for the Itch.mp3", path="~/Music/test/Linkin Park/Hybrid Theory/"), lyrics=[ Lyrics(text="these are some depressive lyrics", language="en"), Lyrics(text="test", language="en") @@ -91,6 +97,9 @@ print("--src--") for source in song.sources: print(source) +# try writing metadata +write_metadata(song) + # getting song by album ref div() song_output_list = cache.pull_songs(album_ref=album_input.reference) diff --git a/src/music_kraken/database/objects/song.py b/src/music_kraken/database/objects/song.py index 677accb..3ca1cdc 100644 --- a/src/music_kraken/database/objects/song.py +++ b/src/music_kraken/database/objects/song.py @@ -301,6 +301,7 @@ class Song(DatabaseObject): self._sources = source_list for source in self._sources: source.add_song(self) + self.metadata[ID3_MAPPING.FILE_WEBPAGE_URL.value] = [s.url for s in self._sources] def get_metadata(self): diff --git a/src/music_kraken/tagging/__init__.py b/src/music_kraken/tagging/__init__.py index e69de29..2a2c527 100644 --- a/src/music_kraken/tagging/__init__.py +++ b/src/music_kraken/tagging/__init__.py @@ -0,0 +1,9 @@ +from .id3 import ( + AudioMetadata, + write_many_metadata, + write_metadata +) + +AudioMetadata = AudioMetadata +write_many_metadata = write_many_metadata +write_metadata = write_metadata diff --git a/src/music_kraken/tagging/id3.py b/src/music_kraken/tagging/id3.py index a7044ae..137ac21 100644 --- a/src/music_kraken/tagging/id3.py +++ b/src/music_kraken/tagging/id3.py @@ -1,23 +1,35 @@ import mutagen -from mutagen.id3 import ID3 +from mutagen.id3 import ID3, Frame +from typing import List import logging - -logger = logging.Logger("hs") +from ..utils.shared import ( + TAGGING_LOGGER as logger +) +from ..database import ( + Song +) class AudioMetadata: def __init__(self, file_location: str = None) -> None: - self.file_location = file_location + self._file_location = None self.frames: ID3 = ID3() + if self.file_location is not None: - # try loading the data from the given file. if it doesn't succeed the frame remains empty - try: - self.frames.load(self.file_location) - except mutagen.MutagenError: - logger.warning(f"couldn't find any metadata at: \"{self.file_location}\"") + self.file_location = file_location + + + def add_song_metadata(self, song: Song): + print("adding") + for key, value in song.metadata: + """ + TODO: + Implement the adding to the frame thingie of the metadata + """ + print(key, value) def save(self, file_location: str = None): if file_location is not None: @@ -27,6 +39,31 @@ class AudioMetadata: raise Exception("no file target provided to save the data to") self.frames.save(filething=self.file_location) + def set_file_location(self, file_location): + # try loading the data from the given file. if it doesn't succeed the frame remains empty + try: + self.frames.load(file_location) + self._file_location = file_location + except mutagen.MutagenError: + logger.warning(f"couldn't find any metadata at: \"{self.file_location}\"") + + file_location = property(fget=lambda self: self._file_location, fset=set_file_location) + + +def write_metadata(song: Song): + if not song.target.exists_on_disc: + print("afhhkj") + return + + id3_object = AudioMetadata(file_location=song.target.file) + id3_object.add_song_metadata(song=song) + id3_object.save() + + +def write_many_metadata(song_list: List[Song]): + for song in song_list: + write_metadata(song=song) + if __name__ == "__main__": print("called directly") diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 4b5e037..fdd7fa9 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -39,6 +39,7 @@ PATH_LOGGER = logging.getLogger("create-paths") DOWNLOAD_LOGGER = logging.getLogger("download") LYRICS_LOGGER = logging.getLogger("lyrics") GENIUS_LOGGER = logging.getLogger("genius") +TAGGING_LOGGER = logging.getLogger("tagging") NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea" MUSIC_DIR = os.path.join(os.path.expanduser("~"), "Music") diff --git a/test.db b/test.db index 232709a..856e732 100644 Binary files a/test.db and b/test.db differ