import funcs, json, os, config

db, newCursor = config.gen_connection()

newCursor.execute("SELECT hash FROM media")
hashes = [hash[0] for hash in newCursor.fetchall()]

file = 'bunnyVideos.json'

data = json.loads(open(file).read())

for media in data:
    if media['imported'] == True:
        if os.path.exists(media['filepath']):
            print(f'File {media["filepath"]} does not exist. Skipping...')
            continue
    

countImported = 0
countSkipped = 0
for media in data:
    filepath = os.path.join('STREAM_VIDEOS_IMPORTED', media['guid'] + '.mp4')
    if media['imported'] == True:
        countImported += 1
        print('File already imported. Skipping...')
        continue

    countSkipped += 1
    
    if not os.path.exists(filepath):
        print(f'File {filepath} does not exist. Skipping...')
        continue
    
    hash = funcs.calculate_file_hash(filepath)
    
    if '67caa15e-390c-4223-b7b9-4d7842f3b443' in filepath:
        print(f'File {filepath} does not exist. Skipping...')
        continue
    
    if hash in hashes:
        print('Duplicate file detected. Removing...')
        
        
print(f'Imported: {countImported}')
print(f'Skipped: {countSkipped}')