XBMC script to cleanup orphaned info, fanart, thumbs ...
#1
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

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
Reply
#2
Cleanup as a plugin w/ plugin settings to change proper video home
also with the option to remove every nfo fanart etc.. regardless of video presence

get it here:

http://ul.to/0ssi3k

uncompress in plugins/programs directory
Reply
#3
Thats a pretty cool idea. Has anybody tried to run this yet?
Reply
#4
If you use (like i do) ember media manager or some other scraping tool to scrape movies/tv shows fanarts etc and then import in XBMC then you definitely need this plugin.
I wrote it to cleanup no more necessary files after I've removed a movie or tvshow from XBMC library anf filesystem.

You can give it a try with a test directory if you feel worried about the script execution..
you can also enable the debug output in the plugin settings to see exactly what's going on with your files but it slows down a lot the execution if you have 100+ media

i've asked the same feature enhancement to EMM dev team and i believe it's planned for the next release.. check it out:

http://www.embermm.com/issues/38

Wink
Reply

Logout Mark Read Team Forum Stats Members Help
XBMC script to cleanup orphaned info, fanart, thumbs ...0