Añadido resumen final de descargas al terminar o interrumpir
Muestra contadores de ficheros por estado (caché, descargados, no encontrados, omitidos) tanto al finalizar normalmente como al interrumpir con Ctrl+C. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
stats = {}
|
||||||
try:
|
try:
|
||||||
if run_setup() != 0:
|
if run_setup() != 0:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -18,9 +19,10 @@ def main():
|
|||||||
elements = organizer.process_elements(elements)
|
elements = organizer.process_elements(elements)
|
||||||
filesystem.print_elements(elements, mode=1)
|
filesystem.print_elements(elements, mode=1)
|
||||||
filesystem.clear_destination_folder()
|
filesystem.clear_destination_folder()
|
||||||
downloader.get_files(elements)
|
stats = downloader.get_files(elements)
|
||||||
filesystem.remove_empty_directories(config.DESTINATION_PATH)
|
filesystem.remove_empty_directories(config.DESTINATION_PATH)
|
||||||
finally:
|
finally:
|
||||||
|
filesystem.print_summary(stats)
|
||||||
stop_container(CONTAINER_NAME)
|
stop_container(CONTAINER_NAME)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ def get_files(elements):
|
|||||||
Parámetros:
|
Parámetros:
|
||||||
elements (list): Lista de diccionarios con la información de cada fichero a procesar.
|
elements (list): Lista de diccionarios con la información de cada fichero a procesar.
|
||||||
"""
|
"""
|
||||||
|
stats = {"cached": 0, "downloaded": 0, "not_found": 0, "skipped": 0}
|
||||||
current_file = 0
|
current_file = 0
|
||||||
total_files = len(elements)
|
total_files = len(elements)
|
||||||
total_files_width = len(str(total_files))
|
total_files_width = len(str(total_files))
|
||||||
@@ -122,10 +123,12 @@ def get_files(elements):
|
|||||||
if os.path.isfile(cache_file):
|
if os.path.isfile(cache_file):
|
||||||
process_cache_file(cache_file, destination_subfolder, destination_file, element)
|
process_cache_file(cache_file, destination_subfolder, destination_file, element)
|
||||||
print_status(current_file, total_files, element, total_files_width, status="cached")
|
print_status(current_file, total_files, element, total_files_width, status="cached")
|
||||||
|
stats["cached"] += 1
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if download_file(element["url"], config.TEMP_FILE):
|
if download_file(element["url"], config.TEMP_FILE):
|
||||||
print_status(current_file, total_files, element, total_files_width, status="downloaded")
|
print_status(current_file, total_files, element, total_files_width, status="downloaded")
|
||||||
|
stats["downloaded"] += 1
|
||||||
if os.path.isfile(config.TEMP_FILE):
|
if os.path.isfile(config.TEMP_FILE):
|
||||||
os.makedirs(cache_subfolder, exist_ok=True)
|
os.makedirs(cache_subfolder, exist_ok=True)
|
||||||
shutil.move(config.TEMP_FILE, cache_file)
|
shutil.move(config.TEMP_FILE, cache_file)
|
||||||
@@ -133,12 +136,16 @@ def get_files(elements):
|
|||||||
process_cache_file(cache_file, destination_subfolder, destination_file, element)
|
process_cache_file(cache_file, destination_subfolder, destination_file, element)
|
||||||
else:
|
else:
|
||||||
print_status(current_file, total_files, element, total_files_width, status="not found")
|
print_status(current_file, total_files, element, total_files_width, status="not found")
|
||||||
|
stats["not_found"] += 1
|
||||||
|
|
||||||
if config.WAIT:
|
if config.WAIT:
|
||||||
time.sleep(random.randint(config.MIN_WAIT, config.MAX_WAIT))
|
time.sleep(random.randint(config.MIN_WAIT, config.MAX_WAIT))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print_status(current_file, total_files, element, total_files_width, status="skipping")
|
print_status(current_file, total_files, element, total_files_width, status="skipping")
|
||||||
|
stats["skipped"] += 1
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error al procesar el fichero {element['file_name']}: {e}")
|
logging.error(f"Error al procesar el fichero {element['file_name']}: {e}")
|
||||||
|
|
||||||
|
return stats
|
||||||
|
|||||||
@@ -72,6 +72,14 @@ def clear_destination_folder():
|
|||||||
logging.error(f'No se pudo eliminar {file_path}. Razón: {e}')
|
logging.error(f'No se pudo eliminar {file_path}. Razón: {e}')
|
||||||
|
|
||||||
|
|
||||||
|
def print_summary(stats: dict) -> None:
|
||||||
|
print("\n--- Resumen ---")
|
||||||
|
print(f" Caché: {stats.get('cached', 0)}")
|
||||||
|
print(f" Descargados: {stats.get('downloaded', 0)}")
|
||||||
|
print(f" No encontrados: {stats.get('not_found', 0)}")
|
||||||
|
print(f" Omitidos: {stats.get('skipped', 0)}")
|
||||||
|
|
||||||
|
|
||||||
def remove_empty_directories(path):
|
def remove_empty_directories(path):
|
||||||
"""
|
"""
|
||||||
Elimina los subdirectorios vacíos de forma recursiva.
|
Elimina los subdirectorios vacíos de forma recursiva.
|
||||||
|
|||||||
Reference in New Issue
Block a user