def ShowNameLookUp(self, string): goodlogging.Log.Info( "EPGUIDES", "Looking up show name match for string '{0}' in guide".format( string), verbosity=self.logVerbosity) self._GetTitleList() showName = util.GetBestMatch(string, self._showTitleList) return (showName)
def ShowNameLookUp(self, string): """ Attempts to find the best match for the given string in the list of epguides show titles. If this list has not previous been generated it will be generated first. Parameters ---------- string : string String to find show name match against. Returns ---------- string Show name which best matches input string. """ goodlogging.Log.Info( "EPGUIDES", "Looking up show name match for string '{0}' in guide".format( string), verbosity=self.logVerbosity) self._GetTitleList() showName = util.GetBestMatch(string, self._showTitleList) return (showName)
def _GenerateLibraryPath(self, tvFile, libraryDir): """ Creates a full path for TV file in TV library. This initially attempts to directly match a show directory in the database, if this fails it searches the library directory for the best match. The user can then select an existing match or can propose a new directory to use as the show root directory. The season directory is also generated and added to the show and library directories. This is then used by the tvFile GenerateNewFilePath method to create a new path for the file. Parameters ---------- tvFile : tvfile.TVFile Contains show and file info. libraryDir : string Root path of TV library directory. Returns ---------- tvfile.TVFile This is an updated version of the input object. """ goodlogging.Log.Info( "RENAMER", "Looking up library directory in database for show: {0}".format( tvFile.showInfo.showName)) goodlogging.Log.IncreaseIndent() showID, showName, showDir = self._db.SearchTVLibrary( showName=tvFile.showInfo.showName)[0] if showDir is None: goodlogging.Log.Info( "RENAMER", "No directory match found in database - looking for best match in library directory: {0}" .format(libraryDir)) dirList = os.listdir(libraryDir) listDir = False matchName = tvFile.showInfo.showName while showDir is None: if len(dirList) == 0: goodlogging.Log.Info("RENAMER", "TV Library directory is empty") response = None else: if listDir is True: goodlogging.Log.Info( "RENAMER", "TV library directory contains: {0}".format( ', '.join(dirList))) else: matchDirList = util.GetBestMatch(matchName, dirList) listDir = False if self._skipUserInput is True: if len(matchDirList) == 1: response = matchDirList[0] goodlogging.Log.Info( "RENAMER", "Automatic selection of show directory: {0}". format(response)) else: response = None goodlogging.Log.Info( "RENAMER", "Could not make automatic selection of show directory" ) else: listDirPrompt = "enter 'ls' to list all items in TV library directory" response = util.UserAcceptance( matchDirList, promptComment=listDirPrompt, promptOnly=listDir, xStrOverride="to create new show directory") if response is None: showDir = self._CreateNewShowDir(tvFile.showInfo.showName) if showDir is None: goodlogging.Log.DecreaseIndent() return tvFile elif response.lower() == 'ls': listDir = True elif response in matchDirList: showDir = response else: matchName = response self._db.UpdateShowDirInTVLibrary(showID, showDir) # Add base directory to show path showDir = os.path.join(libraryDir, showDir) goodlogging.Log.DecreaseIndent() # Lookup and add season directory to show path seasonDir = self._LookUpSeasonDirectory(showID, showDir, tvFile.showInfo.seasonNum) if seasonDir is None: return tvFile else: showDir = os.path.join(showDir, seasonDir) # Call tvFile function to generate file name tvFile.GenerateNewFilePath(showDir) return tvFile
def _LookUpSeasonDirectory(self, showID, showDir, seasonNum): """ Look up season directory. First attempt to find match from database, otherwise search TV show directory. If no match is found in the database the user can choose to accept a match from the TV show directory, enter a new directory name to use or accept an autogenerated name. Parameters ---------- showID : int Show ID number showDir : string Path to show file directory seasonNum : int Season number Returns ---------- string Name of season directory to use. This can be a blank string to use the root show directory, an autogenerated string or a user given string. """ goodlogging.Log.Info( "RENAMER", "Looking up season directory for show {0}".format(showID)) goodlogging.Log.IncreaseIndent() # Look up existing season folder from database seasonDirName = self._db.SearchSeasonDirTable(showID, seasonNum) if seasonDirName is not None: goodlogging.Log.Info( "RENAMER", "Found season directory match from database: {0}".format( seasonDirName)) else: # Look up existing season folder in show directory goodlogging.Log.Info( "RENAMER", "Looking up season directory (Season {0}) in {1}".format( seasonNum, showDir)) if os.path.isdir(showDir) is False: goodlogging.Log.Info( "RENAMER", "Show directory ({0}) is not an existing directory".format( showDir)) seasonDirName = self._CreateNewSeasonDir(seasonNum) else: matchDirList = [] for dirName in os.listdir(showDir): subDir = os.path.join(showDir, dirName) if os.path.isdir(subDir): seasonResult = re.findall("Season", dirName) if len(seasonResult) > 0: numResult = re.findall("[0-9]+", dirName) numResult = set(numResult) if len(numResult) == 1: if int(numResult.pop()) == int(seasonNum): matchDirList.append(dirName) if self._skipUserInput is True: if len(matchDirList) == 1: userAcceptance = matchDirList[0] goodlogging.Log.Info( "RENAMER", "Automatic selection of season directory: {0}". format(seasonDirName)) else: userAcceptance = None goodlogging.Log.Info( "RENAMER", "Could not make automatic selection of season directory" ) else: listDirPrompt = "enter 'ls' to list all items in show directory" userAcceptance = util.UserAcceptance( matchDirList, promptComment=listDirPrompt, xStrOverride="to create new season directory") if userAcceptance in matchDirList: seasonDirName = userAcceptance elif userAcceptance is None: seasonDirName = self._CreateNewSeasonDir(seasonNum) else: recursiveSelectionComplete = False promptOnly = False dirLookup = userAcceptance while recursiveSelectionComplete is False: dirList = os.listdir(showDir) if dirLookup.lower() == 'ls': dirLookup = '' promptOnly = True if len(dirList) == 0: goodlogging.Log.Info( "RENAMER", "Show directory is empty") else: goodlogging.Log.Info( "RENAMER", "Show directory contains: {0}".format( ', '.join(dirList))) else: matchDirList = util.GetBestMatch( dirLookup, dirList) response = util.UserAcceptance( matchDirList, promptComment=listDirPrompt, promptOnly=promptOnly, xStrOverride="to create new season directory") promptOnly = False if response in matchDirList: seasonDirName = response recursiveSelectionComplete = True elif response is None: seasonDirName = self._CreateNewSeasonDir( seasonNum) recursiveSelectionComplete = True else: dirLookup = response # Add season directory to database if seasonDirName is not None: self._db.AddSeasonDirTable(showID, seasonNum, seasonDirName) goodlogging.Log.DecreaseIndent() return seasonDirName
def _GetShowID(self, stringSearch, origStringSearch=None): """ Search for given string as an existing entry in the database file name table or, if no match is found, as a show name from the TV guide. If an exact match is not found in the database the user can accept or decline the best match from the TV guide or can provide an alternate match to lookup. Parameters ---------- stringSearch : string String to look up in database or guide. origStringSearch : string [optional: default = None] Original search string, used by recusive function calls. Returns ---------- tvfile.ShowInfo or None If no show id could be found this returns None, otherwise it returns a tvfile.ShowInfo object containing show name and show id. """ showInfo = tvfile.ShowInfo() if origStringSearch is None: goodlogging.Log.Info( "RENAMER", "Looking up show ID for: {0}".format(stringSearch)) origStringSearch = stringSearch goodlogging.Log.IncreaseIndent() showInfo.showID = self._db.SearchFileNameTable(stringSearch) if showInfo.showID is None: goodlogging.Log.Info( "RENAMER", "No show ID match found for '{0}' in database".format( stringSearch)) showNameList = self._guide.ShowNameLookUp(stringSearch) if self._skipUserInput is True: if len(showNameList) == 1: showName = showNameList[0] goodlogging.Log.Info( "RENAMER", "Automatic selection of showname: {0}".format( showName)) else: showName = None goodlogging.Log.Info( "RENAMER", "Show skipped - could not make automatic selection of showname" ) else: showName = util.UserAcceptance(showNameList) if showName in showNameList: libEntry = self._db.SearchTVLibrary(showName=showName) if libEntry is None: if self._skipUserInput is True: response = 'y' else: goodlogging.Log.Info( "RENAMER", "No show by this name found in TV library database. Is this a new show for the database?" ) response = goodlogging.Log.Input( "RENAMER", "Enter 'y' (yes), 'n' (no) or 'ls' (list existing shows): " ) response = util.ValidUserResponse( response, ('y', 'n', 'ls')) if response.lower() == 'ls': dbLibList = self._db.SearchTVLibrary() if dbLibList is None: goodlogging.Log.Info("RENAMER", "TV library is empty") response = 'y' else: dbShowNameList = [i[1] for i in dbLibList] dbShowNameStr = ', '.join(dbShowNameList) goodlogging.Log.Info( "RENAMER", "Existing shows in database are: {0}". format(dbShowNameStr)) response = goodlogging.Log.Input( "RENAMER", "Is this a new show? [y/n]: ") response = util.ValidUserResponse( response, ('y', 'n')) if response.lower() == 'y': showInfo.showID = self._db.AddShowToTVLibrary(showName) showInfo.showName = showName else: try: dbShowNameList except NameError: dbLibList = self._db.SearchTVLibrary() if dbLibList is None: goodlogging.Log.Info( "RENAMER", "No show ID found - TV library is empty") return None dbShowNameList = [i[1] for i in dbLibList] while showInfo.showID is None: matchShowList = util.GetBestMatch( showName, dbShowNameList) showName = util.UserAcceptance(matchShowList) if showName is None: goodlogging.Log.Info( "RENAMER", "No show ID found - could not match to existing show" ) return None elif showName in matchShowList: showInfo.showID = self._db.SearchTVLibrary( showName=showName)[0][0] showInfo.showName = showName else: showInfo.showID = libEntry[0][0] self._db.AddToFileNameTable(origStringSearch, showInfo.showID) goodlogging.Log.DecreaseIndent() return showInfo elif showName is None: goodlogging.Log.DecreaseIndent() return None else: goodlogging.Log.DecreaseIndent() return self._GetShowID(showName, origStringSearch) else: goodlogging.Log.Info( "RENAMER", "Match found: show ID = {0}".format(showInfo.showID)) if origStringSearch != stringSearch: self._db.AddToFileNameTable(origStringSearch, showInfo.showID) goodlogging.Log.DecreaseIndent() return showInfo
def _GetShowID(self, stringSearch, origStringSearch=None): showInfo = tvfile.ShowInfo() if origStringSearch is None: goodlogging.Log.Info( "RENAMER", "Looking up show ID for: {0}".format(stringSearch)) origStringSearch = stringSearch goodlogging.Log.IncreaseIndent() showInfo.showID = self._db.SearchFileNameTable(stringSearch) if showInfo.showID is None: goodlogging.Log.Info( "RENAMER", "No show ID match found for '{0}' in database".format( stringSearch)) showNameList = self._guide.ShowNameLookUp(stringSearch) if self._skipUserInput is True: if len(showNameList) == 1: showName = showNameList[0] goodlogging.Log.Info( "RENAMER", "Automatic selection of showname: {0}".format( showName)) else: showName = None goodlogging.Log.Info( "RENAMER", "Show skipped - could not make automatic selection of showname" ) else: showName = util.UserAcceptance(showNameList) if showName in showNameList: libEntry = self._db.SearchTVLibrary(showName=showName) if libEntry is None: if self._skipUserInput is True: response = 'y' else: goodlogging.Log.Info( "RENAMER", "No show by this name found in TV library database. Is this a new show for the database?" ) response = goodlogging.Log.Input( "RENAMER", "Enter 'y' (yes), 'n' (no) or 'ls' (list existing shows): " ) response = util.ValidUserResponse( response, ('y', 'n', 'ls')) if response.lower() == 'ls': dbLibList = self._db.SearchTVLibrary() if dbLibList is None: goodlogging.Log.Info("RENAMER", "TV library is empty") response = 'y' else: dbShowNameList = [i[1] for i in dbLibList] dbShowNameStr = ', '.join(dbShowNameList) goodlogging.Log.Info( "RENAMER", "Existing shows in database are: {0}". format(dbShowNameStr)) response = goodlogging.Log.Input( "RENAMER", "Is this a new show? [y/n]: ") response = util.ValidUserResponse( response, ('y', 'n')) if response.lower() == 'y': showInfo.showID = self._db.AddShowToTVLibrary(showName) showInfo.showName = showName else: try: dbShowNameList except NameError: dbLibList = self._db.SearchTVLibrary() if dbLibList is None: goodlogging.Log.Info( "RENAMER", "No show ID found - TV library is empty") return None dbShowNameList = [i[1] for i in dbLibList] finally: while showInfo.showID is None: matchShowList = util.GetBestMatch( showName, dbShowNameList) showName = util.UserAcceptance(matchShowList) if showName is None: goodlogging.Log.Info( "RENAMER", "No show ID found - could not match to existing show" ) return None elif showName in matchShowList: showInfo.showID = self._db.SearchTVLibrary( showName=showName)[0][0] showInfo.showName = showName else: showInfo.showID = libEntry[0][0] self._db.AddToFileNameTable(origStringSearch, showInfo.showID) goodlogging.Log.DecreaseIndent() return showInfo elif showName is None: goodlogging.Log.DecreaseIndent() return None else: goodlogging.Log.DecreaseIndent() return self._GetShowID(showName, origStringSearch) else: goodlogging.Log.Info( "RENAMER", "Match found: show ID = {0}".format(showInfo.showID)) if origStringSearch != stringSearch: self._db.AddToFileNameTable(origStringSearch, showInfo.showID) goodlogging.Log.DecreaseIndent() return showInfo
def _GenerateLibraryPath(self, tvFile, libraryDir): goodlogging.Log.Info( "RENAMER", "Looking up library directory in database for show: {0}".format( tvFile.showInfo.showName)) goodlogging.Log.IncreaseIndent() showID, showName, showDir = self._db.SearchTVLibrary( showName=tvFile.showInfo.showName)[0] if showDir is None: goodlogging.Log.Info( "RENAMER", "No directory match found in database - looking for best match in library directory: {0}" .format(libraryDir)) dirList = os.listdir(libraryDir) listDir = False matchName = tvFile.showInfo.showName while showDir is None: if len(dirList) == 0: goodlogging.Log.Info("RENAMER", "TV Library directory is empty") response = None else: if listDir is True: goodlogging.Log.Info( "RENAMER", "TV library directory contains: {0}".format( ', '.join(dirList))) else: matchDirList = util.GetBestMatch(matchName, dirList) listDir = False if self._skipUserInput is True: if len(matchDirList) == 1: response = matchDirList[0] goodlogging.Log.Info( "RENAMER", "Automatic selection of show directory: {0}". format(response)) else: response = None goodlogging.Log.Info( "RENAMER", "Could not make automatic selection of show directory" ) else: listDirPrompt = "enter 'ls' to list all items in TV library directory" response = util.UserAcceptance( matchDirList, promptComment=listDirPrompt, promptOnly=listDir, xStrOverride="to create new show directory") if response is None: showDir = self._CreateNewShowDir(tvFile.showInfo.showName) if showDir is None: goodlogging.Log.DecreaseIndent() return tvFile elif response.lower() == 'ls': listDir = True elif response in matchDirList: showDir = response else: matchName = response self._db.UpdateShowDirInTVLibrary(showID, showDir) # Add base directory to show path showDir = os.path.join(libraryDir, showDir) goodlogging.Log.DecreaseIndent() # Lookup and add season directory to show path seasonDir = self._LookUpSeasonDirectory(showID, showDir, tvFile.showInfo.seasonNum) if seasonDir is None: return tvFile else: showDir = os.path.join(showDir, seasonDir) # Call tvFile function to generate file name tvFile.GenerateNewFilePath(showDir) return tvFile
def _LookUpSeasonDirectory(self, showID, showDir, seasonNum): goodlogging.Log.Info( "RENAMER", "Looking up season directory for show {0}".format(showID)) goodlogging.Log.IncreaseIndent() # Look up existing season folder from database seasonDirName = self._db.SearchSeasonDirTable(showID, seasonNum) if seasonDirName is not None: goodlogging.Log.Info( "RENAMER", "Found season directory match from database: {0}".format( seasonDirName)) else: # Look up existing season folder in show directory goodlogging.Log.Info( "RENAMER", "Looking up season directory (Season {0}) in {1}".format( seasonNum, showDir)) if os.path.isdir(showDir) is False: goodlogging.Log.Info( "RENAMER", "Show directory ({0}) is not an existing directory".format( showDir)) seasonDirName = self._CreateNewSeasonDir(seasonNum) else: matchDirList = [] if os.path.isdir(showDir): for dirName in os.listdir(showDir): subDir = os.path.join(showDir, dirName) if os.path.isdir(subDir): seasonResult = re.findall("Season", dirName) if len(seasonResult) > 0: numResult = re.findall("[0-9]+", dirName) numResult = set(numResult) if len(numResult) == 1: if int(numResult.pop()) == int(seasonNum): matchDirList.append(dirName) if self._skipUserInput is True: if len(matchDirList) == 1: userAcceptance = matchDirList[0] goodlogging.Log.Info( "RENAMER", "Automatic selection of season directory: {0}". format(seasonDirName)) else: userAcceptance = None goodlogging.Log.Info( "RENAMER", "Could not make automatic selection of season directory" ) else: listDirPrompt = "enter 'ls' to list all items in show directory" userAcceptance = util.UserAcceptance( matchDirList, promptComment=listDirPrompt, xStrOverride="to create new season directory") if userAcceptance in matchDirList: seasonDirName = userAcceptance elif userAcceptance is None: seasonDirName = self._CreateNewSeasonDir(seasonNum) else: recursiveSelectionComplete = False promptOnly = False dirLookup = userAcceptance while recursiveSelectionComplete is False: dirList = os.listdir(showDir) if dirLookup.lower() == 'ls': dirLookup = '' promptOnly = True if len(dirList) == 0: goodlogging.Log.Info( "RENAMER", "Show directory is empty") else: goodlogging.Log.Info( "RENAMER", "Show directory contains: {0}".format( ', '.join(dirList))) else: matchDirList = util.GetBestMatch( dirLookup, dirList) response = util.UserAcceptance( matchDirList, promptComment=listDirPrompt, promptOnly=promptOnly, xStrOverride="to create new season directory") promptOnly = False if response in matchDirList: seasonDirName = response recursiveSelectionComplete = True elif response is None: seasonDirName = self._CreateNewSeasonDir( seasonNum) recursiveSelectionComplete = True else: dirLookup = response # Add season directory to database if seasonDirName is not None: self._db.AddSeasonDirTable(showID, seasonNum, seasonDirName) goodlogging.Log.DecreaseIndent() return seasonDirName