Refactor pt 1
This commit is contained in:
+42
-16
@@ -4,28 +4,54 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
from mutagen.id3 import ID3, APIC, TIT2, TPE1, TCOM, TCON, TSOA, TRCK, TIT3, TALB, COMM
|
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 traverse_directory(directory):
|
||||||
'''Iterate through all subfolders and files'''
|
'''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:
|
||||||
subfolder_path = os.path.join(root, dir)
|
subfolder_path = os.path.join(root, dir)
|
||||||
if os.path.exists(os.path.join(subfolder_path, "cleaned_metadata.json")):
|
metadata_file = os.path.join(subfolder_path, "cleaned_metadata.json")
|
||||||
add_metadata(subfolder_path)
|
if os.path.exists(metadata_file):
|
||||||
|
metadata = read_metadata(metadata_file)
|
||||||
|
if metadata is None:
|
||||||
|
print("Skipping due to json error")
|
||||||
|
continue
|
||||||
|
add_metadata_to_files(subfolder_path, metadata)
|
||||||
|
|
||||||
def add_metadata(directory):
|
def read_metadata(metadata_file):
|
||||||
'''Get the metadata from the cleaned_metadata.json file in the current subfolder'''
|
'''Read and return json data from the given metadata file'''
|
||||||
with open(os.path.join(directory, "cleaned_metadata.json"), "r") as f:
|
try:
|
||||||
json_data = json.load(f)
|
with open(metadata_file, "r") as f:
|
||||||
artist = json_data.get("Author", "")
|
return json.load(f)
|
||||||
composer = json_data.get("Narrator", "")
|
except (FileNotFoundError, json.JSONDecodeError) as e:
|
||||||
genre = json_data.get("Subjects", "")
|
print(f"Error reading metadata file {metadata_file}: {e}")
|
||||||
album_title = json_data.get("Title", "")
|
return None
|
||||||
|
|
||||||
'''Replace all occurrences of -s with "'s" in the album title'''
|
def get_album_title(metadata):
|
||||||
album_title = re.sub("-s", "'s", album_title)
|
'''Extract the Book Title from metadata and clean it'''
|
||||||
|
album_title = metadata.get("Title", "")
|
||||||
|
return re.sub(r"-s\b", "'s", album_title)
|
||||||
|
|
||||||
|
def get_artist(metadata):
|
||||||
|
'''Extract the Author from metadata'''
|
||||||
|
return metadata.get("Author", "")
|
||||||
|
|
||||||
|
def get_composer(metadata):
|
||||||
|
'''Extract the Narrator from metadata'''
|
||||||
|
return metadata.get("Narrator", "")
|
||||||
|
|
||||||
|
def get_genre(metadata):
|
||||||
|
'''Extract the Subjects from metadata'''
|
||||||
|
return metadata.get("Subjects", "")
|
||||||
|
|
||||||
|
def add_metadata_to_files(directory, metadata):
|
||||||
|
'''Add metadata to all chapterized mp3s in the directory'''
|
||||||
|
album_title = get_album_title(metadata)
|
||||||
|
artist = get_artist(metadata)
|
||||||
|
composer = get_composer(metadata)
|
||||||
|
genre = get_genre(metadata)
|
||||||
|
|
||||||
'''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")]
|
mp3_files = [f for f in os.listdir(directory) if f.endswith(".mp3")]
|
||||||
@@ -38,7 +64,7 @@ def add_metadata(directory):
|
|||||||
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, file_ext = os.path.splitext(mp3_file)
|
file_name = os.path.splitext(mp3_file)
|
||||||
title = file_name.lstrip("0123456789_")
|
title = file_name.lstrip("0123456789_")
|
||||||
|
|
||||||
'''Add the track number to the file using ID3'''
|
'''Add the track number to the file using ID3'''
|
||||||
@@ -63,7 +89,7 @@ def add_metadata(directory):
|
|||||||
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()
|
||||||
|
|
||||||
'''Point the script at a directory'''
|
'''Point the script at your Circulation folder'''
|
||||||
directory = circulation
|
directory = circulation
|
||||||
|
|
||||||
traverse_directory(directory)
|
traverse_directory(directory)
|
||||||
|
|||||||
Reference in New Issue
Block a user