dl/commands/retro.py

70 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
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
2024-02-23 00:39:20 +00:00
run(["has_files.sh", console.name, 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()
# 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)