Files
scripts/python/fbneo_roms_by_manufacturer.py
T

155 lines
5.5 KiB
Python

# Script para copiar roms a partir de un xml de fbneo
# Copia las roms por desarrollador y sin clones
# Por hacer:
# pasar por parametro si se quieren clones
import os
import shutil
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")
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("-o", "--output", help="Ruta donde depositar las roms", default="")
args = parser.parse_args()
# Importa el xml
if args.dat == None:
print("No se ha especificado un fichero .dat")
sys.exit(2)
if not os.path.isfile(args.dat):
print("No se encuentra el fichero .dat")
sys.exit(2)
print("Parsing {} file".format(args.dat))
file = minidom.parse(args.dat)
# Lista los desarrolladores
if args.list and args.manufacturer == None:
print("List of all manufacturers:")
manufacturers = []
games = file.getElementsByTagName("game")
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")
for game in games:
cloneof = game.getAttribute("cloneof")
isbios = game.getAttribute("isbios")
manufacturer = game.getElementsByTagName("manufacturer")[0]
description = game.getElementsByTagName("description")[0]
if (
manufacturer.firstChild.data == args.manufacturer
and not cloneof
and not isbios
):
print("%s" % (description.firstChild.data))
# Copia los juegos seleccionados
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))
notfound = []
ignored_games = []
games = file.getElementsByTagName("game")
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]
if (
manufacturer.firstChild.data == args.manufacturer
and not cloneof
and not isbios
):
for element in ignore_list:
if description.firstChild.data.find(element) != -1:
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:
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("\nIgnored 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)