track number functionality

This commit is contained in:
billypom on debian 2024-07-31 18:11:06 -04:00
parent cd61d76cc8
commit 24128f97ba
6 changed files with 27 additions and 11 deletions

View File

@ -49,6 +49,7 @@ class MusicTable(QTableView):
"title",
"artist",
"album",
"track_number",
"genre",
"codec",
"year",
@ -59,9 +60,10 @@ class MusicTable(QTableView):
"TIT2",
"TPE1",
"TALB",
"TRCK",
"content_type",
None,
None,
"TDRC",
None,
]
# db names of headers
@ -327,7 +329,7 @@ class MusicTable(QTableView):
try:
with DBA.DBAccess() as db:
data = db.query(
"SELECT id, title, artist, album, genre, codec, album_date, filepath FROM song;",
"SELECT id, title, artist, album, track_number, genre, codec, album_date, filepath FROM song;",
(),
)
except Exception as e:

View File

@ -34,6 +34,10 @@ def add_files_to_library(files):
album = audio["TALB"].text[0]
except KeyError:
album = ""
try:
track_number = audio["TRCK"].text[0]
except KeyError:
track_number = 0
try:
genre = audio["TCON"].text[0]
except KeyError:
@ -54,6 +58,7 @@ def add_files_to_library(files):
title,
album,
artist,
track_number,
genre,
filename.split(".")[-1],
date,
@ -64,7 +69,7 @@ def add_files_to_library(files):
if len(insert_data) >= 1000:
with DBA.DBAccess() as db:
db.executemany(
"INSERT OR IGNORE INTO song (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
"INSERT OR IGNORE INTO song (filepath, title, album, artist, track_number, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
insert_data,
)
insert_data = [] # Reset the insert_data list
@ -72,7 +77,7 @@ def add_files_to_library(files):
if insert_data:
with DBA.DBAccess() as db:
db.executemany(
"INSERT OR IGNORE INTO song (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
"INSERT OR IGNORE INTO song (filepath, title, album, artist, track_number, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
insert_data,
)
return True

View File

@ -7,6 +7,7 @@ CREATE TABLE song(
title varchar(255),
album varchar(255),
artist varchar(255),
track_number integer,
genre varchar(255),
codec varchar(15),
album_date date,

View File

@ -5,11 +5,14 @@ import os
def get_id3_tags(file):
"""Get the ID3 tags for an audio file
# Parameters
`file` | str | Fully qualified path to file
# Returns
dict of all id3 tags
if all tags are empty, at minimum fill in the 'title'
at minimum we will get the filename as a title.[ID3:TIT2]
"""
try:
@ -20,9 +23,10 @@ def get_id3_tags(file):
# Check if all tags are empty
# tags_are_empty = all(not values for values in audio.values())
try:
title = os.path.splitext(os.path.basename(file))[0]
frame = TIT2(encoding=3, text=[title])
audio["TIT2"] = frame
if audio["TIT2"] is None:
title = os.path.splitext(os.path.basename(file))[0]
frame = TIT2(encoding=3, text=[title])
audio["TIT2"] = frame
except Exception as e:
print(f"get_id3_tags.py | Exception: {e}")
pass

View File

@ -9,6 +9,7 @@ CREATE TABLE song(
title varchar(255),
album varchar(255),
artist varchar(255),
track_number integer,
genre varchar(255),
codec varchar(15),
album_date date,

View File

@ -87,15 +87,16 @@ def set_id3_tag(filepath: str, tag_name: str, value: str):
Returns:
True / False"""
# print(
# f"set_id3_tag.py | filepath: {filepath} | tag_name: {tag_name} | value: {value}"
# )
print(
f"set_id3_tag.py | filepath: {filepath} | tag_name: {tag_name} | value: {value}"
)
try:
try: # Load existing tags
audio_file = ID3(filepath)
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
@ -103,6 +104,7 @@ def set_id3_tag(filepath: str, tag_name: str, value: str):
if tdat_tag:
# update TDAT if we have it
audio_file.add(tdat_tag)
# Lyrics
elif tag_name == "lyrics" or tag_name == "USLT":
try:
audio = ID3(filepath)
@ -116,6 +118,7 @@ def set_id3_tag(filepath: str, tag_name: str, value: str):
audio.add(frame)
audio.save()
return True
# Other
elif tag_name in id3_tag_mapping: # Tag accounted for
tag_class = id3_tag_mapping[tag_name]
if issubclass(tag_class, Frame):