This commit is contained in:
Lars Noack 2023-01-11 16:30:53 +01:00
parent 013e3dccfd
commit b6a8966503
6 changed files with 67 additions and 10 deletions

View File

@ -8,6 +8,12 @@ from music_kraken import (
Artist Artist
) )
from music_kraken.tagging import (
AudioMetadata,
write_metadata,
write_many_metadata
)
import music_kraken.database.new_database as db import music_kraken.database.new_database as db
@ -45,7 +51,7 @@ song_input = Song(
length=666, length=666,
isrc="US-S1Z-99-00001", isrc="US-S1Z-99-00001",
tracksort=2, 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=[
Lyrics(text="these are some depressive lyrics", language="en"), Lyrics(text="these are some depressive lyrics", language="en"),
Lyrics(text="test", language="en") Lyrics(text="test", language="en")
@ -91,6 +97,9 @@ print("--src--")
for source in song.sources: for source in song.sources:
print(source) print(source)
# try writing metadata
write_metadata(song)
# getting song by album ref # getting song by album ref
div() div()
song_output_list = cache.pull_songs(album_ref=album_input.reference) song_output_list = cache.pull_songs(album_ref=album_input.reference)

View File

@ -301,6 +301,7 @@ class Song(DatabaseObject):
self._sources = source_list self._sources = source_list
for source in self._sources: for source in self._sources:
source.add_song(self) source.add_song(self)
self.metadata[ID3_MAPPING.FILE_WEBPAGE_URL.value] = [s.url for s in self._sources] self.metadata[ID3_MAPPING.FILE_WEBPAGE_URL.value] = [s.url for s in self._sources]
def get_metadata(self): def get_metadata(self):

View File

@ -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

View File

@ -1,23 +1,35 @@
import mutagen import mutagen
from mutagen.id3 import ID3 from mutagen.id3 import ID3, Frame
from typing import List
import logging import logging
from ..utils.shared import (
logger = logging.Logger("hs") TAGGING_LOGGER as logger
)
from ..database import (
Song
)
class AudioMetadata: class AudioMetadata:
def __init__(self, file_location: str = None) -> None: def __init__(self, file_location: str = None) -> None:
self.file_location = file_location self._file_location = None
self.frames: ID3 = ID3() self.frames: ID3 = ID3()
if self.file_location is not None: if self.file_location is not None:
# try loading the data from the given file. if it doesn't succeed the frame remains empty self.file_location = file_location
try:
self.frames.load(self.file_location)
except mutagen.MutagenError: def add_song_metadata(self, song: Song):
logger.warning(f"couldn't find any metadata at: \"{self.file_location}\"") 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): def save(self, file_location: str = None):
if file_location is not None: if file_location is not None:
@ -27,6 +39,31 @@ class AudioMetadata:
raise Exception("no file target provided to save the data to") raise Exception("no file target provided to save the data to")
self.frames.save(filething=self.file_location) 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__": if __name__ == "__main__":
print("called directly") print("called directly")

View File

@ -39,6 +39,7 @@ PATH_LOGGER = logging.getLogger("create-paths")
DOWNLOAD_LOGGER = logging.getLogger("download") DOWNLOAD_LOGGER = logging.getLogger("download")
LYRICS_LOGGER = logging.getLogger("lyrics") LYRICS_LOGGER = logging.getLogger("lyrics")
GENIUS_LOGGER = logging.getLogger("genius") GENIUS_LOGGER = logging.getLogger("genius")
TAGGING_LOGGER = logging.getLogger("tagging")
NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea" NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea"
MUSIC_DIR = os.path.join(os.path.expanduser("~"), "Music") MUSIC_DIR = os.path.join(os.path.expanduser("~"), "Music")

BIN
test.db

Binary file not shown.