afegit create_m3u_multidisc_files.py
This commit is contained in:
@@ -1,34 +0,0 @@
|
|||||||
import os
|
|
||||||
import re
|
|
||||||
|
|
||||||
def create_m3u_files(folder_path):
|
|
||||||
# Diccionario para almacenar listas de archivos por nombre base
|
|
||||||
files_dict = {}
|
|
||||||
|
|
||||||
# Expresión regular para detectar el patrón de (Disc X)
|
|
||||||
pattern = re.compile(r"\(Disc \d+\)")
|
|
||||||
|
|
||||||
for filename in os.listdir(folder_path):
|
|
||||||
if filename.endswith(".chd") and pattern.search(filename):
|
|
||||||
# Extraer el nombre base
|
|
||||||
base_name = pattern.split(filename)[0].strip()
|
|
||||||
|
|
||||||
if base_name not in files_dict:
|
|
||||||
files_dict[base_name] = []
|
|
||||||
|
|
||||||
files_dict[base_name].append(filename)
|
|
||||||
|
|
||||||
# Crear archivos .m3u
|
|
||||||
for base_name, files in files_dict.items():
|
|
||||||
m3u_filename = f"{base_name}.m3u"
|
|
||||||
m3u_filepath = os.path.join(folder_path, m3u_filename)
|
|
||||||
|
|
||||||
with open(m3u_filepath, 'w') as m3u_file:
|
|
||||||
for file in files:
|
|
||||||
m3u_file.write(f"{file}\n")
|
|
||||||
|
|
||||||
print(f"Archivo {m3u_filename} creado con éxito")
|
|
||||||
|
|
||||||
# Ruta de la carpeta con los archivos
|
|
||||||
folder_path = "/ruta/a/la/carpeta"
|
|
||||||
create_m3u_files(folder_path)
|
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def organize_files(base_path):
|
||||||
|
# Expresión regular para buscar específicamente '(Disc X)' donde X es un único dígito
|
||||||
|
disc_pattern = re.compile(r'\(Disc \d\)')
|
||||||
|
|
||||||
|
print(f"Iniciando organización de archivos en: {base_path}")
|
||||||
|
|
||||||
|
# Obtiene una lista de archivos que coincidan exactamente con el formato esperado
|
||||||
|
files = [f for f in os.listdir(base_path) if disc_pattern.search(f)]
|
||||||
|
print(f"Archivos detectados con '(Disc X)': {len(files)} encontrados")
|
||||||
|
|
||||||
|
# Diccionario para agrupar por nombre base
|
||||||
|
groups = {}
|
||||||
|
|
||||||
|
# Organiza los archivos según el nombre base (todo antes de "(Disc X)")
|
||||||
|
for file in files:
|
||||||
|
base_name = disc_pattern.split(file)[0].strip()
|
||||||
|
if base_name not in groups:
|
||||||
|
groups[base_name] = []
|
||||||
|
groups[base_name].append(file)
|
||||||
|
|
||||||
|
print(f"Grupos formados: {len(groups)} grupos identificados")
|
||||||
|
|
||||||
|
# Crear carpetas, mover archivos y generar archivos .m3u
|
||||||
|
for base_name, group_files in groups.items():
|
||||||
|
print(f"Procesando grupo: {base_name}")
|
||||||
|
|
||||||
|
# Crear la carpeta para el grupo
|
||||||
|
folder_path = os.path.join(base_path, base_name)
|
||||||
|
os.makedirs(folder_path, exist_ok=True)
|
||||||
|
print(f"Carpeta creada: {folder_path}")
|
||||||
|
|
||||||
|
# Mover archivos al nuevo directorio
|
||||||
|
for file in sorted(group_files): # Ordenar los archivos por disco
|
||||||
|
shutil.move(os.path.join(base_path, file), folder_path)
|
||||||
|
print(f"Archivo movido: {file} → {folder_path}")
|
||||||
|
|
||||||
|
# Crear el archivo .m3u
|
||||||
|
m3u_path = os.path.join(folder_path, f'{base_name}.m3u')
|
||||||
|
with open(m3u_path, 'w') as m3u_file:
|
||||||
|
for file in sorted(group_files): # Asegura que estén en orden
|
||||||
|
m3u_file.write(file + '\n')
|
||||||
|
print(f"Archivo .m3u creado: {m3u_path}")
|
||||||
|
|
||||||
|
print("Organización y creación de listas .m3u completadas.")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Comprobamos que se pase un argumento con la ruta base
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print("Uso: python script.py <ruta_base>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Obtenemos la ruta base desde los argumentos
|
||||||
|
base_path = sys.argv[1]
|
||||||
|
|
||||||
|
# Verificamos que la ruta exista
|
||||||
|
if not os.path.exists(base_path):
|
||||||
|
print(f"Error: La ruta especificada '{base_path}' no existe.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
organize_files(base_path)
|
||||||
Reference in New Issue
Block a user