dl/commands/retro.py

66 lines
2.0 KiB
Python
Raw Normal View History

2024-02-23 00:33:38 +00:00
#!/usr/bin/env python3
from argparse import ArgumentParser
from json import load
from pathlib import Path
from subprocess import run
from requests import get
parser = ArgumentParser()
2024-02-23 10:39:06 +00:00
parser.add_argument("path", nargs="?", default=Path.cwd())
2024-02-23 00:33:38 +00:00
args = parser.parse_args()
2024-02-23 10:39:06 +00:00
path = Path(args.path)
2024-02-23 00:33:38 +00:00
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
2024-02-23 10:54:11 +00:00
run(["has_files.sh", f"{console.name}/roms", console.name, "true", path])
2024-02-23 00:33:38 +00:00
# download metadata
metadatas = get(
2024-02-23 00:37:08 +00:00
"https://raw.githubusercontent.com/linuxserver/emulatorjs/master/metadata/"
+ console.name
+ ".json"
2024-02-23 00:33:38 +00:00
).json()
2024-02-23 10:04:40 +00:00
# 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 = {}
2024-02-23 00:33:38 +00:00
# loop over roms without hashes
2024-02-23 10:04:40 +00:00
for rom in Path(path / "hashes" / console.name / "roms").iterdir():
if rom.is_file():
2024-02-23 00:33:38 +00:00
# get rom hash
2024-02-23 10:04:40 +00:00
with open(rom, "r") as file:
hash = file.read()
# skip if hash match
if hash in metadatas.keys():
continue
2024-02-23 00:33:38 +00:00
# get rom ref
2024-02-23 10:04:40 +00:00
for id, metadata in metadatas.items():
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
2024-02-23 01:17:02 +00:00
):
2024-02-23 10:04:40 +00:00
ref = metadata["ref"] if "ref" in metadata else id
2024-02-23 01:17:02 +00:00
print(f"Found {ref} for {rom}")
2024-02-23 10:04:40 +00:00
metas[hash] = {"ref": ref}
2024-02-23 01:17:02 +00:00
break
2024-02-23 10:04:40 +00:00
# write json
with open(metafile, "w") as file:
file.write(metas)