#!/usr/bin/env python
#encoding: UTF-8
## [CONFIG] ############################################
duplicateFilePath = u"/home/dimzon/!test/duplicates.txt"
locationFileName = u"ku-ku.txt"
folderPathAll = u"/home/dimzon/!test/all"
folderPathByFirstLetter = u"/home/dimzon/!test/by_letter"
folderPathByYear = u"/home/dimzon/!test/by_year"
sourceFolders = [\
u"/home/dimzon/!test/drives/1", \
u"/home/dimzon/!test/drives/2", \
u"/home/dimzon/!test/drives/3" ]
## [/CONFIG] ###########################################
import os,re
def get_first_letter( fileName ):
ltr = fileName[0].upper()
if "0123456789.,!@#$_()[]{}-".count(ltr):
return "(0-9)"
return ltr
reYear = re.compile(r"(?<=\D)(?:19|20)\d{2}(?=\D)", re.UNICODE )
def get_years(fileName):
return reYear.findall( fileName )
def clean_symlinks( folderPath ):
for i in os.listdir( folderPath ):
j = os.path.join( folderPath, i )
if os.path.islink( j ):
os.unlink( j )
elif os.path.isdir( j ):
clean_symlinks( j )
if len( os.listdir( j ) ) == 0 :
print 11
os.rmdir( j )
print 'Processing...'
clean_symlinks( folderPathAll )
clean_symlinks( folderPathByFirstLetter )
clean_symlinks( folderPathByYear )
knownFilms = {}
for f in sourceFolders:
print f
for i in os.listdir( f ):
j = os.path.join( f, i )
if os.path.isdir( j ):
with open( os.path.join( j, locationFileName ) , 'w' ) as loc:
loc.write( j.encode('utf-8') )
key = i.upper( )
if key in knownFilms :
knownFilms[key][1].append( j )
else:
knownFilms[key]=( i.strip() , [ j ] )
with open( duplicateFilePath , 'w' ) as loc:
for i in knownFilms.values():
if len(i[1]) != 1 :
loc.write( ('### ' + i[0] + '\n\r').encode('utf-8') )
for j in i[1] :
loc.write( (j + '\n\r').encode('utf-8') )
loc.write( '\n\r\n\r' )
for i in knownFilms.values():
os.symlink( i[1][0] , os.path.join( folderPathAll , i[0] ) )
f = os.path.join( folderPathByFirstLetter , get_first_letter( i[0] ) )
if not os.path.exists( f ) :
os.mkdir( f )
os.symlink( i[1][0] , os.path.join( f , i[0] ) )
dt = get_years( i[0] )
for y in dt :
f = os.path.join( folderPathByYear , y )
if not os.path.exists( f ) :
os.mkdir( f )
os.symlink( i[1][0] , os.path.join( f , i[0] ) )
print 'Done!'