def add_number(self, src, count) : dirname, basename = os.path.split(src) clean_pattern = ['^(?P<head>.*)(?P<clear>\.[0-9]+)', '^(?P<head>.*)(?P<clear>\|[0-9]+\|)'] for pattern in clean_pattern : match = re.match(pattern basename) if match is not None : dest = os.path.join(dirname, match.group('head')) Tools.rename(src, dest) self.rename(dest, count) return match = re.match('^(?P<head>.*)(?P<number>\[枚\])$', basename) if match is not None : head = match.group('head') else : head = basename if count['video'] <= 0 and count['jpg'] > 0 : dest = '{0}[{1}枚]'.format(head, count['jpg']) else : dest = '{0}[Animes & {1}枚]'.format(head, count['jpg']) dest = os.path.join(dirname, dest) tool = FileTools(src, dest) tool.safe_rename()
def get_number(self, path) : count = {'jpg':0, 'video':0} tool = FileTools(path) tool.remove_empty() for root, dirs, files in os.walk(path) : for d in dirs : d = os.path.join(root, d) if os.path.basename(d) not in ['Video', 'System', 'Other'] : print('{0} abnomal folder name'.format(d)) return None for f in files : f = os.path.join(root, f) basename, ext = os.path.splitext(f) if re.match('\.(jpg)', ext.lower()) is not None : count['jpg']+= 1 elif FileTools.is_media(f) count['video'] += 1 elif SevenZ.is_archive(f) : os.remove(f) elif re.match('.*Thumbs.db', f) is not None : os.remove(f) else : print("{0} abnomal".format(f)) return None return count
def rename(self): dst = self.make_new_name() if dst is not None and self.src != dst: filetools = FileTools(self.src, dst) dst = filetools.check_conflict() if dst is not None: Tools.rename(self.src, dst) else: print("FileTools check_conflict failed")
def move_to_av(self) : move = os.path.join(self.move_to, '#move') if self.move_to_db is None : return for name in os.listdir(move) : src = os.path.join(move, name) dest = os.path.join(self.move_to_db, name) tool = FileTools(src, dest) tool.safe_move()
def start(self) : print('-'*50, self.name, 'Moving ...', '-'*50) for src in self.get_match_files() : dst = os.path.join(self.move_to, os.path.basename(src)) ftools = FileTools(src, dst) dst = ftools.check_conflict() if dst is not None : try : shutil.move(src, dst) print(src, '-->', dst) except : traceback.print_exc()
def rename(self) : root = self.paths['rename'] for name in os.listdir(root) : name = os.path.join(root, name) if not SevenZ.is_archive(name) : continue dest = self.make_new_name(name) if dest is None : continue tool = FileTools(name, dest) tool.safe_rename()
def start(self) : print('-'*50, self.name, 'Renaming ...', '-'*50) for src in self.get_raw_datas() : dst = self.make_new_name(src) if dst is None : continue ftool = FileTools(src, dst) dst = ftool.check_conflict() if dst is not None : try : os.rename(src, dst) self.result['done'].append('SRC : {0}\nDST : {1}'.format(src, dst)) except : traceback.print_exc() self.result['fail'].append('rename fail :\nSRC : {0}\nDST : {1}'.format(src, dst))
def start(self) : if not SevenZ.is_zip : return self.dst = self.make_dst_name() if SevenZ.unzip(self.filename, self.dst, self.verbose) : print('UNZIP DONE :', self.filename) if self.top is not None: os.remove(self.filename) trance = TranceCoder(self.dst) trance.start() self.start_sub_zips() if self.top is None : tool = FileTools(self.dst) tool.move_all_folders_to(self.dst) tool.remove_empty_folders()
def done(self) : for f in os.listdir(self.move_to) : path = os.path.join(self.move_to, f) if not os.path.isdir(path) : continue elif f == '#move' : self.move_to_av() elif f == '#remove' : print("REMOVE : {0}".format(path)) shutil.rmtree(path) os.makedirs(path) else : tool = FileTools(path, self.move_to) tool.move_sub_into(pass_func=FileTools.is_media) print("REMOVE : {0}".format(path)) shutil.rmtree(path) renamer = AvRenamer(self.cfg) renamer.watchs = [self.move_to] renamer.start()
def unzip(self) : dst = os.path.splitext(self.src)[0] filetools = FileTools(self.src, dst) dst = filetools.check_conflict() if dst is None : return os.mkdir(dst) sevenz = SevenZ(self.src, dst) print("UNZIP : {0} started".format(os.path.basename(self.src))) if not sevenz.unzip() : # unzip failed shutil.rmtree(dst) print("UNZIP : {0} fail".format(os.path.basename(self.src))) return # trance code to jpg mogrify = Mogrify(dst) mogrify.start() print("UNZIP : {0} done".format(os.path.basename(dst))) if not self.isTop : try : shutil.rmtree(self.src) except : print("{0}".format(self.src)) # run sub folder exclude = '(ch|char|bg)' for root, dirs, files in os.walk(dst) : for name in files : path = os.path.join(root, name) if SevenZ.is_archive(path) and not re.search(exclude, name) : unzipper = Unzipper(path, False) unzipper.unzip() self.dst = dst
def is_include(self, raw) : return FileTools.is_media(raw)
def make_dst_name(self) : name, extension = os.path.splitext(os.path.basename(self.filename)) dst = os.path.join(self.output, name) tool = FileTools(self.filename) dst = tool.check_conflict_name(dst, self.output) return dst
def done(self, next_phase, backup) : if self.dst is not None and self.isTop : # move to top filetools = FileTools(self.dst, self.dst) filetools.move_sub_into('folders') filetools.remove_empty() # rename folder to just number count = 1 for name in os.listdir(self.dst) : name = os.path.join(self.dst, name) if not os.path.isdir(name) : continue dst = os.path.join(self.dst, "{0:02d}".format(count)) while os.path.exists(dst) : count += 1 dst = os.path.join(self.dst, "{0:02d}".format(count)) os.rename(name, dst) # make 'System' and 'Other' folder for name in ['System', 'Other'] : name = os.path.join(self.dst, name) os.mkdir(name) # move dst to next filetools = FileTools(self.dst, os.path.join(next_phase, os.path.basename(self.dst))) filetools.safe_move() # move src to backup filetools = FileTools(self.src, os.path.join(backup, os.path.basename(self.src))) filetools.safe_move()