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 mutagen.id3 import ID3, APIC, TIT2, TPE1, TCOM, TCON, TRCK, TALB
from config import circulation,bookshelf from config import circulation,bookshelf
def traverse_directory(directory): def process_directories(directory):
'''Iterate through all subfolders & create cleaned_metadata.json''' '''Iterate through all subfolders & create cleaned_metadata.json'''
for root, dirs, files in os.walk(directory): for root, dirs, files in os.walk(directory):
for dir in dirs: for dir in dirs:
@@ -48,6 +48,9 @@ def get_genre(metadata):
def add_metadata_to_files(directory, metadata): def add_metadata_to_files(directory, metadata):
'''Add metadata to all chapterized mp3s in the directory''' '''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) album_title = get_album_title(metadata)
artist = get_artist(metadata) artist = get_artist(metadata)
composer = get_composer(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) mp3_file_path = os.path.join(directory, mp3_file)
'''Extract the title of the mp3 file''' '''Extract the title of the mp3 file'''
file_name = os.path.splitext(mp3_file) file_name, _ = os.path.splitext(mp3_file)
title = file_name.lstrip("0123456789_") title = re.sub(r"^\d+_", "", file_name) # Correctly extracts the title from the filename
'''Add the track number to the file using ID3''' '''Add the track number & other metadata to the file using ID3'''
audio = ID3(mp3_file_path) try:
audio.add(TRCK(encoding=3, text=str(i))) # Add the track number & other metadata to the file using ID3
audio.add(TIT2(encoding=3, text=title)) audio = ID3(mp3_file_path)
audio.save() 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''' def meta_to_mp3(directory):
audio = ID3(mp3_file_path) for root, dirs, files in os.walk(directory):
audio.add(TPE1(encoding=3, text=artist)) '''Traverse the directory and apply the metadata to the mp3 files'''
audio.add(TCOM(encoding=3, text=composer)) for dir in dirs:
audio.add(TCON(encoding=3, text=genre)) subfolder_path = os.path.join(root, dir)
audio.add(TALB(encoding=3, text=album_title)) parent_folder = os.path.dirname(subfolder_path)
audio.save() 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''' def traverse_directory(directory):
if os.path.exists(os.path.join(directory, "folder.jpg")): process_directories(directory)
'''Add the album art to the file using ID3''' meta_to_mp3(directory)
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()
'''Point the script at your Circulation folder''' '''Point the script at your Circulation folder'''
directory = circulation directory = circulation
traverse_directory(directory) 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)