From 7c6adfc0f49a019310714c6220708d9380e2371e Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Fri, 23 Feb 2024 11:04:40 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=90=20Better=20support=20using=20hashf?= =?UTF-8?q?iles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- commands/retro.py | 68 ++++++++++++++++++++--------------------------- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/Dockerfile b/Dockerfile index 332507e..1c8fcba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM linuxserver/ffmpeg:6.1.1-cli-ls123 +FROM linuxserver/ffmpeg:6.1.1-cli-ls124 RUN apt-get update && \ apt-get install -y \ diff --git a/commands/retro.py b/commands/retro.py index 4bfe55d..a9c9da5 100644 --- a/commands/retro.py +++ b/commands/retro.py @@ -1,6 +1,5 @@ #!/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 @@ -17,22 +16,7 @@ if not path.is_dir(): 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(): - print(f"Unlink {logo}") - logo.unlink() - # cleaning videos - for video in Path(console / "videos").iterdir(): - print(f"Unlink {video}") - video.unlink() - # run has_files run(["has_files.sh", console.name, console.name, "true", path]) @@ -43,33 +27,39 @@ for console in path.iterdir(): + ".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(console / "roms").iterdir(): - if rom.is_file() and not Path(paths["hashes"] / rom.name).is_file(): + for rom in Path(path / "hashes" / console.name / "roms").iterdir(): + if rom.is_file(): # get rom hash - with open(rom, "rb") as file: - hash = file_digest(file, "sha1").hexdigest().upper() + with open(rom, "r") as file: + hash = file.read() + + # skip if hash match + if hash in metadatas.keys(): + continue # get rom ref - for id in metadatas: - if rom.name in metadatas[id]["name"] and ( - "ref" in metadatas[id] or "logo" in metadatas[id] + 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 = metadatas[id]["ref"] if "ref" in metadatas[id] else id + ref = metadata["ref"] if "ref" in metadata else id print(f"Found {ref} for {rom}") - - # 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) - + metas[hash] = {"ref": ref} break + + # write json + with open(metafile, "w") as file: + file.write(metas)