validator.py
This commit is contained in:
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
from core.scanner import find_comic_files
|
from core.scanner import find_comic_files
|
||||||
|
from processors.validator import validate_comic
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Obtener ruta desde argumentos o pedirla
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
base_path = sys.argv[1]
|
base_path = sys.argv[1]
|
||||||
else:
|
else:
|
||||||
@@ -18,9 +18,23 @@ def main():
|
|||||||
print("No se encontraron archivos .cbr o .cbz")
|
print("No se encontraron archivos .cbr o .cbz")
|
||||||
return
|
return
|
||||||
|
|
||||||
print("Archivos encontrados:")
|
print(f"Se encontraron {len(comic_files)} archivos.\n")
|
||||||
|
|
||||||
for f in comic_files:
|
for f in comic_files:
|
||||||
print(f" - {f}")
|
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()
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
# processors/validator.py
|
||||||
|
|
||||||
|
import os
|
||||||
|
import zipfile
|
||||||
|
import rarfile # Necesita: pip install rarfile
|
||||||
|
|
||||||
|
IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp"}
|
||||||
|
TRASH_FILES = {"thumbs.db", ".ds_store"}
|
||||||
|
|
||||||
|
class ValidationResult:
|
||||||
|
def __init__(self, path):
|
||||||
|
self.path = path
|
||||||
|
self.is_valid = True
|
||||||
|
self.errors = []
|
||||||
|
self.warnings = []
|
||||||
|
self.images = []
|
||||||
|
|
||||||
|
def add_error(self, msg):
|
||||||
|
self.is_valid = False
|
||||||
|
self.errors.append(msg)
|
||||||
|
|
||||||
|
def add_warning(self, msg):
|
||||||
|
self.warnings.append(msg)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
status = "OK" if self.is_valid else "ERROR"
|
||||||
|
return f"[{status}] {self.path}"
|
||||||
|
|
||||||
|
|
||||||
|
def validate_comic(path):
|
||||||
|
"""
|
||||||
|
Valida un archivo CBR/CBZ y devuelve un ValidationResult.
|
||||||
|
"""
|
||||||
|
result = ValidationResult(path)
|
||||||
|
ext = os.path.splitext(path)[1].lower()
|
||||||
|
|
||||||
|
try:
|
||||||
|
if ext == ".cbz":
|
||||||
|
archive = zipfile.ZipFile(path, "r")
|
||||||
|
file_list = archive.namelist()
|
||||||
|
|
||||||
|
elif ext == ".cbr":
|
||||||
|
archive = rarfile.RarFile(path, "r")
|
||||||
|
file_list = archive.namelist()
|
||||||
|
|
||||||
|
else:
|
||||||
|
result.add_error("Extensión no reconocida")
|
||||||
|
return result
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
result.add_error(f"No se pudo abrir el archivo: {e}")
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Comprobar imágenes
|
||||||
|
for f in file_list:
|
||||||
|
name = f.lower()
|
||||||
|
_, fext = os.path.splitext(name)
|
||||||
|
|
||||||
|
if fext in IMAGE_EXTENSIONS:
|
||||||
|
result.images.append(f)
|
||||||
|
|
||||||
|
if os.path.basename(name) in TRASH_FILES:
|
||||||
|
result.add_warning(f"Archivo basura encontrado: {f}")
|
||||||
|
|
||||||
|
if not result.images:
|
||||||
|
result.add_error("No contiene imágenes válidas")
|
||||||
|
|
||||||
|
return result
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
rarfile
|
||||||
Reference in New Issue
Block a user