#!/usr/bin/python

import os
import video_thumb
import video_info
import config
import sys

config.init()

dicter = {}
toremove = []

for root, dirs, files in os.walk( os.path.join( config.get_media_dir(), "TV" ) ):
	for filename in files:
		if( video_info.is_video( filename ) ):
			name = os.path.join( root, filename )
			result = video_thumb.get_thumb( name, create=False )
			dicter[ result ] = True
for root, dirs, files in os.walk( os.path.join( config.get_media_dir(), "Movies" ) ):
	for filename in files:
		if( video_info.is_video( filename ) ):
			name = os.path.join( root, filename )
			result = video_thumb.get_thumb( name, create=False )
			dicter[ result ] = True
for root, dirs, files in os.walk( '/var/www/thumbs' ):
	for filename in files:
		if( filename.endswith( '.png' ) ):
			name = os.path.join( root, filename )
			if( filename not in dicter ):
				toremove.append( name )
print( len( dicter.keys() ) )
print( len( toremove ) )

if( len( sys.argv ) > 1 and len( dicter.keys() ) > 0 ):
    print( 'Removing {} files'.format( len( toremove ) ) )
    for name in toremove:
        os.remove( name )
else:
    print( 'Dry run' )

