diff --git a/TVEncoder.py b/TVEncoder.py index c5b8f04..152a36d 100644 --- a/TVEncoder.py +++ b/TVEncoder.py @@ -31,6 +31,7 @@ def showhelp(): 'files that will be processed without actually encoding them' print 'TVEncoder.py -e - encode the files that have been processed' print 'TVEncoder.py -e -l - list the files that would be encoded' + print 'TVEncoder.py -c - check the output directories for duplicates' def print_shows(shows): @@ -74,9 +75,11 @@ def processarguments(options): """ inputoptions = namedtuple("inputoptions", - "numfiles doencode readonly dolist") + "numfiles doencode readonly dolist " + "checkduplicates") inputoptions.readonly = False + inputoptions.checkduplicates = False for opt, arg in options: if opt == '-h': @@ -90,6 +93,8 @@ def processarguments(options): inputoptions.numfiles = arg elif opt == "-l": inputoptions.readonly = True + elif opt == "-c": + inputoptions.checkduplicates = True return inputoptions @@ -99,7 +104,7 @@ def main(argv): The main program for TVEncoder. """ try: - opts, _ = getopt.getopt(argv, "hlpen:") + opts, _ = getopt.getopt(argv, "hlpecn:") except getopt.GetoptError: showhelp() sys.exit(2) @@ -108,6 +113,16 @@ def main(argv): settings = Settings(SETTINGS) filemanager = FileManager(settings) + if inputoptions.checkduplicates: + print "Searching for duplicates..." + duplicates = filemanager.checkexistingduplicates() + if duplicates: + for duplicate in duplicates: + print duplicate + else: + print "No duplicates found." + return + if inputoptions.readonly: if inputoptions.doencode: #Generate the list of files that would be encoded diff --git a/libfilemanager.py b/libfilemanager.py index e404438..3faa963 100644 --- a/libfilemanager.py +++ b/libfilemanager.py @@ -112,6 +112,26 @@ class FileManager: #will reach here if there were less than numberofFiles found return showstoprocess + def checkexistingduplicates(self): + """ + Check the existing files in the output directories for duplicate + files, typically in different formats + """ + + duplicates = [] + for show in self.__settings.getshownames(): + outputdir = self.__settings.getshowoutputdirectory(show) + + for rootdir, dirnames, filenames in os.walk(outputdir): + for fle in filenames: + filename = os.path.join(rootdir, fle) + if os.path.splitext(fle)[1] in [".avi", ".mpg", ".mpeg", + "mp4", ".mkv"]: + if self.checkduplicates(filename): + duplicates.append(filename) + + return duplicates + @staticmethod def checkduplicates(filename): """ @@ -120,13 +140,16 @@ class FileManager: """ dirname = os.path.dirname(filename) - filename = os.path.basename(filename)[:6] + filename = os.path.basename(filename) + fileseasonepisode = filename[:6] + fileextension = os.path.splitext(filename)[1] for _, _, filenames in os.walk(dirname): for show in filenames: extension = os.path.splitext(show)[1] - if (extension in [".avi", ".mpg", ".mpeg", "mp4"] and - show[:6] == filename): + if (extension in [".avi", ".mpg", ".mpeg", "mp4", ".mkv"] and + show[:6] == fileseasonepisode + and fileextension != extension): return True return False diff --git a/libsettings.py b/libsettings.py index 7e69edf..efc5db6 100644 --- a/libsettings.py +++ b/libsettings.py @@ -8,19 +8,6 @@ Created on Fri Jul 5 20:14:15 2013 from configobj import ConfigObj -#============================================================================== -# class ShowSettings: -# """ -# Container for the settings for a show -# """ -# -# def __init__(self, name, inputdirectory, outputdirectory): -# self.name = name -# self.inputdirectory = inputdirectory -# self.outputdirectory = outputdirectory -#============================================================================== - - class Settings: """ Accessor for the configuration file diff --git a/tests/libfilemanagertest.py b/tests/libfilemanagertest.py index 643670a..94e0bd8 100644 --- a/tests/libfilemanagertest.py +++ b/tests/libfilemanagertest.py @@ -66,6 +66,16 @@ class libfilemanagertest(unittest.TestCase): self.assertFalse(result) + def test_checkduplicatesameextension(self): + settings = Mock('libsettings.Settings') + filemanager = FileManager(settings) + + os.walk = dummywalk + + result = filemanager.checkduplicates("/path/to/S03E14 - Test - SD TV.avi") + + self.assertFalse(result) + def dummywalk(arg): return [("/path/to/", [], ["S03E14 - Test - SD TV.avi"])]