Refactored & cleaned up

This commit is contained in:
LeLawnGames
2023-11-08 10:43:58 -05:00
parent 386e1cefef
commit f276d5e245
+24 -15
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,23 +67,19 @@ 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'''
try:
# Add the track number & other metadata to the file using ID3
audio = ID3(mp3_file_path) audio = ID3(mp3_file_path)
audio.add(TRCK(encoding=3, text=str(i))) audio.add(TRCK(encoding=3, text=str(i)))
audio.add(TIT2(encoding=3, text=title)) audio.add(TIT2(encoding=3, text=title))
audio.save()
'''Add the metadata to the file using ID3'''
audio = ID3(mp3_file_path)
audio.add(TPE1(encoding=3, text=artist)) audio.add(TPE1(encoding=3, text=artist))
audio.add(TCOM(encoding=3, text=composer)) audio.add(TCOM(encoding=3, text=composer))
audio.add(TCON(encoding=3, text=genre)) audio.add(TCON(encoding=3, text=genre))
audio.add(TALB(encoding=3, text=album_title)) audio.add(TALB(encoding=3, text=album_title))
audio.save()
'''Check if folder.jpg exists in the current directory''' '''Check if folder.jpg exists in the current directory'''
if os.path.exists(os.path.join(directory, "folder.jpg")): if os.path.exists(os.path.join(directory, "folder.jpg")):
'''Add the album art to the file using ID3''' '''Add the album art to the file using ID3'''
@@ -88,16 +87,26 @@ def add_metadata_to_files(directory, metadata):
audio = ID3(mp3_file_path) audio = ID3(mp3_file_path)
audio.add(APIC(encoding=3, mime='image/jpeg', type=3, desc='Cover', data=albumart.read())) audio.add(APIC(encoding=3, mime='image/jpeg', type=3, desc='Cover', data=albumart.read()))
audio.save() audio.save()
except Exception as e:
print(f"Failed to add metadata to {mp3_file_path}: {e}")
'''Point the script at your Circulation folder''' def meta_to_mp3(directory):
directory = circulation
traverse_directory(directory)
for root, dirs, files in os.walk(directory): for root, dirs, files in os.walk(directory):
'''Traverse the directory and apply the metadata to the mp3 files''' '''Traverse the directory and apply the metadata to the mp3 files'''
for dir in dirs: for dir in dirs:
subfolder_path = os.path.join(root, dir) subfolder_path = os.path.join(root, dir)
parent_folder = os.path.dirname(subfolder_path) parent_folder = os.path.dirname(subfolder_path)
if parent_folder == directory: if parent_folder == directory:
try:
shutil.move(subfolder_path, bookshelf) shutil.move(subfolder_path, bookshelf)
except OSError as e:
print(f"Error moving directory {subfolder_path} to {bookshelf}: {e}")
def traverse_directory(directory):
process_directories(directory)
meta_to_mp3(directory)
'''Point the script at your Circulation folder'''
directory = circulation
traverse_directory(directory)