🧐 Better support using hashfiles
All checks were successful
dl / lint (push) Successful in 1m7s
dl / docker (push) Successful in 2m37s

This commit is contained in:
Michel Roux 2024-02-23 11:04:40 +01:00
parent 1ef87c84da
commit 7c6adfc0f4
2 changed files with 30 additions and 40 deletions

View File

@ -1,4 +1,4 @@
FROM linuxserver/ffmpeg:6.1.1-cli-ls123 FROM linuxserver/ffmpeg:6.1.1-cli-ls124
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y \ apt-get install -y \

View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from argparse import ArgumentParser from argparse import ArgumentParser
from hashlib import file_digest
from json import load from json import load
from pathlib import Path from pathlib import Path
from subprocess import run from subprocess import run
@ -17,22 +16,7 @@ if not path.is_dir():
exit(1) exit(1)
for console in path.iterdir(): 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(): 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
run(["has_files.sh", console.name, console.name, "true", path]) run(["has_files.sh", console.name, console.name, "true", path])
@ -43,33 +27,39 @@ for console in path.iterdir():
+ ".json" + ".json"
).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 # loop over roms without hashes
for rom in Path(console / "roms").iterdir(): for rom in Path(path / "hashes" / console.name / "roms").iterdir():
if rom.is_file() and not Path(paths["hashes"] / rom.name).is_file(): if rom.is_file():
# get rom hash # get rom hash
with open(rom, "rb") as file: with open(rom, "r") as file:
hash = file_digest(file, "sha1").hexdigest().upper() hash = file.read()
# skip if hash match
if hash in metadatas.keys():
continue
# get rom ref # get rom ref
for id in metadatas: for id, metadata in metadatas.items():
if rom.name in metadatas[id]["name"] and ( rom_name = rom.name.split("(")[0].strip().lower()
"ref" in metadatas[id] or "logo" in metadatas[id] 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}") print(f"Found {ref} for {rom}")
metas[hash] = {"ref": ref}
# 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)
break break
# write json
with open(metafile, "w") as file:
file.write(metas)