From ede768c8b5bbce6f6490551e9a34d310a54ec659 Mon Sep 17 00:00:00 2001 From: tsi-billypom Date: Thu, 17 Apr 2025 15:51:55 -0400 Subject: [PATCH] auto --- components/MusicTable.py | 13 +++++---- utils/convert_id3_timestamp_to_datetime.py | 6 +++- utils/get_id3_tags.py | 33 ++++++++++++++-------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/components/MusicTable.py b/components/MusicTable.py index 18574c7..1e76a6a 100644 --- a/components/MusicTable.py +++ b/components/MusicTable.py @@ -49,7 +49,7 @@ from utils.delete_song_id_from_database import delete_song_id_from_database from utils.add_files_to_database import add_files_to_database from utils.get_reorganize_vars import get_reorganize_vars from utils.update_song_in_database import update_song_in_database -from utils.get_id3_tags import get_id3_tags +from utils.get_id3_tags import get_id3_tags, id3_remap from utils.get_album_art import get_album_art from utils import set_id3_tag from subprocess import Popen @@ -582,7 +582,8 @@ class MusicTable(QTableView): selected_song_filepath = self.get_selected_song_filepath() if selected_song_filepath is None: return - current_song = self.get_selected_song_metadata() + # current_song = self.get_selected_song_metadata() + current_song = get_id3_tags(selected_song_filepath)[0] try: uslt_tags = [tag for tag in current_song.keys() if tag.startswith("USLT::")] if uslt_tags: @@ -848,13 +849,13 @@ class MusicTable(QTableView): """Returns the currently playing song filepath""" return self.current_song_filepath - def get_current_song_metadata(self) -> ID3 | dict: + def get_current_song_metadata(self) -> dict: """Returns the currently playing song's ID3 tags""" - return get_id3_tags(self.current_song_filepath)[0] + return id3_remap(get_id3_tags(self.current_song_filepath)[0]) - def get_selected_song_metadata(self) -> ID3 | dict: + def get_selected_song_metadata(self) -> dict: """Returns the selected song's ID3 tags""" - return get_id3_tags(self.selected_song_filepath)[0] + return id3_remap(get_id3_tags(self.selected_song_filepath)[0]) def get_current_song_album_art(self) -> bytes: """Returns the APIC data (album art lol) for the currently playing song""" diff --git a/utils/convert_id3_timestamp_to_datetime.py b/utils/convert_id3_timestamp_to_datetime.py index d2c9864..fa24c9d 100644 --- a/utils/convert_id3_timestamp_to_datetime.py +++ b/utils/convert_id3_timestamp_to_datetime.py @@ -2,8 +2,12 @@ import datetime from mutagen.id3._specs import ID3TimeStamp -def convert_id3_timestamp_to_datetime(timestamp: ID3TimeStamp): +def convert_id3_timestamp_to_datetime(timestamp): """Turns a mutagen ID3TimeStamp into a format that SQLite can use for Date field""" + if timestamp is None: + return + if not isinstance(timestamp, ID3TimeStamp): + return if len(timestamp.text) == 4: # If only year is provided datetime_obj = datetime.datetime.strptime(timestamp.text, "%Y") else: diff --git a/utils/get_id3_tags.py b/utils/get_id3_tags.py index 7c3dbe1..7dabc03 100644 --- a/utils/get_id3_tags.py +++ b/utils/get_id3_tags.py @@ -36,18 +36,22 @@ def get_mp3_tags(filename: str) -> tuple[MP3 | ID3 | FLAC, str]: def id3_remap(audio: MP3 | ID3 | FLAC) -> dict: """ - Turns an ID3 dict into a normal dict that I the human can use. - with words... + Turns an ID3 dict into a normal dict that I, the human, can use. + Add extra fields as well """ - return { - "title": audio["TIT2"].text[0], - "artist": audio["TPE1"].text[0], - "album": audio["TALB"].text[0], - "track_number": audio["TRCK"].text[0], - "genre": audio["TCON"].text[0], - "date": convert_id3_timestamp_to_datetime(audio["TDRC"].text[0]), - "bitrate": audio["TBIT"].text[0], + remap = { + "title": audio.get("TIT2"), + "artist": audio.get("TPE1"), + "album": audio.get("TALB"), + "track_number": audio.get("TRCK"), + "genre": audio.get("TCON"), + "date": convert_id3_timestamp_to_datetime(audio.get("TDRC")), + "bitrate": audio.get("TBIT"), + "lyrics": audio.get("USLT"), } + if isinstance(audio, MP3): + remap["length"] = int(round(audio.info.length, 0)) + return remap def get_id3_tags(filename: str) -> tuple[MP3 | ID3 | FLAC, str]: @@ -61,8 +65,13 @@ def get_id3_tags(filename: str) -> tuple[MP3 | ID3 | FLAC, str]: - filename Returns - tuple(ID3/dict, fail_reason) - ID3 dict looks like this: - {'TIT2': TIT2(encoding=, text=['song title']), 'TSSE': TSSE(encoding=, text=['Lavf59.27.100'])} + + ID3 dict looks like this: + { + 'TIT2': TIT2(encoding=, text=['song title']), + 'TSSE': TSSE(encoding=, text=['Lavf59.27.100'], + ... + } """ if filename.endswith(".mp3"): tags, details = get_mp3_tags(filename)