from funcs import get_files, generate_phash
import os, config

    
db, cursor = config.gen_connection()
cursor.execute("SELECT phash FROM media WHERE phash IS NOT NULL;")
phashes = [x[0] for x in cursor.fetchall()]

files = get_files('images')
for item in files:
    phash = generate_phash(item)
    if phash in phashes:
        print(item)
        newpath = item.replace('images', 'duplicates')
        newdir = os.path.dirname(newpath)
        os.makedirs(newdir, exist_ok=True)
        os.rename(item, newpath)