dl/commands/retro.py
Michel Roux 0e645e463a
All checks were successful
dl / lint (push) Successful in 1m4s
dl / docker (push) Successful in 40s
💚 Fix flake8
2024-02-23 12:08:10 +01:00

70 lines
2.2 KiB
Python

#!/usr/bin/env python3
from argparse import ArgumentParser
from json import dump, load
from pathlib import Path
from subprocess import run
from requests import get
parser = ArgumentParser()
parser.add_argument("path", nargs="?", default=Path.cwd())
args = parser.parse_args()
path = Path(args.path)
if not path.is_dir():
print("Invalid path")
exit(1)
for console in path.iterdir():
if console.is_dir() and Path(console / "roms").is_dir():
# run has_files
run(["has_files.sh", f"{console.name}/roms", console.name, "true", path])
# download metadata
metadatas = get(
"https://raw.githubusercontent.com/linuxserver/emulatorjs/master/metadata/"
+ console.name
+ ".json"
).json()
# load json or create empty object
metafile = Path(path / "metadata" / f"{console.name}.json")
if metafile.is_file():
with open(metafile, "r") as file:
metas = load(file)
else:
metas = {}
# loop over roms without hashes
for rom in Path(path / "hashes" / console.name / "roms").iterdir():
if rom.is_file():
# get rom hash
with open(rom, "r") as file:
hash = file.read()
# skip if hash match
if hash in metadatas.keys():
continue
# get rom ref
for id, metadata in metadatas.items():
# skip if no name
if "name" not in metadata:
continue
rom_name = rom.name.split("(")[0].strip().lower()
meta_name = metadata["name"].split("(")[0].strip().lower()
if rom_name == meta_name and (
"ref" in metadata or "logo" in metadata
):
ref = metadata["ref"] if "ref" in metadata else id
print(f"Found {ref} for {rom.name}")
metas[hash] = {"ref": ref}
break
# write json
with open(metafile, "w") as file:
dump(metas, file)