import os, json
from funcs import generate_phash

count = 0
cacheDir = '_sort'
dataPath = 'pins.json'

os.makedirs(cacheDir, exist_ok=True)

medias = json.load(open(dataPath))

for item in medias:
    count += 1
    if item['type'] == 'image':
        filepath = item['filepath']
        if 'phash' in item:
            print(f"Skipping {count}/{len(medias)}: already processed.")
            continue
        
        if not os.path.exists(filepath):
            print(f"File {filepath} does not exist, skipping.")
            continue
        phash = generate_phash(filepath)
        item['phash'] = phash
        print(f"Processed {count}/{len(medias)}: with pHash {phash}")
        
with open(dataPath, 'w') as f:
    json.dump(medias, f)