Updated formatting & removed redundancies
This commit is contained in:
+14
-16
@@ -1,4 +1,4 @@
|
||||
#Confirmed working as of 1/23/23
|
||||
'''Adds metadata to the chapterized mp3s'''
|
||||
|
||||
import os
|
||||
import shutil
|
||||
@@ -8,7 +8,7 @@ from mutagen.id3 import ID3, APIC, TIT2, TPE1, TCOM, TCON, TSOA, TRCK, TIT3, TAL
|
||||
from config import circulation,bookshelf
|
||||
|
||||
def traverse_directory(directory):
|
||||
# Iterate through all subfolders and files
|
||||
'''Iterate through all subfolders and files'''
|
||||
for root, dirs, files in os.walk(directory):
|
||||
for dir in dirs:
|
||||
subfolder_path = os.path.join(root, dir)
|
||||
@@ -16,7 +16,7 @@ def traverse_directory(directory):
|
||||
add_metadata(subfolder_path)
|
||||
|
||||
def add_metadata(directory):
|
||||
# Get the metadata from the cleaned_metadata.json file in the current subfolder
|
||||
'''Get the metadata from the cleaned_metadata.json file in the current subfolder'''
|
||||
with open(os.path.join(directory, "cleaned_metadata.json"), "r") as f:
|
||||
json_data = json.load(f)
|
||||
artist = json_data.get("Author", "")
|
||||
@@ -24,32 +24,30 @@ def add_metadata(directory):
|
||||
genre = json_data.get("Subjects", "")
|
||||
album_title = json_data.get("Title", "")
|
||||
|
||||
# Rest of the code
|
||||
|
||||
# Replace all occurrences of -s with 's in the album title
|
||||
'''Replace all occurrences of -s with "'s" in the album title'''
|
||||
album_title = re.sub("-s", "'s", album_title)
|
||||
|
||||
# Get a list of all mp3 files in the current directory
|
||||
'''Get a list of all mp3 files in the current directory'''
|
||||
mp3_files = [f for f in os.listdir(directory) if f.endswith(".mp3")]
|
||||
|
||||
# Sort the list of mp3 files alphabetically
|
||||
'''Sort the list of mp3 files alphabetically'''
|
||||
mp3_files.sort()
|
||||
|
||||
# Iterate through the list of mp3 files
|
||||
'''Iterate through the list of mp3 files'''
|
||||
for i, mp3_file in enumerate(mp3_files, 1):
|
||||
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, file_ext = os.path.splitext(mp3_file)
|
||||
title = file_name.lstrip("0123456789_")
|
||||
|
||||
# Add the track number to the file using ID3
|
||||
'''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 metadata to the file using ID3
|
||||
'''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))
|
||||
@@ -57,21 +55,21 @@ def add_metadata(directory):
|
||||
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")):
|
||||
# Add the album art to the file using ID3
|
||||
'''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()
|
||||
|
||||
# Point the script at a directory
|
||||
'''Point the script at a directory'''
|
||||
directory = circulation
|
||||
|
||||
# Traverse the directory and apply the metadata to the mp3 files
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user