dl/commands/retro.py
Michel Roux fcce48de5a
All checks were successful
dl / lint (push) Successful in 1m37s
dl / docker (push) Successful in 56s
️ Don't delete old hashes
2024-02-24 23:01:37 +01:00

74 lines
2.3 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()
and list(Path(console / "roms").iterdir())
):
# run has_files
run(["has_files.sh", f"{console.name}/roms", console.name, "false", 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() and "Disc" not in rom.stem:
# 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.stem.split(".")[0].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.stem}")
metas[hash] = {"ref": ref}
break
# write json
with open(metafile, "w") as file:
dump(metas, file)