import os, json, config

# Load the data
pins = json.load(open('db_pins.json', 'r'))
files = os.listdir('STORAGE_IMPORTED/')

db, cursor = config.gen_connection()

cursor.execute('SELECT hash FROM media WHERE hash IS NOT NULL;')
existing_hashes = [hash[0] for hash in cursor.fetchall()]

for pin in pins:
    if pin['hash'] in existing_hashes:
        print(f"Found {pin['hash']} in the imported folder.")
        pins.remove(pin)

alreadyImported = []
for pin in pins:
    filepath = pin['filepath']
    username = pin['title']
    filename = os.path.basename(filepath)
    
    if filename in files:
        print(f"Found {filename} in the imported folder.")
        alreadyImported.append(pins.pop(pins.index(pin)))
 
# Save to the file
json.dump(pins, open('db_pins.json', 'w'))
json.dump(alreadyImported, open('db_pins_imported.json', 'w'))