def load_settings(self, data_path=None): data_path = data_path or self.SETTINGS_PATH if os.path.isfile(data_path): f = open(data_path, "r") data = sporklib.byteify(json.loads(f.read(), "ascii")) try: self.load_custom_settings_path(sporklib.normalize_path(data["settingsPath"])) self.load_settings() return None #end loop here except (IOError,KeyError): pass try: self.RECORD_TEMPLATE = data["recordSchema"] except KeyError: pass try: self.CONTENT_DIRECTORY = sporklib.normalize_path(data["contentDirectory"]) except KeyError: pass try: self.RECORDS_PATH = data["recordsPath"] except KeyError: pass try: self.AVAIL_ITEMS_PATH = data["availItemsPath"] except KeyError: pass
def extract_unique_files(dir, dir_dest, move=False, dry_run=False): if (os.path.isfile(dir)) or (os.path.isfile(dir_dest)): raise IOError("ERROR! extract_unique_files takes only directories as arguments, not files.") dir_dest = sporklib.normalize_path(dir_dest) sporklib.safe_mkdir(dir_dest) unique_hashes = sporklib.hash_dir(dir) if dry_run: for hash,path in unique_hashes.items(): print(ntpath.basename(path)) else: if move: for hash,path in unique_hashes.items(): sporklib.safe_move(path, dir_dest) else: for hash,path in unique_hashes.items(): shutil.copy2(path, dir_dest)
def extract_duplicate_files(dir_source, dir_dest, move=False, dry_run=False): if (os.path.isfile(dir_source)) or (os.path.isfile(dir_dest)): raise IOError("ERROR! extract_unique_files takes only directories as arguments, not files.") dir_dest = sporklib.normalize_path(dir_dest) sporklib.safe_mkdir(dir_dest) dupes = sporklib.hash_dir(dir_source, True)[1] if dry_run: for hash,path in dupes.items(): print(ntpath.basename(path)) else: if move: for hash,paths in dupes.items(): for p in paths[1:]: sporklib.safe_move(p, dir_dest) else: for hash,paths in dupes.items(): for p in paths: shutil.copy2(p, dir_dest) # @staticmethod # def find_matching_file(match_str, tgt_dir,
def get_record_file_path(self, record): return sporklib.normalize_path(self.CONTENT_DIRECTORY + "/" + record["name"] + "." + record["ext"])
import sporklib_utils import sporklib import argparse arg_parser = argparse.ArgumentParser(description="Renames a directory of TV shows to more pleasant format") arg_parser.add_argument("-d", "--dir", required=True, help="The directory containing the shows you'd like to rename") arg_parser.add_argument("-t", "--target", required=True, help="Directory to extract unique files to") arg_parser.add_argument("-r", "--dryrun", required=False, help="Do a \"dry run\", printing unique file names but taking no action", action="store_true") arg_parser.add_argument("-m", "--move", required=False, help="Move files instead of copying them.", action="store_true") args = arg_parser.parse_args() dir = sporklib.normalize_path(args.dir) target = sporklib.normalize_path(args.target) dry_run = args.dryrun move = args.move if dir == "": print("ERROR: Directory not specified") exit(1) if target == "": print("ERROR: target not specified") exit(2) sporklib_utils.files.extract_unique_files(dir, target, move, dry_run)
import sporklib_utils import sporklib import argparse arg_parser = argparse.ArgumentParser(description="Renames a directory of TV shows to more pleasant format") arg_parser.add_argument("-d", "--dir", required=True, help="The directory containing the shows you'd like to rename") arg_parser.add_argument("-n", "--name", required=True, help="Name of the show as you'd like it to appear. Pass UPDATE (all caps) to use name of show that is majority in directory.") arg_parser.add_argument("-r", "--dryrun", required=False, help="Do a \"dry run\", printing new names but taking no action", action="store_true") arg_parser.add_argument("-p", "--pattern", required=False, help="Enter a regex which will be used to extract the episode name from each file") args = arg_parser.parse_args() dir = sporklib.normalize_path(args.dir) name = args.name dry_run = args.dryrun pattern = args.pattern if dir == "": print("ERROR: Directory not specified") exit(1) if name == "": print("ERROR: Name not specified") exit(2) if name == "UPDATE": scoreboard = {} files_list = sporklib.list_files(dir, True, False)
def rename_shows(dir, show_name, dry_run=False): if dry_run: print("DRY RUN -- FILES NOT RENAMED") video_formats = [".mpg", ".mp4", ".avi", ".mkv", ".mpeg"] dir = sporklib.normalize_path(dir) season = "XX" episode = "XX" re_season = "[Ss]\d{1,2}" re_episode = "[Ee]\d{1,2}" files = [] vid_file_paths = [] file_paths = sporklib.list_files(dir, True) for path in file_paths: vid_file_paths.append(path) i = path.rfind("/") f = path[i+1:] files.append(f) file_paths = vid_file_paths if len(file_paths) != len(files): raise IndexError("List of file paths did not have corresponding indices in list of file names.") i = 0 for filename in files: ext_i = filename.rfind(".") extension = filename[ext_i:] if extension in video_formats: new_name = "" s_matcher = re.compile(re_season) e_matcher = re.compile(re_episode) looper = [["s", s_matcher], ["e", e_matcher]] for loop in looper: matcher = loop[1] mo = matcher.search(filename) if mo != None: #regex failed positions = mo.span() match = filename[positions[0]:positions[1]] match = re.sub("[^0 -9]", "", match) match = int(match) if match > 9: match = str(match) else: match = "0" + str(match) if loop[0] == "s": season = match elif loop[0] == "e": episode = match else: raise IndexError("ERROR! DID NOT RECOGNIZE LOOP DIRECTIVE: " + loop[0]) new_name = show_name + " - S" + season + "E" + episode + extension path = file_paths[i] new_path = dir + "/" + new_name if new_path != path: print(filename + " --> " + new_name) if not dry_run: os.rename(path, new_path) i += 1