mirror of
https://github.com/nikdoof/tvorganise.git
synced 2025-12-17 20:09:23 +00:00
Moved finding files into find_files function
This commit is contained in:
216
checkTvEps.py
216
checkTvEps.py
@@ -207,114 +207,126 @@ class Episode:
|
|||||||
#end Episode
|
#end Episode
|
||||||
|
|
||||||
|
|
||||||
###################################
|
def find_files(loc):
|
||||||
# Find all valid files
|
"""Recursivly finds files in a directory
|
||||||
###################################
|
"""
|
||||||
allfiles=[]
|
allfiles=[]
|
||||||
for (path,dirs,files) in os.walk(loc):
|
for (path,dirs,files) in os.walk(loc):
|
||||||
for file in files:
|
for file in files:
|
||||||
filename = os.path.join(os.path.abspath(path), file)
|
filename = os.path.join(os.path.abspath(path), file)
|
||||||
allfiles.append( str(filename) )
|
allfiles.append( str(filename) )
|
||||||
#end for f
|
return allfiles
|
||||||
|
#end find_files
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Strip out dotfiles/folder.jpg
|
def main():
|
||||||
decrappified = []
|
###################################
|
||||||
for current_file in allfiles:
|
# Find all valid files
|
||||||
current_file_path,current_file_name = os.path.split(current_file)
|
###################################
|
||||||
crappy = False
|
allfiles = find_files(loc)
|
||||||
for cur_decrap in tv_regex['decrappify']:
|
|
||||||
if cur_decrap.match(current_file_name):
|
# Strip out dotfiles/folder.jpg
|
||||||
crappy = True
|
decrappified = []
|
||||||
break
|
for current_file in allfiles:
|
||||||
if not crappy: decrappified.append(current_file)
|
current_file_path,current_file_name = os.path.split(current_file)
|
||||||
#end for current_file
|
crappy = False
|
||||||
allfiles = decrappified
|
for cur_decrap in tv_regex['decrappify']:
|
||||||
|
if cur_decrap.match(current_file_name):
|
||||||
# Warn if no files are found, then exit
|
crappy = True
|
||||||
if len(allfiles) == 0:
|
break
|
||||||
print colour('No files found','red')
|
if not crappy: decrappified.append(current_file)
|
||||||
sys.exit(1)
|
#end for current_file
|
||||||
|
allfiles = decrappified
|
||||||
###################################
|
|
||||||
# Validate filenames
|
# Warn if no files are found, then exit
|
||||||
###################################
|
if len(allfiles) == 0:
|
||||||
|
print colour('No files found','red')
|
||||||
valid = []
|
sys.exit(1)
|
||||||
invalid = []
|
|
||||||
|
###################################
|
||||||
for cur in allfiles:
|
# Validate filenames
|
||||||
cpath,cfile = os.path.split(cur)
|
###################################
|
||||||
cfile,cext = os.path.splitext(cfile)
|
|
||||||
|
valid = []
|
||||||
for cur_checker in tv_regex['valid_path']:
|
invalid = []
|
||||||
# Check if path is valid
|
|
||||||
check = cur_checker.findall(cpath)
|
for cur in allfiles:
|
||||||
if check:
|
cpath,cfile = os.path.split(cur)
|
||||||
break
|
cfile,cext = os.path.splitext(cfile)
|
||||||
else:
|
|
||||||
invalid.append({'errorno':3, 'path':cpath,'filename':cfile,
|
for cur_checker in tv_regex['valid_path']:
|
||||||
'cext':cext})
|
# Check if path is valid
|
||||||
#end for cur_checker
|
check = cur_checker.findall(cpath)
|
||||||
|
if check:
|
||||||
for cur_checker in tv_regex['with_ep_name']:
|
|
||||||
# Check if filename is valid (with ep name)
|
|
||||||
check = cur_checker.findall(cfile)
|
|
||||||
if check:
|
|
||||||
# Valid file name
|
|
||||||
valid.append({'path':cpath,'filename':cfile,
|
|
||||||
'cext':cext, 'match':check[0]})
|
|
||||||
break # Found valid episode, skip to the next one
|
|
||||||
#end if
|
|
||||||
else:
|
|
||||||
for cur_checker in tv_regex['missing_ep_name']:
|
|
||||||
# Check for valid name with missing episode name
|
|
||||||
check = cur_checker.findall(cfile)
|
|
||||||
if check:
|
|
||||||
invalid.append({'errorno':2, 'path':cpath,'filename':cfile,
|
|
||||||
'cext':cext})
|
|
||||||
break
|
break
|
||||||
#end if check
|
|
||||||
else:
|
else:
|
||||||
# Doesn't match valid-name or missing-ep-name regexs, it's invalid
|
invalid.append({'errorno':3, 'path':cpath,'filename':cfile,
|
||||||
invalid.append({'errorno':1, 'path':cpath,'filename':cfile,
|
|
||||||
'cext':cext})
|
'cext':cext})
|
||||||
#end for cur_checker
|
#end for cur_checker
|
||||||
#end for cur_checker
|
|
||||||
#end for
|
|
||||||
|
|
||||||
###################################
|
for cur_checker in tv_regex['with_ep_name']:
|
||||||
# Show invalid names
|
# Check if filename is valid (with ep name)
|
||||||
###################################
|
check = cur_checker.findall(cfile)
|
||||||
if len(invalid) > 0:
|
if check:
|
||||||
print colour('WARNING', 'red'), ': Invalid file-names found'
|
# Valid file name
|
||||||
|
valid.append({'path':cpath,'filename':cfile,
|
||||||
for errorno,errordescr in errors.items():
|
'cext':cext, 'match':check[0]})
|
||||||
errors = getError(invalid,errorno)
|
break # Found valid episode, skip to the next one
|
||||||
if len(errors) == 0: continue
|
#end if
|
||||||
|
else:
|
||||||
errormsg = "# %s (error code %d) #" % (errordescr, errorno)
|
for cur_checker in tv_regex['missing_ep_name']:
|
||||||
print "#"*len(errormsg)
|
# Check for valid name with missing episode name
|
||||||
print errormsg
|
check = cur_checker.findall(cfile)
|
||||||
print "#"*len(errormsg)
|
if check:
|
||||||
|
invalid.append({'errorno':2, 'path':cpath,'filename':cfile,
|
||||||
for c in errors:
|
'cext':cext})
|
||||||
cur_path = os.path.join(c['path'], c['filename'] + c['cext'])
|
break
|
||||||
print cur_path
|
#end if check
|
||||||
|
else:
|
||||||
|
# Doesn't match valid-name or missing-ep-name regexs, it's invalid
|
||||||
|
invalid.append({'errorno':1, 'path':cpath,'filename':cfile,
|
||||||
|
'cext':cext})
|
||||||
|
#end for cur_checker
|
||||||
|
#end for cur_checker
|
||||||
|
#end for
|
||||||
|
|
||||||
###################################
|
###################################
|
||||||
# Show valid names
|
# Show invalid names
|
||||||
###################################
|
###################################
|
||||||
if len(valid) > 0:
|
if len(invalid) > 0:
|
||||||
print colour('INFO','green'), ': Valid file-names found:'
|
print colour('WARNING', 'red'), ': Invalid file-names found'
|
||||||
allepisodes = ShowContainer()
|
|
||||||
|
|
||||||
for cur in valid:
|
for errorno,errordescr in errors.items():
|
||||||
if len(cur['match']) == 4:
|
errors = getError(invalid,errorno)
|
||||||
showname,seasno,epno,title = cur['match']
|
if len(errors) == 0: continue
|
||||||
elif len(cur['match']) == 3:
|
|
||||||
seasno = 1
|
|
||||||
showname,epno,title = cur['match']
|
|
||||||
|
|
||||||
allepisodes[showname][seasno][epno]['name'] = title
|
errormsg = "# %s (error code %d) #" % (errordescr, errorno)
|
||||||
#end for cur in valid
|
print "#"*len(errormsg)
|
||||||
print allepisodes
|
print errormsg
|
||||||
|
print "#"*len(errormsg)
|
||||||
|
|
||||||
|
for c in errors:
|
||||||
|
cur_path = os.path.join(c['path'], c['filename'] + c['cext'])
|
||||||
|
print cur_path
|
||||||
|
|
||||||
|
###################################
|
||||||
|
# Show valid names
|
||||||
|
###################################
|
||||||
|
if len(valid) > 0:
|
||||||
|
print colour('INFO','green'), ': Valid file-names found:'
|
||||||
|
allepisodes = ShowContainer()
|
||||||
|
|
||||||
|
for cur in valid:
|
||||||
|
if len(cur['match']) == 4:
|
||||||
|
showname,seasno,epno,title = cur['match']
|
||||||
|
elif len(cur['match']) == 3:
|
||||||
|
seasno = 1
|
||||||
|
showname,epno,title = cur['match']
|
||||||
|
|
||||||
|
allepisodes[showname][seasno][epno]['name'] = title
|
||||||
|
#end for cur in valid
|
||||||
|
print allepisodes
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user