96 lines
3.2 KiB
Python
Executable File
96 lines
3.2 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# - generated, gpt3.5
|
|
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import datetime
|
|
from pathlib import Path
|
|
|
|
# Configurable variables
|
|
MOVIES_EXTENSION = {".mkv", ".mp4", ".avi"}
|
|
MUSIC_EXTENSION = {".mp3", ".ogg", ".flac", ".wma", ".wav"}
|
|
EPISODIC_THRESHOLD = 1 # Maximum number of video files to consider as episodic (inclusive)
|
|
MOVIES_PATH = "Movies"
|
|
MUSIC_PATH = "Music"
|
|
SHOWS_PATH = "Shows"
|
|
|
|
def organize_folders(directory, root_path):
|
|
log_file = create_log_file(directory, root_path)
|
|
write_log(log_file, f"Organizing folders in {directory}")
|
|
|
|
actions = preview_actions(directory, root_path)
|
|
if confirm_actions(actions):
|
|
execute_actions(actions, log_file)
|
|
else:
|
|
log_file.close()
|
|
os.remove(log_file.name)
|
|
print("Log file deleted.")
|
|
|
|
log_file_path = os.path.abspath(log_file.name)
|
|
print(f"Log file saved at: {log_file_path}")
|
|
|
|
def preview_actions(directory, root_path):
|
|
actions = []
|
|
|
|
for folder_name in os.listdir(directory):
|
|
folder_path = os.path.join(directory, folder_name)
|
|
|
|
if os.path.isdir(folder_path):
|
|
file_types = set()
|
|
|
|
for file in os.listdir(folder_path):
|
|
file_extension = Path(file).suffix.lower()
|
|
file_types.add(file_extension)
|
|
|
|
if MOVIES_EXTENSION.intersection(file_types) and len(file_types) <= EPISODIC_THRESHOLD:
|
|
actions.append((folder_path, os.path.join(root_path, MOVIES_PATH, folder_name)))
|
|
elif len(file_types.intersection(MUSIC_EXTENSION)) == len(file_types):
|
|
actions.append((folder_path, os.path.join(root_path, MUSIC_PATH, folder_name)))
|
|
elif len(file_types) > EPISODIC_THRESHOLD:
|
|
actions.append((folder_path, os.path.join(root_path, SHOWS_PATH, folder_name)))
|
|
|
|
return actions
|
|
|
|
def confirm_actions(actions):
|
|
print("Preview of actions:")
|
|
for source, destination in actions:
|
|
print(f"Move: {source} -> {destination}")
|
|
|
|
user_input = input("Do you want to proceed with the above actions? (y/n): ").lower()
|
|
return user_input == "y"
|
|
|
|
def execute_actions(actions, log_file):
|
|
for source, destination in actions:
|
|
message = f"Moving {os.path.basename(source)} to {destination}"
|
|
write_log(log_file, message)
|
|
print(message)
|
|
shutil.move(source, destination)
|
|
|
|
def move_folder(folder_path, destination):
|
|
if not os.path.exists(destination):
|
|
os.makedirs(destination)
|
|
shutil.move(folder_path, os.path.join(destination, os.path.basename(folder_path)))
|
|
|
|
def create_log_file(directory, root_path):
|
|
current_time = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
|
|
log_file_name = f"{current_time}.log"
|
|
log_file_path = os.path.join(root_path, directory, log_file_name)
|
|
return open(log_file_path, "w")
|
|
|
|
def write_log(log_file, message):
|
|
timestamp = datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
|
|
log_message = f"{timestamp} {message}\n"
|
|
log_file.write(log_message)
|
|
print(log_message, end="")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
print("Usage: ./organize_folders.py <directory_path> <root_path>")
|
|
sys.exit(1)
|
|
|
|
directory = sys.argv[1]
|
|
root_path = sys.argv[2]
|
|
organize_folders(directory, root_path)
|
|
|