Arguments added to the utility.

This commit is contained in:
2025-01-08 11:57:16 +01:00
parent 19fbd27f3a
commit 5dd2aadd7c
+61 -25
View File
@@ -3,44 +3,46 @@
import time import time
import re import re
import os import os
import argparse
from mutagen.easyid3 import EasyID3 from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
def update_metadata(base_folder):
for root, _, files in os.walk(base_folder):
for file in files:
if file.endswith('.mp3'):
full_path = os.path.join(root, file)
try:
print(f"Processing {full_path}")
# Parse metadata from the filename or path
# For example, assume filenames are in the format "Artist - Title.mp3"
filename = os.path.splitext(file)[0]
parts = filename.split(' - ')
if len(parts) == 2:
artist, title = parts
else:
artist = 'Unknown Artist'
title = filename
# Load the MP3 file def update_metadata(folder_path):
audio = MP3(full_path, ID3=EasyID3) for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith(".mp3"):
file_path = os.path.join(root, file)
# Sample logic for guessing metadata from filename
# Assuming the filename format is "Artist - Title.mp3"
try:
artist, title = file.rsplit(" - ", 1)
title = title.replace(".mp3", "")
except ValueError:
# If file name doesn't fit the expected pattern, skip
print(f"Skipping file due to unexpected format: {file}")
continue
try:
# Load the mp3 file
audio = MP3(file_path, ID3=EasyID3)
# Update metadata # Update metadata
audio['title'] = title audio["artist"] = artist.strip()
audio['artist'] = artist audio["title"] = title.strip()
# Save changes # Save changes
audio.save() audio.save()
print(f"Updated metadata: Artist = {artist}, Title = {title}") print(f"Updated metadata for: {file}")
except Exception as e: except Exception as e:
print(f"Error processing {file}: {e}") print(f"Failed to update metadata for {file}: {e}")
base_folder = 'path/to/your/folder' # Update with the path to your folder
#update_metadata(base_folder)
NAME = "/home/pi/Conjurer/persistence.log" NAME = "/home/pi/Conjurer/persistence.log"
def print_top(): def print_top():
""" """
Prints the top 65 lines from a file specified by the NAME variable. Prints the top 65 lines from a file specified by the NAME variable.
@@ -58,5 +60,39 @@ def print_top():
print("=====================================") print("=====================================")
time.sleep(180) time.sleep(180)
if __name__ == "__main__":
def main():
parser = argparse.ArgumentParser(
description="Perform operations on a directory of .mp3 files."
)
# Positional argument for choosing the function to execute
parser.add_argument(
"operation",
type=str,
choices=["update_metadata", "print_top"],
help="Operation to perform: update_metadata or print_top.",
default="print_top",
)
# Positional argument for folder path
parser.add_argument("folder", type=str, help="Path to the folder to operate on.")
args = parser.parse_args()
if args.operation == "update_metadata":
if args.folder:
folder_to_scan = args.folder
else:
print("Please provide a folder path to scan.")
raise ValueError("No folder path provided.")
if args.operation == "update_metadata" and os.path.isdir(folder_to_scan):
update_metadata(folder_to_scan)
elif args.operation == "print_top":
print_top() print_top()
else:
print(f"The specified path is not a directory: {folder_to_scan}")
if __name__ == "__main__":
main()