From 84c1495bc648bc8ade62c9dedae6aa6abfbc02bf Mon Sep 17 00:00:00 2001 From: Sergio Date: Tue, 12 Nov 2024 20:05:34 +0100 Subject: [PATCH] Ja separa els jocs anteriors a 1993 dels posteriors --- zxdb.py | 171 +++++++++++++++++++++++++++----------------------------- 1 file changed, 83 insertions(+), 88 deletions(-) diff --git a/zxdb.py b/zxdb.py index e839c16..17862ff 100644 --- a/zxdb.py +++ b/zxdb.py @@ -46,7 +46,7 @@ temp_file = r"/tmp/zxdb.download.tmp" should_clear_destination_path = True # Establece si se limpia primero la carpeta de destino wait = True # Establece una pausa aleatoria entre descargas min_wait = 2 # Segundos mínimos a esperar entre descargas -max_wait = min_wait + 1 # Segundos máximos a esperar entre descargas +max_wait = min_wait + 2 # Segundos máximos a esperar entre descargas elements = [] filetypes_on_root = [ "Tape image", @@ -251,31 +251,48 @@ def download_file(url, dest): # Descomprime los ficheros que coinciden con la lista de extensiones def unzip_file(src, dst): - # with zipfile.ZipFile(src, "r") as zip_ref: - # zip_ref.extractall(dst) archive = src directory = dst - extensions = (".z80", ".sna", ".tzx", ".tap", "dsk", ".trd", ".Z80", ".SNA", ".TZX", ".TAP", "DSK", ".TRD") - zip_file = zipfile.ZipFile(archive, "r") - [ - zip_file.extract(file, directory) - for file in zip_file.namelist() - if file.endswith(extensions) - ] - zip_file.close() + extensions = (".z80", ".sna", ".tzx", ".tap", ".dsk", ".trd") + + try: + with zipfile.ZipFile(archive, "r") as zip_file: + for file in zip_file.namelist(): + if file.lower().endswith(extensions): + zip_file.extract(file, directory) + # logging.info(f"Archivo {file} extraído a {directory}") + except zipfile.BadZipFile: + logging.error("El archivo ZIP está corrupto.") + except FileNotFoundError: + logging.error("El archivo ZIP no se encontró.") + except Exception as e: + logging.error(f"Ocurrió un error: {e}") + # Obtiene los ficheros de la consulta desde internet o desde la caché # y los deposita en la carpeta destino, descomprimiendo los archivos necesarios def get_files(): + # Configuración del logger + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + # Variables para la presentación en pantalla de la descarga current_file = 0 total_files = len(elements) total_files_width = len(str(total_files)) last_game_folder = "" + for element in elements: # Carpeta del juego en destino y en caché + release_year = element["release_year"] + + if release_year == "none" or release_year is None or release_year >= 1993: + classification_folder = "modern/" + element["root_folder"] + else: + classification_folder = "classics/" + element["root_folder"] + + game_folder = element["root_folder"] - destination_folder = os.path.join(destination_path, element["root_folder"]) + destination_folder = os.path.join(destination_path, classification_folder) destination_subfolder = os.path.join(destination_folder, element["subfolder"]) cache_folder = os.path.join(cache_path, element["root_folder"]) cache_subfolder = os.path.join(cache_folder, element["subfolder"]) @@ -285,96 +302,74 @@ def get_files(): cache_file = os.path.join(cache_subfolder, element["file_name"]) # Actualiza las variables de presentación - current_file = current_file + 1 + current_file += 1 if game_folder != last_game_folder: print("\n{}".format(game_folder)) last_game_folder = game_folder - #print( - # "(WORKING : {} ({})".format( - # element["file_name"], - # element["filetype"] - # ) - #) - - # Comprueba si ya existe el fichero a descargar - if not os.path.isfile(destination_file) and ( - not os.path.isfile( - os.path.join(destination_subfolder, element["non_zip_file_name"]) - ) - ): - # Comprueba si ya existe el fichero en la cache - if os.path.isfile(cache_file): - # Si encuentra el fichero en cache, crea las carpetas de destino y lo copia o lo extrae - if not os.path.isdir(destination_folder): - os.mkdir(destination_folder) - if not os.path.isdir(destination_subfolder): - os.mkdir(destination_subfolder) - if cache_file.endswith(".zip") and element["subfolder"] == "": - unzip_file(cache_file, destination_subfolder) + try: + # Comprueba si ya existe el fichero a descargar + if not os.path.isfile(destination_file) and not os.path.isfile(os.path.join(destination_subfolder, element["non_zip_file_name"])): + # Comprueba si ya existe el fichero en la cache + if os.path.isfile(cache_file): + # Si encuentra el fichero en cache, crea las carpetas de destino y lo copia o lo extrae + os.makedirs(destination_subfolder, exist_ok=True) + if cache_file.endswith(".zip") and element["subfolder"] == "": + unzip_file(cache_file, destination_subfolder) + else: + shutil.copyfile(cache_file, destination_file) + print( + "({:{width}} / {}) : cached : {} ({})".format( + current_file, + total_files, + element["file_name"], + element["filetype"], + width=total_files_width, + ) + ) + # El fichero no está en la cache else: - shutil.copyfile(cache_file, destination_file) - print( - "({:{width}} / {}) : cached : {} ({})".format( - current_file, - total_files, - element["file_name"], - element["filetype"], - width=total_files_width, + status = "not found " + if download_file(element["url"], temp_file): + status = "downloaded" + if os.path.isfile(temp_file): + # Copia el fichero temporal a la cache + os.makedirs(cache_subfolder, exist_ok=True) + shutil.copyfile(temp_file, cache_file) + os.remove(temp_file) + # Copia el fichero de la cache al destino + if os.path.isfile(cache_file): + os.makedirs(destination_subfolder, exist_ok=True) + if cache_file.endswith(".zip") and element["subfolder"] == "": + unzip_file(cache_file, destination_folder) + else: + shutil.copyfile(cache_file, destination_file) + print( + "({:{width}} / {}) : {} : {} ({})".format( + current_file, + total_files, + status, + element["file_name"], + element["filetype"], + width=total_files_width, + ) ) - ) - # El fichero no está en la cache + if wait: + time.sleep(random.randint(min_wait, max_wait)) + # El fichero ya existe en el destino else: - status = "not found " - if download_file(element["url"], temp_file): - status = "downloaded" - if os.path.isfile(temp_file): - # Copia el fichero temnporal a la cache - if not os.path.isdir(cache_folder): - os.mkdir(cache_folder) - if not os.path.isdir(cache_subfolder): - os.mkdir(cache_subfolder) - shutil.copyfile(temp_file, cache_file) - os.remove(temp_file) - # Copia el fichero de la cache al destino - if os.path.isfile(cache_file): - if not os.path.isdir(destination_folder): - os.mkdir(destination_folder) - if not os.path.isdir(destination_subfolder): - os.mkdir(destination_subfolder) - if ( - cache_file.endswith(".zip") - and element["subfolder"] == "" - ): - unzip_file(cache_file, destination_folder) - else: - shutil.copyfile(cache_file, destination_file) print( - "({:{width}} / {}) : {} : {} ({})".format( + "({:{width}} / {}) : skipping : {} ({})".format( current_file, total_files, - status, element["file_name"], element["filetype"], width=total_files_width, ) ) - if wait: - time.sleep(random.randint(min_wait, max_wait)) - - # El fichero ya existe en el destino - else: - print( - "({:{width}} / {}) : skipping : {} ({})".format( - current_file, - total_files, - element["file_name"], - element["filetype"], - width=total_files_width, - ) - ) - + except Exception as e: + logging.error(f"Error al procesar el fichero {element['file_name']}: {e}") # Elimina los caracteres ilegales de la cadena de texto def normalize_path(path): @@ -424,8 +419,8 @@ def main(): connect(query_index=0) process_elements() print_elements(mode=1) - #clear_destination_folder() - #get_files() + clear_destination_folder() + get_files() if __name__ == "__main__": main()