34 lines
986 B
Python
34 lines
986 B
Python
"""Targeted content hashing for duplicate candidates."""
|
|
|
|
import hashlib
|
|
from pathlib import Path
|
|
from typing import Iterable
|
|
|
|
|
|
def sha256_file(path: Path, chunk_size: int = 1024 * 1024) -> str:
|
|
digest = hashlib.sha256()
|
|
with path.open("rb") as source:
|
|
for chunk in iter(lambda: source.read(chunk_size), b""):
|
|
digest.update(chunk)
|
|
return digest.hexdigest()
|
|
|
|
|
|
def compare_audio_files(paths: Iterable[Path]) -> dict:
|
|
paths = tuple(paths)
|
|
if len(paths) < 2:
|
|
raise ValueError("At least two files are required for comparison")
|
|
fingerprints = []
|
|
for path in paths:
|
|
fingerprints.append(
|
|
{
|
|
"path": str(path),
|
|
"size": path.stat().st_size,
|
|
"sha256": sha256_file(path),
|
|
}
|
|
)
|
|
identical = len({item["sha256"] for item in fingerprints}) == 1
|
|
return {
|
|
"status": "identical" if identical else "different",
|
|
"files": fingerprints,
|
|
}
|