def main(): print("main") if len(sys.argv) > 1: sourcepath = sys.argv[1] if len(sys.argv) > 2: targetpath = sys.argv[2] series_list = os.listdir(targetpath) search_patterns = dict([(e, getMatcherString(e)) for e in series_list]) cache = EpisodeNamesCache() for dirpath, dirnames, filenames in os.walk(sourcepath): for f in filenames: if getFileEnding(f) not in ENDINGS: continue series = findDestDirectory(f.rstrip(), search_patterns) if series is None: continue episode_names = cache.get(series) newfilename = episode_names.getNewFileName(f) + "." + getFileEnding(f) try: moveFileToCorrectDirectory(os.path.join(targetpath, series), os.path.join(dirpath, f), newfilename) except IOError as e: logger.warn("could not move file to correct directory.") logger.warn(str(e)) continue
def moveFileToCorrectDirectory(targetpath, sourcefilepath, newfilename): filename = os.path.basename(sourcefilepath) match = re.search("^[0-9]+", newfilename) if match is None: logger.warn("cannot determine season-dir for %s" % filename) return season = match.group() targetdir = os.path.join(targetpath, season) if os.path.dirname(sourcefilepath) == targetdir: logger.info("%s is in the correct directory" % filename) return logger.info("%s should be moved" % filename) if not os.path.exists(targetdir): os.makedirs(targetdir) moveAndPrintProgress(sourcefilepath, os.path.join(targetdir, newfilename))
def getFileEnding(name): endingmatch = re.search("[^\.]+$", name) if endingmatch is None: logger.warn("no ending found for file: %s" % name) return None return endingmatch.group()