detecta extensions mal nombrades cbz/cbr
This commit is contained in:
@@ -1,40 +1,74 @@
|
|||||||
# main.py
|
# main.py
|
||||||
|
|
||||||
import sys
|
import argparse
|
||||||
from core.scanner import find_comic_files
|
from core.scanner import find_comic_files
|
||||||
from processors.validator import validate_comic
|
from processors.validator import validate_comic
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Gestor de colección de cómics CBR/CBZ"
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--ruta",
|
||||||
|
type=str,
|
||||||
|
required=True,
|
||||||
|
help="Ruta base donde buscar los cómics"
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--listar",
|
||||||
|
action="store_true",
|
||||||
|
help="Listar todos los archivos CBR/CBZ encontrados"
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--validar",
|
||||||
|
action="store_true",
|
||||||
|
help="Validar los archivos encontrados"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Aquí podremos añadir más opciones en el futuro:
|
||||||
|
# parser.add_argument("--convertir", action="store_true", help="Convertir CBR a CBZ")
|
||||||
|
# parser.add_argument("--organizar", action="store_true", help="Organizar la colección")
|
||||||
|
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) > 1:
|
args = parse_args()
|
||||||
base_path = sys.argv[1]
|
|
||||||
else:
|
|
||||||
base_path = input("Introduce la ruta de la colección: ").strip()
|
|
||||||
|
|
||||||
print(f"Buscando archivos CBR/CBZ en: {base_path}\n")
|
print(f"Buscando archivos CBR/CBZ en: {args.ruta}\n")
|
||||||
|
comic_files = find_comic_files(args.ruta)
|
||||||
comic_files = find_comic_files(base_path)
|
|
||||||
|
|
||||||
if not comic_files:
|
if not comic_files:
|
||||||
print("No se encontraron archivos .cbr o .cbz")
|
print("No se encontraron archivos .cbr o .cbz")
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f"Se encontraron {len(comic_files)} archivos.\n")
|
if args.listar:
|
||||||
|
print("Archivos encontrados:")
|
||||||
for f in comic_files:
|
for f in comic_files:
|
||||||
result = validate_comic(f)
|
print(f" - {f}")
|
||||||
print(result)
|
|
||||||
|
|
||||||
if result.errors:
|
|
||||||
print(" Errores:")
|
|
||||||
for e in result.errors:
|
|
||||||
print(f" - {e}")
|
|
||||||
|
|
||||||
if result.warnings:
|
|
||||||
print(" Avisos:")
|
|
||||||
for w in result.warnings:
|
|
||||||
print(f" - {w}")
|
|
||||||
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
if args.validar:
|
||||||
|
print("Validando archivos...\n")
|
||||||
|
for f in comic_files:
|
||||||
|
result = validate_comic(f)
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
if result.errors:
|
||||||
|
print(" Errores:")
|
||||||
|
for e in result.errors:
|
||||||
|
print(f" - {e}")
|
||||||
|
|
||||||
|
if result.warnings:
|
||||||
|
print(" Avisos:")
|
||||||
|
for w in result.warnings:
|
||||||
|
print(f" - {w}")
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
+47
-16
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import zipfile
|
import zipfile
|
||||||
import rarfile # Necesita: pip install rarfile
|
import rarfile
|
||||||
|
|
||||||
IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp"}
|
IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp"}
|
||||||
TRASH_FILES = {"thumbs.db", ".ds_store"}
|
TRASH_FILES = {"thumbs.db", ".ds_store"}
|
||||||
@@ -14,6 +14,7 @@ class ValidationResult:
|
|||||||
self.errors = []
|
self.errors = []
|
||||||
self.warnings = []
|
self.warnings = []
|
||||||
self.images = []
|
self.images = []
|
||||||
|
self.real_format = None # "rar", "zip", None
|
||||||
|
|
||||||
def add_error(self, msg):
|
def add_error(self, msg):
|
||||||
self.is_valid = False
|
self.is_valid = False
|
||||||
@@ -27,31 +28,61 @@ class ValidationResult:
|
|||||||
return f"[{status}] {self.path}"
|
return f"[{status}] {self.path}"
|
||||||
|
|
||||||
|
|
||||||
|
def try_open_rar(path):
|
||||||
|
try:
|
||||||
|
archive = rarfile.RarFile(path, "r")
|
||||||
|
archive.namelist() # fuerza lectura
|
||||||
|
return archive
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def try_open_zip(path):
|
||||||
|
try:
|
||||||
|
archive = zipfile.ZipFile(path, "r")
|
||||||
|
archive.namelist()
|
||||||
|
return archive
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def validate_comic(path):
|
def validate_comic(path):
|
||||||
"""
|
|
||||||
Valida un archivo CBR/CBZ y devuelve un ValidationResult.
|
|
||||||
"""
|
|
||||||
result = ValidationResult(path)
|
result = ValidationResult(path)
|
||||||
ext = os.path.splitext(path)[1].lower()
|
ext = os.path.splitext(path)[1].lower()
|
||||||
|
|
||||||
try:
|
archive = None
|
||||||
if ext == ".cbz":
|
|
||||||
archive = zipfile.ZipFile(path, "r")
|
|
||||||
file_list = archive.namelist()
|
|
||||||
|
|
||||||
elif ext == ".cbr":
|
|
||||||
archive = rarfile.RarFile(path, "r")
|
|
||||||
file_list = archive.namelist()
|
|
||||||
|
|
||||||
|
# 1) Intentar abrir como RAR
|
||||||
|
if ext == ".cbr":
|
||||||
|
archive = try_open_rar(path)
|
||||||
|
if archive:
|
||||||
|
result.real_format = "rar"
|
||||||
else:
|
else:
|
||||||
result.add_error("Extensión no reconocida")
|
# 2) Intentar abrir como ZIP (CBZ renombrado)
|
||||||
|
archive = try_open_zip(path)
|
||||||
|
if archive:
|
||||||
|
result.real_format = "zip"
|
||||||
|
result.add_warning("El archivo tiene extensión .cbr pero es un ZIP (CBZ renombrado)")
|
||||||
|
else:
|
||||||
|
result.add_error("No se pudo abrir como RAR ni como ZIP")
|
||||||
|
return result
|
||||||
|
|
||||||
|
# 3) Si es CBZ, abrir como ZIP
|
||||||
|
elif ext == ".cbz":
|
||||||
|
archive = try_open_zip(path)
|
||||||
|
if archive:
|
||||||
|
result.real_format = "zip"
|
||||||
|
else:
|
||||||
|
result.add_error("No se pudo abrir el archivo ZIP")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
except Exception as e:
|
else:
|
||||||
result.add_error(f"No se pudo abrir el archivo: {e}")
|
result.add_error("Extensión no reconocida")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# Comprobar imágenes
|
# 4) Validar contenido
|
||||||
|
file_list = archive.namelist()
|
||||||
|
|
||||||
for f in file_list:
|
for f in file_list:
|
||||||
name = f.lower()
|
name = f.lower()
|
||||||
_, fext = os.path.splitext(name)
|
_, fext = os.path.splitext(name)
|
||||||
|
|||||||
Reference in New Issue
Block a user