diff --git a/python/fbneo_roms_by_manufacturer.py b/python/fbneo_roms_by_manufacturer.py index fa5132a..dd9642d 100644 --- a/python/fbneo_roms_by_manufacturer.py +++ b/python/fbneo_roms_by_manufacturer.py @@ -10,17 +10,51 @@ import sys import argparse from xml.dom import minidom -# Inicialización de las opciones -ignore_list = ["DECO Cassette"] -parser = argparse.ArgumentParser() -parser.add_argument("-l", "--list", help="Muestra la lista de desarrolladores o de juegos", action="store_true") -parser.add_argument("-c", "--copy", help="Copia las roms", action="store_true") -parser.add_argument("-s", "--sort", help="Ordena los resultados", action="store_true") +# Elimina los caracteres ilegales de la cadena de texto +def normalize_path(path): + illegal_chars = ["<", ">", ":", '"', "/", "\\", "|", "?", "*"] + replace_with = "_" + for char in illegal_chars: + path = path.replace(char, replace_with) + return path + + +# Inicialización de las opciones +ignore_list = ["DECO Cassette", "Bootleg", "prototype", "Quiz", "Mahjong", "Demo"] + +# Obtiene los argumentos +parser = argparse.ArgumentParser( + description="Gestiona las roms de fbneo por desarrolladores a partir de un fichero .dat" +) +group = parser.add_mutually_exclusive_group() +group.add_argument( + "-l", + "--list", + help="Muestra la lista de desarrolladores o de juegos", + action="store_true", +) +group.add_argument("-c", "--copy", help="Copia las roms", action="store_true") +parser.add_argument( + "-s", + "--sort", + help="Ordena los resultados por carpetas de desarrollador al copiar todas las roms", + action="store_true", +) +parser.add_argument( + "-cf", + "--create_folder", + help="Copia las roms dentro de la carpeta del desarrollador", + action="store_true", +) parser.add_argument("-m", "--manufacturer", help="Selecciona un desarrollador") -parser.add_argument("-d", "--dat", help="Ruta del fichero .dat con información de las roms") -parser.add_argument("-i", "--input", help="Ruta donde se encuentran las roms", default="") +parser.add_argument( + "-d", "--dat", help="Ruta del fichero .dat con información de las roms" +) +parser.add_argument( + "-i", "--input", help="Ruta donde se encuentran las roms", default="" +) parser.add_argument("-o", "--output", help="Ruta donde depositar las roms", default="") args = parser.parse_args() @@ -44,18 +78,21 @@ if args.list and args.manufacturer == None: for game in games: manufacturer = game.getElementsByTagName("manufacturer")[0] manufacturers.append(manufacturer.firstChild.data) + # Elimina los duplicados manufacturers = list(dict.fromkeys(manufacturers)) + # Ordena la lista manufacturers.sort() + # Imprime la lista for i in manufacturers: print(i) # Lista los juegos de un desarrollador if args.list and args.manufacturer != None: - print("List of all {} games".format(args.manufacturer)) games = file.getElementsByTagName("game") + print("List of all", args.manufacturer, "games:", len(games)) for game in games: cloneof = game.getAttribute("cloneof") isbios = game.getAttribute("isbios") @@ -66,19 +103,25 @@ if args.list and args.manufacturer != None: and not cloneof and not isbios ): - print("%s" % (description.firstChild.data)) + print(description.firstChild.data) -# Copia los juegos seleccionados +# Copia los juegos del desarrollador seleccionado if ( args.copy and os.path.isdir(args.input) and os.path.isdir(args.output) and args.manufacturer != None ): - print("Copying all {} games".format(args.manufacturer)) + # Comprueba si ha de crear la carpeta con el nombre del desarrollador + if args.create_folder: + normalized_manufacturer = normalize_path(args.manufacturer) + os.mkdir(os.path.join(args.output, args.manufacturer)) + notfound = [] ignored_games = [] + copied_games = [] games = file.getElementsByTagName("game") + print("Copying all", args.manufacturer, "games:", len(games)) for game in games: isignored = False name = game.getAttribute("name") + ".zip" @@ -86,15 +129,88 @@ if ( isbios = game.getAttribute("isbios") manufacturer = game.getElementsByTagName("manufacturer")[0] description = game.getElementsByTagName("description")[0] + driver = game.getElementsByTagName("driver") + status = ( + game.getElementsByTagName("driver")[0].getAttribute("status") + if driver + else "" + ) if ( manufacturer.firstChild.data == args.manufacturer and not cloneof and not isbios + and status == "good" ): - for element in ignore_list: - if description.firstChild.data.find(element) != -1: + for ignore_element in ignore_list: + if ignore_element.casefold() in description.firstChild.data.casefold(): ignored_games.append(description.firstChild.data) isignored = True + + if not isignored: + src = os.path.join(args.input, name) + if args.sort: + x = manufacturer.firstChild.data + x = x.replace(r"/", r"-") + dst = os.path.join(args.output, x, name) + if not os.path.isdir(os.path.join(args.output, x)): + os.mkdir(os.path.join(args.output, x)) + else: + if args.create_folder: + dst = os.path.join(args.output, normalized_manufacturer, name) + else: + dst = os.path.join(args.output, name) + + if os.path.isfile(src): + shutil.copyfile(src, dst) + copied_games.append(description.firstChild.data) + print(description.firstChild.data) + else: + notfound.append(description.firstChild.data) + + print("\nMissing games:", len(notfound)) + for game in notfound: + print(game) + + print("\nIgnored games:", len(ignored_games)) + for game in ignored_games: + print(game) + + print("\nCopied games: ", len(copied_games)) + print("Missing games:", len(notfound)) + print("Ignored games:", len(ignored_games)) + +# Copia todos los juegos +if ( + args.copy + and os.path.isdir(args.input) + and os.path.isdir(args.output) + and args.manufacturer == None +): + notfound = [] + ignored_games = [] + copied_games = [] + games = file.getElementsByTagName("game") + print("Copying all games:", len(games)) + for game in games: + isignored = False + name = game.getAttribute("name") + ".zip" + cloneof = game.getAttribute("cloneof") + isbios = game.getAttribute("isbios") + manufacturer = game.getElementsByTagName("manufacturer")[0] + description = game.getElementsByTagName("description")[0] + driver = game.getElementsByTagName("driver") + status = ( + game.getElementsByTagName("driver")[0].getAttribute("status") + if driver + else "" + ) + + if not cloneof and not isbios and status == "good": + for ignore_element in ignore_list: + if ignore_element.casefold() in description.firstChild.data.casefold(): + ignored_games.append(description.firstChild.data) + isignored = True + if not isignored: src = os.path.join(args.input, name) if args.sort: @@ -105,50 +221,22 @@ if ( os.mkdir(os.path.join(args.output, x)) else: dst = os.path.join(args.output, name) + if os.path.isfile(src): shutil.copyfile(src, dst) - print("%s" % (description.firstChild.data)) + copied_games.append(description.firstChild.data) + print(description.firstChild.data) else: notfound.append(description.firstChild.data) - print("\nMissing games:") + + print("\nMissing games:", len(notfound)) for game in notfound: print(game) - print("\nIgnored games:") + print("\nIgnored games:", len(ignored_games)) for game in ignored_games: print(game) -# Copia todos los juegos -if ( - args.copy - and os.path.isdir(args.input) - and os.path.isdir(args.output) - and args.manufacturer == None -): - print("Copying all games") - notfound = [] - games = file.getElementsByTagName("game") - for game in games: - name = game.getAttribute("name") + ".zip" - cloneof = game.getAttribute("cloneof") - isbios = game.getAttribute("isbios") - manufacturer = game.getElementsByTagName("manufacturer")[0] - description = game.getElementsByTagName("description")[0] - if not cloneof and not isbios: - src = os.path.join(args.input, name) - if args.sort: - x = manufacturer.firstChild.data - x = x.replace(r"/", r"-") - dst = os.path.join(args.output, x, name) - if not os.path.isdir(os.path.join(args.output, x)): - os.mkdir(os.path.join(args.output, x)) - else: - dst = os.path.join(args.output, name) - if os.path.isfile(src): - shutil.copyfile(src, dst) - print("%s" % (description.firstChild.data)) - else: - notfound.append(description.firstChild.data) - print("\nMissing games:") - for game in notfound: - print(game) + print("\nCopied games: ", len(copied_games)) + print("Missing games:", len(notfound)) + print("Ignored games:", len(ignored_games))