import os
import hashlib

# Directories
fucked_dir = 'tiktoks/fucked/aleksandra'
source_dir = 'tiktoks/waiting_for_process/aleksandraverse'

def hash_file(filepath):
    """Generate MD5 hash of a file."""
    hash_md5 = hashlib.md5()
    with open(filepath, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

def get_file_hashes(directory):
    """Generate a dictionary of file hashes for all files in a directory."""
    file_hashes = {}
    for root, _, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)
            file_hashes[file_path] = hash_file(file_path)
    return file_hashes

def files_are_identical(file1, file2):
    """Compare two files byte-by-byte."""
    with open(file1, "rb") as f1, open(file2, "rb") as f2:
        while True:
            chunk1 = f1.read(4096)
            chunk2 = f2.read(4096)
            if chunk1 != chunk2:
                return False
            if not chunk1:  # End of file
                return True

def remove_duplicates(fucked_dir, source_files):
    """Remove files in 'fucked' that are identical to those in 'source_files'."""
    for root, _, files in os.walk(fucked_dir):
        for file in files:
            file_path = os.path.join(root, file)
            for source_file in source_files:
                if files_are_identical(file_path, source_file):
                    print(f"Duplicate found. Removing: {file_path}")
                    os.remove(file_path)
                    break

def main():
    print("Scanning source directory for hashes...")
    source_hashes = get_file_hashes(source_dir)
    
    print("Scanning 'fucked' directory for duplicates...")
    remove_duplicates(fucked_dir, source_hashes)
    
    print("Cleanup complete.")

if __name__ == "__main__":
    main()