#!/usr/bin/env python3 from argparse import ArgumentParser from hashlib import file_digest 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(), required=True) args = parser.parse_args() path = Path(args.path[0]) if not path.is_dir(): print("Invalid path") exit(1) for console in path.iterdir(): # define paths paths = { "hashes": Path(path / "hashes" / console.name), "metadata": Path(path / "metadata" / f"{console.name}.json"), } if console.is_dir() and Path(console / "roms").is_dir(): # cleaning logos for logo in Path(console / "logos").iterdir(): logo.unlink() # cleaning videos for video in Path(console / "videos").iterdir(): video.unlink() # 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() # loop over roms without hashes for rom in Path(console / "roms").iterdir(): if rom.is_file() and not Path(paths["hashes"] / rom.name).is_file(): # get rom hash with open(rom, "rb") as file: hash = file_digest(file, "sha1").hexdigest().upper() # get rom ref for id in metadatas: if rom.name in metadatas[id]["name"]: ref = metadatas[id]["ref"] if "ref" in metadatas[id] else id break # load json or create empty object if paths["metadata"].is_file(): with open(paths["metadata"], "r") as file: meta = load(file) else: meta = {} # add rom to json meta[hash] = {"ref": ref} # write json with open(paths["metadata"], "w") as file: file.write(meta)