🧐 Better support using hashfiles
dl / lint (push) Successful in 1m7s Details
dl / docker (push) Successful in 2m37s Details

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 && \
apt-get install -y \

View File

@ -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)