import config

altpins_db, altpins_cursor = config.altpins_gen_connection()
db, cursor = config.gen_connection()

altpins_cursor.execute("SELECT id, title, hash, url FROM pins WHERE hash IS NOT NULL;")
altpins_results = { (row[1], row[2]): (row[0], row[3]) for row in altpins_cursor.fetchall() }

cursor.execute("SELECT id, username, hash, media_url FROM media WHERE hash IS NOT NULL;")
media_results = { (row[1], row[2]): (row[0], row[3]) for row in cursor.fetchall() }

common_items = set(altpins_results.keys()) & set(media_results.keys())

for title, hash_value in common_items:
    altpins_id, altpins_url = altpins_results[(title, hash_value)]
    media_id, media_url = media_results[(title, hash_value)]

    print(f"Found a match for hash {hash_value} with title {title}")
    print(f"Altpins URL: {altpins_url}")
    print(f"Media URL: {media_url}")

    altpins_cursor.execute("DELETE FROM pins WHERE id = %s;", [altpins_id])
    altpins_db.commit()
    print(f"Deleted pin {altpins_id}. {altpins_cursor.rowcount} rows affected")