Refactored & cleaned up

This commit is contained in:
LeLawnGames
2023-11-08 10:43:58 -05:00
parent 386e1cefef
commit f276d5e245
+40 -31
View File
@@ -7,7 +7,7 @@ import re
from mutagen.id3 import ID3, APIC, TIT2, TPE1, TCOM, TCON, TRCK, TALB
from config import circulation,bookshelf
def traverse_directory(directory):
def process_directories(directory):
'''Iterate through all subfolders & create cleaned_metadata.json'''
for root, dirs, files in os.walk(directory):
for dir in dirs:
@@ -48,6 +48,9 @@ def get_genre(metadata):
def add_metadata_to_files(directory, metadata):
'''Add metadata to all chapterized mp3s in the directory'''
if not metadata:
print(f"No metadata available for directory {directory}. Skipping.")
return
album_title = get_album_title(metadata)
artist = get_artist(metadata)
composer = get_composer(metadata)
@@ -64,40 +67,46 @@ def add_metadata_to_files(directory, metadata):
mp3_file_path = os.path.join(directory, mp3_file)
'''Extract the title of the mp3 file'''
file_name = os.path.splitext(mp3_file)
title = file_name.lstrip("0123456789_")
file_name, _ = os.path.splitext(mp3_file)
title = re.sub(r"^\d+_", "", file_name) # Correctly extracts the title from the filename
'''Add the track number to the file using ID3'''
audio = ID3(mp3_file_path)
audio.add(TRCK(encoding=3, text=str(i)))
audio.add(TIT2(encoding=3, text=title))
audio.save()
'''Add the track number & other metadata to the file using ID3'''
try:
# Add the track number & other metadata to the file using ID3
audio = ID3(mp3_file_path)
audio.add(TRCK(encoding=3, text=str(i)))
audio.add(TIT2(encoding=3, text=title))
audio.add(TPE1(encoding=3, text=artist))
audio.add(TCOM(encoding=3, text=composer))
audio.add(TCON(encoding=3, text=genre))
audio.add(TALB(encoding=3, text=album_title))
'''Check if folder.jpg exists in the current directory'''
if os.path.exists(os.path.join(directory, "folder.jpg")):
'''Add the album art to the file using ID3'''
with open(os.path.join(directory, "folder.jpg"), "rb") as albumart:
audio = ID3(mp3_file_path)
audio.add(APIC(encoding=3, mime='image/jpeg', type=3, desc='Cover', data=albumart.read()))
audio.save()
except Exception as e:
print(f"Failed to add metadata to {mp3_file_path}: {e}")
'''Add the metadata to the file using ID3'''
audio = ID3(mp3_file_path)
audio.add(TPE1(encoding=3, text=artist))
audio.add(TCOM(encoding=3, text=composer))
audio.add(TCON(encoding=3, text=genre))
audio.add(TALB(encoding=3, text=album_title))
audio.save()
def meta_to_mp3(directory):
for root, dirs, files in os.walk(directory):
'''Traverse the directory and apply the metadata to the mp3 files'''
for dir in dirs:
subfolder_path = os.path.join(root, dir)
parent_folder = os.path.dirname(subfolder_path)
if parent_folder == directory:
try:
shutil.move(subfolder_path, bookshelf)
except OSError as e:
print(f"Error moving directory {subfolder_path} to {bookshelf}: {e}")
'''Check if folder.jpg exists in the current directory'''
if os.path.exists(os.path.join(directory, "folder.jpg")):
'''Add the album art to the file using ID3'''
with open(os.path.join(directory, "folder.jpg"), "rb") as albumart:
audio = ID3(mp3_file_path)
audio.add(APIC(encoding=3, mime='image/jpeg', type=3, desc='Cover', data=albumart.read()))
audio.save()
def traverse_directory(directory):
process_directories(directory)
meta_to_mp3(directory)
'''Point the script at your Circulation folder'''
directory = circulation
traverse_directory(directory)
for root, dirs, files in os.walk(directory):
'''Traverse the directory and apply the metadata to the mp3 files'''
for dir in dirs:
subfolder_path = os.path.join(root, dir)
parent_folder = os.path.dirname(subfolder_path)
if parent_folder == directory:
shutil.move(subfolder_path, bookshelf)
traverse_directory(directory)