dl/commands/retro.py

72 lines
2.2 KiB
Python
Raw Normal View History

2024-02-23 00:33:38 +00:00
#!/usr/bin/env python3
from argparse import ArgumentParser
2024-02-23 11:08:10 +00:00
from json import dump, load
2024-02-23 00:33:38 +00:00
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():
2024-02-24 21:40:05 +00:00
if (
console.is_dir()
and Path(console / "roms").is_dir()
and list(Path(console / "roms").iterdir())
):
2024-02-23 00:33:38 +00:00
# run has_files
2024-02-24 22:01:37 +00:00
run(["has_files.sh", f"{console.name}/roms", console.name, "false", 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():
2024-02-23 14:04:46 +00:00
if rom.is_file() and "Disc" not in rom.stem:
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():
2024-02-23 10:58:44 +00:00
# skip if no name
if "name" not in metadata:
continue
2024-02-23 14:10:48 +00:00
rom_name = rom.stem.split(".")[0].split("(")[0].strip().lower()
2024-02-23 10:04:40 +00:00
meta_name = metadata["name"].split("(")[0].strip().lower()
if rom_name == meta_name and ("ref" in metadata or "logo" in metadata):
2024-02-23 10:04:40 +00:00
ref = metadata["ref"] if "ref" in metadata else id
2024-02-23 14:04:46 +00:00
print(f"Found {ref} for {rom.stem}")
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:
2024-02-23 11:04:40 +00:00
dump(metas, file)