def buildJobForEpisode(aDir, anEpisode, associationTable, outDirectory): listSerieOut = [ o for o in os.listdir(outDirectory) if os.path.isdir(os.path.join(outDirectory, o)) ] listSerieOut.sort() answer = InteractionUtils.query_choice_list( "For file '" + anEpisode + "', is the show is in the following list ? If yes, please write the corresponding index. If not type -1.", listSerieOut) if answer is None: answer = InteractionUtils.query_string("Type the name of the show: ") newPath = os.path.join(outDirectory, answer) if not os.path.exists(newPath): os.makedirs(newPath) showDirectory = os.path.join(outDirectory, answer) if os.path.exists(showDirectory): listSeasonOut = [ o for o in os.listdir(showDirectory) if os.path.isdir(os.path.join(showDirectory, o)) ] answerSeason = InteractionUtils.query_choice_list( "For file '" + anEpisode + "', is the season is in the following list ? If yes, please write the corresponding index. If not type -1.", listSeasonOut) if answerSeason is None: answerSeason = InteractionUtils.query_string( "Type the name for the folder corresponding to the season: ") newPath = os.path.join(showDirectory, answerSeason) if not os.path.exists(newPath): os.makedirs(newPath) showSeason = os.path.join(showDirectory, answerSeason) if os.path.exists(showSeason): if InteractionUtils.query_yes_no( "Do you want to copy episode '" + anEpisode + "' and subtitle to this folder : '" + showSeason + "' ?"): videoPath = os.path.join(aDir, anEpisode) subPath = None if os.path.exists(os.path.join(showSeason, anEpisode)): print("Video file already transferred") else: associationTable[videoPath] = showSeason print("Copy '" + videoPath + "' to '" + showSeason + "'") if not os.path.exists( os.path.join(showSeason, anEpisode[:-4] + ".srt")): if os.path.exists( os.path.join(aDir, anEpisode[:-4] + ".srt")): print("Sub file already transferred") else: subPath = os.path.join(aDir, anEpisode[:-4] + ".srt") print("Copy '" + subPath + "' to '" + showSeason + "'") associationTable[subPath] = showSeason else: print("No Sub file found")
else: result = {} title = '' if re.match('.*[.|\s]S[0-9][0-9]E[0-3][0-9]', truncatedVideo, re.IGNORECASE): title = re.search('(.*)[.|\s]S[0-9][0-9]E[0-3][0-9]', truncatedVideo, re.IGNORECASE).group(1) if re.match('.*[.|\s][0-9]?[0-9][0-3][0-9]', truncatedVideo, re.IGNORECASE): title = re.search('(.*)[.|\s][0-9]?[0-9][0-3][0-9]', truncatedVideo, re.IGNORECASE).group(1) title = title.replace(' ','') title = title.replace('.','') title = title.lower() for aSub in listSubtitle: subTitle = None if re.match('.* - [0-9][0-9]x[0-3][0-9]', aSub, re.IGNORECASE): subTitle = re.search('(.*) - [0-9][0-9]x[0-3][0-9]', aSub, re.IGNORECASE).group(1) if subTitle is not None: subTitle = subTitle.lower() subTitle = subTitle.replace(' ','') seq=SequenceMatcher(None,title, subTitle) result[aSub]=seq.ratio() sortedSub = reversed(sorted(result.items(), key=operator.itemgetter(1))) print ("##################################") print ("For episode : "+aVideo) for aResult in sortedSub: if aResult[0] not in excludedSub and InteractionUtils.query_yes_no("Associate '"+aResult[0]+"' with this episode ?"): association[aVideo] = aResult[0] excludedSub.append(aResult[0]) break for anAsso in association.keys(): newSrtFileName = (anAsso[:-4])+".srt" if InteractionUtils.query_yes_no("Rename '"+association[anAsso]+"' to "+newSrtFileName): os.rename(os.path.join(aDir,association[anAsso]), os.path.join(aDir,newSrtFileName))
title = title.replace(' ', '') title = title.replace('.', '') title = title.lower() for aSub in listSubtitle: subTitle = None if re.match('.* - [0-9][0-9]x[0-3][0-9]', aSub, re.IGNORECASE): subTitle = re.search('(.*) - [0-9][0-9]x[0-3][0-9]', aSub, re.IGNORECASE).group(1) if subTitle is not None: subTitle = subTitle.lower() subTitle = subTitle.replace(' ', '') seq = SequenceMatcher(None, title, subTitle) result[aSub] = seq.ratio() sortedSub = reversed( sorted(result.items(), key=operator.itemgetter(1))) print("##################################") print("For episode : " + aVideo) for aResult in sortedSub: if aResult[ 0] not in excludedSub and InteractionUtils.query_yes_no( "Associate '" + aResult[0] + "' with this episode ?"): association[aVideo] = aResult[0] excludedSub.append(aResult[0]) break for anAsso in association.keys(): newSrtFileName = (anAsso[:-4]) + ".srt" if InteractionUtils.query_yes_no("Rename '" + association[anAsso] + "' to " + newSrtFileName): os.rename(os.path.join(aDir, association[anAsso]), os.path.join(aDir, newSrtFileName))