2010-03-19, 22:12
I don't know if this is already been done by someone else..but..
I've modified a script found around the internet to search for orphaned .nfo, fanart, thumb, poster and delete them when the original movie file has been deleted from the filesystem...
The script is easily customizable to change initial directory to scan, file extensions to match etc..
just copy-paste the code in Cleanup.py file
and here the XBMC GUI dialog to perform the cleanup, save to default.py (the script assumes Cleanup.py is located in /usr/share/xbmc/script/Cleanup directory on linux (that is special://xbmc/scripts/Cleanup/Cleanup.py)
but you can easily change it to whatever location you wrote the Cleanup script file:
I've modified a script found around the internet to search for orphaned .nfo, fanart, thumb, poster and delete them when the original movie file has been deleted from the filesystem...
The script is easily customizable to change initial directory to scan, file extensions to match etc..
just copy-paste the code in Cleanup.py file
Code:
#!/usr/bin/env python
#####################
### Requirements ###
#####################
# python
# Have not tested on windows machine but should run fine. If you have any
# issues with this or it runs successfully please let me know.
import re,sys,os
################
### Settings ###
################
#files home location
FILESHOME="/home/xbmc/Videos/"
#this is NOT case sensitive sample will ignore all variants of sample will block sAmPLE
#sorry you need to be careful here if you put in just for instance e it will ignore all files and directories with an e so make unique!
#EXECPTIONLIST=['documentaries','upcoming.movies','sample','/musicvideos','mummy','2lions-Team','DVDr-sailo','narnia','THE_DARK_KNIGHT','incomplete/']
EXECPTIONLIST=['Bodybuilding','CARTOONS','Downloads','VARIOUS']
EXCLUDEINFOLIST=['tvshow.nfo', 'tvshow.info']
CLEAN_EXTENSIONS=['.nfo','.info','.tbn','-fanart.jpg']
#during our file system search we will search for all files with these extensions and see if file exists in database
#VALIDEXTENSIONS= ["MKV", "AVI", "FLV", "WMV", "AAF", "3GP", "ASF", "CAM", "M2V", "SWF", "FLA", "M4V", "MOV", "MPEG", "OGG", "RM", "MP4","MPG","IFO","VOB","IMG","ISO"]
VALIDNFO= ["NFO", "INFO"]
VALIDEXTENSIONS= ["MKV", "AVI", "FLV", "WMV", "MOV", "MPEG", "MP4","MPG"]
####################
### functions ###
####################
def fileFinder(dir_name, subdir, extArray=""): #directory name to start, true/false if you want recursive, pass an array of extension if you wish to filter
fileList = []
try:
for file in os.listdir(dir_name):
try:
dirfile = os.path.join(dir_name, file)
if os.path.isfile(dirfile):
if (extArray==""):
fileList.append(dirfile)
else:
if os.path.splitext(dirfile)[1][1:].upper() in extArray:
fileList.append(dirfile)
elif os.path.isdir(dirfile) and subdir:
fileList.extend(fileFinder(dirfile, subdir, extArray))
except UnicodeDecodeError:
#need to figure out how to resolve this encoding error!
print "Internal Error! Unicode error found with: "+file;
pass;
return fileList
except OSError:
return "0"
####################
### working code ###
####################
debug=False;
nfofiles=fileFinder(FILESHOME,True,VALIDNFO)
files=fileFinder(FILESHOME,True,VALIDEXTENSIONS)
foundCnt=0;
nfoCnt=0;
for info in nfofiles:
trimNfoPath,trimNfoFilename = os.path.split(info)
#query='select strPath,strFilename from path, files where path.idPath = files.idPath and strPath="'+trimPath+'/" and strFilename="'+trimFilename+'" order by strPath, strFilename'
skipIt=False;
for exc in EXECPTIONLIST:
if(re.match('.*'+exc.upper()+'.*',trimNfoPath.upper())):
skipIt=True;
break;
for exc in EXCLUDEINFOLIST:
if(exc.upper() == trimNfoFilename.upper()):
skipIt=True;
break;
if (skipIt == False):
nfoCnt=nfoCnt+1;
for movie in files:
found=False;
trimMoviePath,trimMovieFilename = os.path.split(movie)
skipIt=False;
for exc in EXECPTIONLIST:
if(re.match('.*'+exc.upper()+'.*',trimMoviePath.upper())):
skipIt=True;
break;
if (skipIt == True):
pass;
infoName, infoExt = os.path.splitext(trimNfoFilename)
if (trimNfoPath.upper() == trimMoviePath.upper()):
movieName, movieExt = os.path.splitext(trimMovieFilename)
#if(re.match('.*'+infoName.upper()+'.*',movieName.upper())):
if(infoName.upper() == movieName.upper()):
found=True;
skipIt=True;
foundCnt=foundCnt+1
if (debug):
print "match: " + trimNfoFilename+ " " + trimMovieFilename
break;
else:
pass;
if (found == False):
print "CleanUp for:"+info
for extension in CLEAN_EXTENSIONS:
toBeDel=os.path.join(trimNfoPath, infoName)
toBeDel=toBeDel+extension;
if os.path.isfile(toBeDel):
os.remove(toBeDel)
print "deleted: "+toBeDel
print "Found "+ str(foundCnt)+ " matches over "+str(nfoCnt)+" info files!"
print "Complete.";
and here the XBMC GUI dialog to perform the cleanup, save to default.py (the script assumes Cleanup.py is located in /usr/share/xbmc/script/Cleanup directory on linux (that is special://xbmc/scripts/Cleanup/Cleanup.py)
but you can easily change it to whatever location you wrote the Cleanup script file:
Code:
import os, xbmc, xbmcgui
#get actioncodes from keymap.xml
ACTION_PREVIOUS_MENU = 10
class MyClass(xbmcgui.Window):
def onAction(self, action):
if action == ACTION_PREVIOUS_MENU:
self.close()
mydisplay = MyClass()
dialog = xbmcgui.Dialog()
if dialog.yesno("Cleanup", "Clean up orphaned info, fanart and thumbs?"):
print "execute cleanup..."
xbmc.executescript('special://xbmc/scripts/Cleanup/Cleanup.py')
else:
print "Nothing done..."
del mydisplay