tyer tdat tdrc crap

This commit is contained in:
billypom on debian 2024-08-21 16:34:33 -04:00
parent 9be971f1b3
commit 47f13045e7
7 changed files with 68 additions and 37 deletions

View File

@ -15,6 +15,7 @@ from utils.get_id3_tags import get_id3_tags
from utils.set_id3_tag import set_id3_tag
from utils.update_song_in_database import update_song_in_database
from utils.id3_tag_mapping import id3_tag_mapping
# import re
class ID3LineEdit(QLineEdit):
@ -30,14 +31,14 @@ class ID3LineEdit(QLineEdit):
class MetadataWindow(QDialog):
refreshMusicTableSignal = pyqtSignal()
def __init__(self, refreshMusicTableSignal, songs: list, ids: list):
"""
Window that allows batch editing of metadata for multiple files
Input: songs
Input: songs, ids
- list of strings, absolute paths to mp3 files
- list of database song ids
"""
super(MetadataWindow, self).__init__()
self.refreshMusicTableSignal = refreshMusicTableSignal
@ -89,12 +90,15 @@ class MetadataWindow(QDialog):
# Field Creation
if value == list(set(value)):
print(value)
# If the ID3 tag is the same for every item we're editing
field_text = str(value[0]) if value else ""
# Normal field
label = QLabel(str(self.id3_tag_mapping[tag]))
input_field = ID3LineEdit(field_text, tag)
input_field.setStyleSheet(None)
else:
print(value)
# Danger field
# this means the metadata differs between the selected items for this tag
# so be careful...dangerous
@ -106,6 +110,7 @@ class MetadataWindow(QDialog):
self.input_fields[tag] = input_field
current_layout.addWidget(label)
current_layout.addWidget(input_field)
layout.addLayout(current_layout)
# Save button
save_button = QPushButton("Save")
@ -119,6 +124,20 @@ class MetadataWindow(QDialog):
for tag, field in self.input_fields.items():
if field.text() is not None and field.text() != "":
if field.has_changed():
# date crap...
# if tag == "TYER":
# continue
# if tag == "TDAT":
# match = re.match(
# r"(\d{4})[-/](\d{2})[-/](\d{2})", field.text()
# )
# if not match:
# continue
# year, _, _ = match.groups()
# _ = set_id3_tag(
# filepath=song[0], tag_name="TYER", value=str(year)
# )
# BUSINESS AS USUAL
# Update the ID3 tag if the tag is not blank,
# and has been edited
success = set_id3_tag(

View File

@ -44,7 +44,7 @@ class MusicTable(QTableView):
# QTableView.__init__(self, parent)
super().__init__(parent)
# Necessary for actions related to cell values
# FIXME: why do these give me pyright errors
# FIXME: why does this give me pyright errors
self.model = QStandardItemModel(self)
# self.model = QAbstractItemModel(self)
self.setModel(self.model)

View File

@ -10,4 +10,4 @@ from .delete_and_create_library_database import delete_and_create_library_databa
from .update_song_in_database import update_song_in_database
from .scan_for_music import scan_for_music
from .add_files_to_library import add_files_to_library
from .handle_year_and_date_id3_tag import handle_year_and_date_id3_tag
from .convert_date_str_to_tyer_tdat_id3_tag import convert_date_str_to_tyer_tdat_id3_tag

View File

@ -0,0 +1,28 @@
import re
from typing import Tuple
from mutagen.id3._frames import TYER, TDAT
def convert_date_str_to_tyer_tdat_id3_tag(date_str) -> Tuple:
"""
Handles date formatting when updating a date record in the music table
Date string format = YYYY-MM-DD
"""
if not date_str:
return None, None
match = re.match(r"(\d{4})[-/](\d{2})[-/](\d{2})", date_str)
if not match:
raise ValueError("Invalid date format")
# print(f"convert_date_str_to_tyer_tdat_id3_tag(): match: {match}")
year, month, day = match.groups()
# print(
# f"convert_date_str_to_tyer_tdat_id3_tag(): month: {month} | day: {day} | year: {year}"
# )
# only year
if not month or not day:
# print(TYER(encoding=3, text=year))
return TYER(encoding=3, text=year), None
else:
date_value = f"{day}{month}"
# print(TYER(encoding=3, text=year), TDAT(encoding=3, text=date_value))
return TYER(encoding=3, text=year), TDAT(encoding=3, text=date_value)

View File

@ -1,23 +0,0 @@
import re
from mutagen.id3 import TYER, TDAT
def handle_year_and_date_id3_tag(date_str):
"""
Handles date formatting when updating a date record in the music table
Date format = YYYY-MM-DD
"""
match = re.match(r"(\d{4})[-/](\d{2})[-/](\d{2})", date_str)
if not match:
raise ValueError("Invalid date format")
print(f"handle_year_and_date_id3_tag(): match: {match}")
year, month, day = match.groups()
print(f"handle_year_and_date_id3_tag(): month: {month} | day: {day} | year: {year}")
# only year
if not month or not day:
print(TYER(encoding=3, text=year))
return TYER(encoding=3, text=year), None
else:
date_value = f"{day}{month}"
print(TYER(encoding=3, text=year), TDAT(encoding=3, text=date_value))
return TYER(encoding=3, text=year), TDAT(encoding=3, text=date_value)

View File

@ -5,6 +5,9 @@ id3_tag_mapping = {
"TPE2": "album_artist",
"TCON": "genre",
"TRCK": "track_number",
"TDRC": "album_date",
# "TYER": "year",
# "TDAT": "date",
# "APIC": "album_cover",
"TCOP": "copyright",
}

View File

@ -1,6 +1,8 @@
import logging
from components import ErrorDialog
from utils.handle_year_and_date_id3_tag import handle_year_and_date_id3_tag
from utils.convert_date_str_to_tyer_tdat_id3_tag import (
convert_date_str_to_tyer_tdat_id3_tag,
)
from mutagen.id3 import ID3
from mutagen.id3._util import ID3NoHeaderError
from mutagen.id3._frames import (
@ -9,6 +11,7 @@ from mutagen.id3._frames import (
TPE1,
TALB,
TRCK,
TDRC,
# TYER,
TCON,
TPOS,
@ -46,6 +49,7 @@ mutagen_id3_tag_mapping = {
"album": TALB, # Album/Movie/Show title
"album_artist": TPE2, # Band/orchestra/accompaniment
"genre": TCON, # Content type
"album_date": TDRC,
# "year": TYER, # Year of recording
# "date": TDAT, # Date
"lyrics": USLT, # Unsynchronized lyric/text transcription
@ -97,15 +101,15 @@ def set_id3_tag(filepath: str, tag_name: str, value: str):
except ID3NoHeaderError: # Create new tags if none exist
audio_file = ID3()
# Date handling - TDRC vs TYER+TDAT
if tag_name == "album_date":
tyer_tag, tdat_tag = handle_year_and_date_id3_tag(value)
# always update TYER
audio_file.add(tyer_tag)
if tdat_tag:
# update TDAT if we have it
audio_file.add(tdat_tag)
# if tag_name == "album_date":
# tyer_tag, tdat_tag = convert_date_str_to_tyer_tdat_id3_tag(value)
# # always update TYER
# audio_file.add(tyer_tag)
# if tdat_tag:
# # update TDAT if we have it
# audio_file.add(tdat_tag)
# Lyrics
elif tag_name == "lyrics" or tag_name == "USLT":
if tag_name == "lyrics" or tag_name == "USLT":
try:
audio = ID3(filepath)
except Exception as e: