66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
#!/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()
|
|
parser.add_argument("path", nargs=1, default=Path.cwd())
|
|
args = parser.parse_args()
|
|
path = Path(args.path[0])
|
|
|
|
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", console.name, 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():
|
|
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}")
|
|
metas[hash] = {"ref": ref}
|
|
break
|
|
|
|
# write json
|
|
with open(metafile, "w") as file:
|
|
file.write(metas)
|