def main(): # utils.fix_output_encoding() settings = ComicTaggerSettings() # this can only work with files with ComicRack tags style = MetaDataStyle.CIX if len(sys.argv) < 2: print("Usage: {0} [comic_folder]".format(sys.argv[0]), file=sys.stderr) return if sys.argv[1] == "-n": filelist = utils.get_recursive_filelist(sys.argv[2:]) else: filelist = utils.get_recursive_filelist(sys.argv[1:]) # first read in CIX metadata from all files, make a list of candidates modify_list = [] for filename in filelist: print(filename, end='\n') ca = ComicArchive( filename, settings.rar_exe_path, default_image_path= "/home/timmy/build/source/comictagger-test/comictaggerlib/graphics/nocover.png" ) if (ca.isZip or ca.isRar()) and ca.hasMetadata(style): md = ca.readMetadata(style) if len(md.pages) != 0: pgs = list() mod = False for p in md.pages: if "Type" in p and p["Type"] in unwanted_types: # This one has pages to remove. Remove it! print("removing " + ca.getPageName(int(p["Image"]))) if sys.argv[1] != "-n": mod = True ca.archiver.removeArchiveFile( ca.getPageName(int(p["Image"]))) else: pgs.append(p) if mod: for num, p in enumerate(pgs): p["Image"] = str(num) md.pages = pgs ca.writeCIX(md)
def main(): utils.fix_output_encoding() settings = ComicTaggerSettings() style = MetaDataStyle.CIX if len(sys.argv) < 2: print >> sys.stderr, "usage: {0} comic_folder ".format(sys.argv[0]) return filelist = utils.get_recursive_filelist( sys.argv[1:] ) #first read in metadata from all files metadata_list = [] max_name_len = 2 for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path ) if ca.hasMetadata( style ): #make a list of paired filenames and metadata objects metadata_list.append((filename, ca.readMetadata( style ))) max_name_len = max ( max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format( filename ) + "\r", sys.stderr.flush() print >> sys.stderr, fmt_str.format( "" ) + "\r", print "-----------------------------------------------" print "Found {0} comics with {1} tags".format( len(metadata_list), MetaDataStyle.name[style]) print "-----------------------------------------------" # now, figure out column widths w0 = 4 w1 = 4 for filename,md in metadata_list: if not md.isEmpty: w0 = max( len((os.path.split(filename)[1])), w0) if md.series is not None: w1 = max( len(md.series), w1) w0 += 2 # build a format string fmt_str = u"{0:" + str(w0) + "} {1:" + str(w1) + "} #{2:6} ({3})" # now sort the list by issue, and then series metadata_list.sort(key=lambda x: IssueString(x[1].issue).asString(3), reverse=False) metadata_list.sort(key=lambda x: unicode(x[1].series).lower()+str(x[1].year), reverse=False) # now print for filename, md in metadata_list: if not md.isEmpty: print fmt_str.format(os.path.split(filename)[1]+":", md.series, md.issue, md.year), md.title
def main(): utils.fix_output_encoding() settings = ComicTaggerSettings() style = MetaDataStyle.CIX if len(sys.argv) < 2: print >> sys.stderr, "usage: {0} comic_folder ".format(sys.argv[0]) return filelist = utils.get_recursive_filelist( sys.argv[1:] ) #first read in info from all files comic_list = [] max_name_len = 2 for filename in filelist: ca = ComicArchive(filename, settings ) if ca.seemsToBeAComicArchive(): comic_list.append( ca ) max_name_len = max ( max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format( filename ) + "\r", sys.stderr.flush() print >> sys.stderr, fmt_str.format( "" ) + "\r", print "-----------------------------------------------" print "Found {0} comics".format( len(comic_list)) print "-----------------------------------------------" # now, figure out column widths w0 = 4 for ca in comic_list: w0 = max( len((os.path.split(ca.path)[1])), w0) w0 += 2 # build a format string fmt_str = u"{0:" + str(w0) + "} {1}" # now print for ca in comic_list: name = ca.getPageName(ca.getScannerPageIndex()) if name is not None: name = os.path.split( name )[1] print fmt_str.format(os.path.split(ca.path)[1]+":", name)
def main(): utils.fix_output_encoding() settings = ComicTaggerSettings() style = MetaDataStyle.CIX if len(sys.argv) < 2: print >> sys.stderr, "usage: {0} comic_folder ".format(sys.argv[0]) return filelist = utils.get_recursive_filelist( sys.argv[1:] ) #first find all comics with metadata print >> sys.stderr, "reading in all comics..." comic_list = [] fmt_str = "" max_name_len = 2 for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path ) if ca.seemsToBeAComicArchive() and ca.hasMetadata( style ): max_name_len = max ( max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format( filename ) + "\r", sys.stderr.flush() comic_list.append((filename, ca.readMetadata( style ))) print >> sys.stderr, fmt_str.format( "" ) + "\r", print "-----------------------------------------------" print "Found {0} comics with {1} tags".format( len(comic_list), MetaDataStyle.name[style]) print "-----------------------------------------------" #sort the list by series+issue+year, to put all the dupes together def makeKey(x): return "<" + unicode(x[1].series) + u" #" + unicode( x[1].issue ) + u" - " + unicode( x[1].year ) + ">" comic_list.sort(key=makeKey, reverse=False) # look for duplicate blocks dupe_set_list = list() dupe_set = list() prev_key = "" for filename, md in comic_list: print >> sys.stderr, fmt_str.format( filename ) + "\r", sys.stderr.flush() new_key = makeKey((filename, md)) #if the new key same as the last, add to to dupe set if new_key == prev_key: dupe_set.append(filename) #else we're on a new potential block else: # only add if the dupe list has 2 or more if len (dupe_set) > 1: dupe_set_list.append( dupe_set ) dupe_set = list() dupe_set.append(filename) prev_key = new_key print >> sys.stderr, fmt_str.format( "" ) + "\r", print "Found {0} duplicate sets".format( len(dupe_set_list)) for dupe_set in dupe_set_list: ca = ComicArchive(dupe_set[0], settings.rar_exe_path ) md = ca.readMetadata( style ) print "{0} #{1} ({2})".format( md.series, md.issue, md.year ) for filename in dupe_set: print "------------->{0}".format( filename )
def main(): utils.fix_output_encoding() settings = ComicTaggerSettings() style = MetaDataStyle.CIX if platform.system() == "Windows": print >> sys.stderr, "Sorry, this script works only on UNIX systems" if len(sys.argv) < 3: print >> sys.stderr, "usage: {0} comic_root link_root".format( sys.argv[0]) return comic_root = sys.argv[1] link_root = sys.argv[2] print "root is : ", comic_root filelist = utils.get_recursive_filelist([comic_root]) make_folder(link_root) #first find all comics with metadata print "reading in all comics..." comic_list = [] max_name_len = 2 for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path) if ca.seemsToBeAComicArchive() and ca.hasMetadata(style): comic_list.append((filename, ca.readMetadata(style))) max_name_len = max(max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format(filename) + "\r", sys.stderr.flush() print >> sys.stderr, fmt_str.format("") print "Found {0} tagged comics.".format(len(comic_list)) # walk through the comic list and add subdirs and links for each one for filename, md in comic_list: print >> sys.stderr, fmt_str.format(filename) + "\r", sys.stderr.flush() #do date organizing: if md.month is not None: month_str = "{0:02d}".format(int(md.month)) else: month_str = "00" date_folder = os.path.join(link_root, "date", str(md.year), month_str) make_folder(date_folder) make_link(filename, os.path.join(date_folder, os.path.basename(filename))) #do publisher/series organizing: fixed_series_name = md.series if fixed_series_name is not None: # some tweaks to keep various filesystems happy fixed_series_name = fixed_series_name.replace("/", "-") fixed_series_name = fixed_series_name.replace("?", "") series_folder = os.path.join(link_root, "series", str(md.publisher), unicode(fixed_series_name)) make_folder(series_folder) make_link(filename, os.path.join(series_folder, os.path.basename(filename)))
def main(): utils.fix_output_encoding() settings = ComicTaggerSettings() # this can only work with files with ComicRack tags style = MetaDataStyle.CIX if len(sys.argv) < 2: print >> sys.stderr, "usage: {0} comic_folder ".format(sys.argv[0]) return filelist = utils.get_recursive_filelist( sys.argv[1:] ) #first read in CIX metadata from all files, make a list of candidates modify_list = [] for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path ) if (ca.isZip or ca.isRar()) and ca.hasMetadata( style ): md = ca.readMetadata( style ) if len(md.pages) != 0: for p in md.pages: if p.has_key('Type') and p['Type'] in unwanted_types: #This one has pages to remove. add to list! modify_list.append((filename, md)) break #now actually process those files for filename,md in modify_list: ca = ComicArchive(filename, settings.rar_exe_path ) curr_folder = os.path.dirname( filename ) curr_subfolder = os.path.join( curr_folder, subfolder_name ) #skip any of our generated subfolders... if os.path.basename(curr_folder) == subfolder_name: continue sys.stdout.write("Removing unwanted pages from " + filename) # verify that we can write to current folder if not os.access(filename, os.W_OK): print "Can't move: {0}: skipped!".format(filename) continue if not os.path.exists( curr_subfolder ) and not os.access(curr_folder, os.W_OK): print "Can't create subfolder here: {0}: skipped!".format(filename) continue if not os.path.exists( curr_subfolder ): os.mkdir( curr_subfolder ) if not os.access(curr_subfolder, os.W_OK): print "Can't write to the subfolder here: {0}: skipped!".format(filename) continue # generate a new file with temp name tmp_fd, tmp_name = tempfile.mkstemp( dir=os.path.dirname(filename) ) os.close( tmp_fd ) try: zout = zipfile.ZipFile (tmp_name, 'w') # now read in all the pages from the old one, except the ones we want to skip new_num = 0 new_pages = list() for p in md.pages: if p.has_key('Type') and p['Type'] in unwanted_types: continue else: pageNum = int(p['Image']) name = ca.getPageName( pageNum ) buffer = ca.getPage( pageNum ) sys.stdout.write('.') sys.stdout.flush() #Generate a new name for the page file ext = os.path.splitext(name)[1] new_name = "page{0:04d}{1}".format(new_num,ext) zout.writestr(new_name, buffer) # create new page entry new_p = dict() new_p['Image'] = str(new_num) if p.has_key('Type'): new_p['Type'] = p['Type'] new_pages.append(new_p) new_num += 1 #preserve the old comment comment = ca.archiver.getArchiveComment() if comment is not None: zout.comment = ca.archiver.getArchiveComment() except Exception as e: print "Failure creating new archive: {0}!".format(filename) print e, sys.exc_info()[0] zout.close() os.unlink( tmp_name ) else: zout.close() # Success! Now move the files shutil.move( filename, curr_subfolder ) os.rename( tmp_name, filename ) # TODO: We might have converted a rar to a zip, and should probably change # the extension, as needed. print "Done!".format(filename) # Create a new archive object for the new file, and write the old CIX data, with new page info ca = ComicArchive( filename, settings.rar_exe_path ) md.pages = new_pages ca.writeMetadata( style, md )
def main(): # utils.fix_output_encoding() settings = ComicTaggerSettings() style = MetaDataStyle.CIX if len(sys.argv) < 2: print("Usage: {0} [comic_folder]".format(sys.argv[0])) return dupecmp = os.path.join(os.getcwd(), "dupecmp") if os.path.exists(dupecmp): subprocess.run(["bash", "-c", "rm -rf *"], cwd=dupecmp) else: os.mkdir(dupecmp) filelist = utils.get_recursive_filelist(sys.argv[1:]) # first find all comics with metadata print("Reading in all comics...", file=sys.stderr) comic_list = [] fmt_str = "" max_name_len = 2 for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path, default_image_path='/home/timmy/build/source/comictagger-test/comictaggerlib/graphics/nocover.png') if ca.seemsToBeAComicArchive() and ca.hasMetadata(style): fmt_str = "{{0:{0}}}".format(max_name_len) print(fmt_str.format(filename) + "\r", end='', file=sys.stderr) sys.stderr.flush() comic_list.append((filename, ca.readMetadata(style))) max_name_len = len(filename) print("", file=sys.stderr) print("--------------------------------------------------------------------------", file=sys.stderr) print("Found {0} comics with {1} tags".format(len(comic_list), MetaDataStyle.name[style]), file=sys.stderr) print("--------------------------------------------------------------------------", file=sys.stderr) # sort the list by series+issue+year, to put all the dupes together def makeKey(x): return "<" + str(x[1].series) + " #" + \ str(x[1].issue) + " - " + str(x[1].title) + " - " + str(x[1].year) + ">" comic_list.sort(key=makeKey, reverse=False) # look for duplicate blocks dupe_set_list = list() dupe_set = list() prev_key = "" for filename, md in comic_list: # sys.stderr.flush() new_key = makeKey((filename, md)) # if the new key same as the last, add to to dupe set if new_key == prev_key: dupe_set.append(filename) # else we're on a new potential block else: # only add if the dupe list has 2 or more if len(dupe_set) > 1: dupe_set_list.append(dupe_set) dupe_set = list() dupe_set.append(filename) prev_key = new_key if len(dupe_set) > 1: dupe_set_list.append(dupe_set) # print(json.dumps(dupe_set_list, indent=4)) # print(fmt_str.format("") + "\r", end=' ', file=sys.stderr) # print("Found {0} duplicate sets".format(len(dupe_set_list))) for dupe_set in dupe_set_list: subprocess.run(["cp"] + dupe_set + [dupecmp]) subprocess.run(["dup-comic.sh"], cwd=dupecmp)
def main(): utils.fix_output_encoding() settings = ComicTaggerSettings() # this can only work with files with ComicRack tags style = MetaDataStyle.CIX if len(sys.argv) < 2: print >> sys.stderr, "usage: {0} comic_folder ".format(sys.argv[0]) return filelist = utils.get_recursive_filelist( sys.argv[1:] ) #first make a list of all comic archive files comics_list = [] max_name_len = 2 fmt_str = u"{{0:{0}}}".format(max_name_len) for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path ) if (ca.seemsToBeAComicArchive()): # Check the images in the file, see if we need to reduce any for idx in range(ca.getNumberOfPages()): in_data = ca.getPage( idx ) if in_data is not None: try: im = Image.open(StringIO.StringIO(in_data)) w,h = im.size if h > max_height: comics_list.append( ca ) max_name_len = max ( max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format( filename ) + "\r", sys.stderr.flush() break except IOError: #doesn't appear to be an image pass print >> sys.stderr, fmt_str.format( "" ) + "\r", print "-----------------------------------------------" print "Found {0} comics with over-large pages".format( len(comics_list)) print "-----------------------------------------------" for item in comics_list: print item.path #now actually process those files with over-large pages for ca in comics_list: filename = ca.path curr_folder = os.path.dirname( filename ) curr_subfolder = os.path.join( curr_folder, subfolder_name ) #skip any of our generated subfolders... if os.path.basename(curr_folder) == subfolder_name: continue sys.stdout.write("Processing: " + filename) # verify that we can write to current folder if not os.access(filename, os.W_OK): print "Can't move: {0}: skipped!".format(filename) continue if not os.path.exists( curr_subfolder ) and not os.access(curr_folder, os.W_OK): print "Can't create subfolder here: {0}: skipped!".format(filename) continue if not os.path.exists( curr_subfolder ): os.mkdir( curr_subfolder ) if not os.access(curr_subfolder, os.W_OK): print "Can't write to the subfolder here: {0}: skipped!".format(filename) continue # generate a new file with temp name tmp_fd, tmp_name = tempfile.mkstemp( dir=os.path.dirname(filename) ) os.close( tmp_fd ) cix_md = None if ca.hasCIX(): cix_md = ca.readCIX() try: zout = zipfile.ZipFile (tmp_name, 'w') # Check the images in the file, see if we want to reduce them page_count = ca.getNumberOfPages() for idx in range(ca.getNumberOfPages()): name = ca.getPageName( idx ) in_data = ca.getPage( idx ) out_data = in_data if in_data is not None: try: im = Image.open(StringIO.StringIO(in_data)) w,h = im.size if h > max_height: #resize the image hpercent = (max_height/float(h)) wsize = int((float(w)*float(hpercent))) size=(wsize, max_height) im = im.resize(size, Image.ANTIALIAS) output = StringIO.StringIO() im.save(output, format="JPEG", quality=85) out_data = output.getvalue() output.close() except IOError: #doesn't appear to be an image pass else: #page is empty?? nothing to write out_data = "" sys.stdout.write('.') sys.stdout.flush() #write out the new resized image zout.writestr(name, out_data) #preserve the old comment comment = ca.archiver.getArchiveComment() if comment is not None: zout.comment = ca.archiver.getArchiveComment() except Exception as e: print "Failure creating new archive: {0}!".format(filename) print e, sys.exc_info()[0] zout.close() os.unlink( tmp_name ) else: zout.close() # Success! Now move the files shutil.move( filename, curr_subfolder ) os.rename( tmp_name, filename ) # TODO: We might have converted a rar to a zip, and should probably change # the extension, as needed. print "Done!".format(filename) # Create a new archive object for the new file, and write the old CIX data, w/o page info if cix_md is not None: ca = ComicArchive( filename, settings.rar_exe_path ) cix_md.pages = [] ca.writeCIX( cix_md )
def main(): utils.fix_output_encoding() settings = ComicTaggerSettings() style = MetaDataStyle.CIX if platform.system() == "Windows": print >> sys.stderr, "Sorry, this script works only on UNIX systems" if len(sys.argv) < 3: print >> sys.stderr, "usage: {0} comic_root link_root".format(sys.argv[0]) return comic_root = sys.argv[1] link_root = sys.argv[2] print "root is : ", comic_root filelist = utils.get_recursive_filelist( [ comic_root ] ) make_folder( link_root ) #first find all comics with metadata print "reading in all comics..." comic_list = [] max_name_len = 2 for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path ) if ca.seemsToBeAComicArchive() and ca.hasMetadata( style ): comic_list.append((filename, ca.readMetadata( style ))) max_name_len = max ( max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format( filename ) + "\r", sys.stderr.flush() print >> sys.stderr, fmt_str.format( "" ) print "Found {0} tagged comics.".format( len(comic_list)) # walk through the comic list and add subdirs and links for each one for filename, md in comic_list: print >> sys.stderr, fmt_str.format( filename ) + "\r", sys.stderr.flush() #do date organizing: if md.month is not None: month_str = "{0:02d}".format(int(md.month)) else: month_str = "00" date_folder = os.path.join(link_root, "date", str(md.year), month_str) make_folder( date_folder ) make_link( filename, os.path.join(date_folder, os.path.basename(filename)) ) #do publisher/series organizing: fixed_series_name = md.series if fixed_series_name is not None: # some tweaks to keep various filesystems happy fixed_series_name = fixed_series_name.replace("/", "-") fixed_series_name = fixed_series_name.replace("?", "") series_folder = os.path.join(link_root, "series", str(md.publisher), unicode(fixed_series_name)) make_folder( series_folder ) make_link( filename, os.path.join(series_folder, os.path.basename(filename)) )
def main(): utils.fix_output_encoding() settings = ComicTaggerSettings() # this can only work with files with ComicRack tags style = MetaDataStyle.CIX if len(sys.argv) < 2: print >> sys.stderr, "usage: {0} comic_folder ".format(sys.argv[0]) return filelist = utils.get_recursive_filelist(sys.argv[1:]) #first make a list of all comic archive files comics_list = [] max_name_len = 2 fmt_str = u"{{0:{0}}}".format(max_name_len) for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path) if (ca.seemsToBeAComicArchive()): # Check the images in the file, see if we need to reduce any for idx in range(ca.getNumberOfPages()): in_data = ca.getPage(idx) if in_data is not None: try: im = Image.open(StringIO.StringIO(in_data)) w, h = im.size if h > max_height: comics_list.append(ca) max_name_len = max(max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format( filename) + "\r", sys.stderr.flush() break except IOError: #doesn't appear to be an image pass print >> sys.stderr, fmt_str.format("") + "\r", print "-----------------------------------------------" print "Found {0} comics with over-large pages".format(len(comics_list)) print "-----------------------------------------------" for item in comics_list: print item.path #now actually process those files with over-large pages for ca in comics_list: filename = ca.path curr_folder = os.path.dirname(filename) curr_subfolder = os.path.join(curr_folder, subfolder_name) #skip any of our generated subfolders... if os.path.basename(curr_folder) == subfolder_name: continue sys.stdout.write("Processing: " + filename) # verify that we can write to current folder if not os.access(filename, os.W_OK): print "Can't move: {0}: skipped!".format(filename) continue if not os.path.exists(curr_subfolder) and not os.access( curr_folder, os.W_OK): print "Can't create subfolder here: {0}: skipped!".format(filename) continue if not os.path.exists(curr_subfolder): os.mkdir(curr_subfolder) if not os.access(curr_subfolder, os.W_OK): print "Can't write to the subfolder here: {0}: skipped!".format( filename) continue # generate a new file with temp name tmp_fd, tmp_name = tempfile.mkstemp(dir=os.path.dirname(filename)) os.close(tmp_fd) cix_md = None if ca.hasCIX(): cix_md = ca.readCIX() try: zout = zipfile.ZipFile(tmp_name, 'w') # Check the images in the file, see if we want to reduce them page_count = ca.getNumberOfPages() for idx in range(ca.getNumberOfPages()): name = ca.getPageName(idx) in_data = ca.getPage(idx) out_data = in_data if in_data is not None: try: im = Image.open(StringIO.StringIO(in_data)) w, h = im.size if h > max_height: #resize the image hpercent = (max_height / float(h)) wsize = int((float(w) * float(hpercent))) size = (wsize, max_height) im = im.resize(size, Image.ANTIALIAS) output = StringIO.StringIO() im.save(output, format="JPEG", quality=85) out_data = output.getvalue() output.close() except IOError: #doesn't appear to be an image pass else: #page is empty?? nothing to write out_data = "" sys.stdout.write('.') sys.stdout.flush() #write out the new resized image zout.writestr(name, out_data) #preserve the old comment comment = ca.archiver.getArchiveComment() if comment is not None: zout.comment = ca.archiver.getArchiveComment() except Exception as e: print "Failure creating new archive: {0}!".format(filename) print e, sys.exc_info()[0] zout.close() os.unlink(tmp_name) else: zout.close() # Success! Now move the files shutil.move(filename, curr_subfolder) os.rename(tmp_name, filename) # TODO: We might have converted a rar to a zip, and should probably change # the extension, as needed. print "Done!".format(filename) # Create a new archive object for the new file, and write the old CIX data, w/o page info if cix_md is not None: ca = ComicArchive(filename, settings.rar_exe_path) cix_md.pages = [] ca.writeCIX(cix_md)
def main(): default_xform_list = [ [ "^2000AD$", "2000 AD" ], [ "^G\.{0,1}I\.{0,1}Joe$", "G.I. Joe" ], ] utils.fix_output_encoding() settings = ComicTaggerSettings() style = MetaDataStyle.CIX parsed_args = parse_args() #parsed_args.noconfirm if parsed_args.transforms is not None: print "Reading in transforms from:", parsed_args.transforms json_data=open(parsed_args.transforms).read() data = json.loads(json_data) xform_list = data['xforms'] else: xform_list = default_xform_list #pprint( xform_list, indent=4) filelist = utils.get_recursive_filelist( parsed_args.paths ) #first find all comics print "reading in all comics..." comic_list = [] max_name_len = 2 fmt_str = "" for filename in filelist: ca = ComicArchive(filename, settings ) # do we care if it already has metadata? if ca.seemsToBeAComicArchive() and not ca.hasMetadata( style ): comic_list.append(ca) max_name_len = max ( max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format( filename ) + "\r", sys.stderr.flush() print >> sys.stderr, fmt_str.format( "" ) print "Found {0} comics.".format( len(comic_list)) modify_list = list() # walk through the comic list fix the filenames for ca in comic_list: # 1. parse the filename into a MD object md = ca.metadataFromFilename() # 2. walk thru list of transforms if md.series is not None and md.series != "": for pattern, replacement in xform_list: # apply each transform new_series = re.sub( pattern, replacement, md.series ) if new_series != md.series: md.series = new_series new_name = caclulate_rename(ca, md, settings) #found a match. add to proposed list, and bail on this file modify_list.append( (ca.path, new_name ) ) break print "{0} filenames to modify".format(len(modify_list)) if len(modify_list) > 0: if parsed_args.noconfirm: print "Not confirming before rename" else: for old_name, new_name in modify_list: print u"'{0}' -> '{1}'".format(os.path.basename(old_name), new_name) i = raw_input("Do you want to proceed with rename? [y/N] ") if i.lower() not in ('y', 'yes'): print "exiting without rename." sys.exit(0) perform_rename(modify_list)
def main(): default_xform_list = [ ["^2000AD$", "2000 AD"], ["^G\.{0,1}I\.{0,1}Joe$", "G.I. Joe"], ] utils.fix_output_encoding() settings = ComicTaggerSettings() style = MetaDataStyle.CIX parsed_args = parse_args() #parsed_args.noconfirm if parsed_args.transforms is not None: print "Reading in transforms from:", parsed_args.transforms json_data = open(parsed_args.transforms).read() data = json.loads(json_data) xform_list = data['xforms'] else: xform_list = default_xform_list #pprint( xform_list, indent=4) filelist = utils.get_recursive_filelist(parsed_args.paths) #first find all comics print "reading in all comics..." comic_list = [] max_name_len = 2 fmt_str = "" for filename in filelist: ca = ComicArchive(filename, settings.rar_exe_path) # do we care if it already has metadata? if ca.seemsToBeAComicArchive() and not ca.hasMetadata(style): comic_list.append(ca) max_name_len = max(max_name_len, len(filename)) fmt_str = u"{{0:{0}}}".format(max_name_len) print >> sys.stderr, fmt_str.format(filename) + "\r", sys.stderr.flush() print >> sys.stderr, fmt_str.format("") print "Found {0} comics.".format(len(comic_list)) modify_list = list() # walk through the comic list fix the filenames for ca in comic_list: # 1. parse the filename into a MD object md = ca.metadataFromFilename() # 2. walk thru list of transforms if md.series is not None and md.series != "": for pattern, replacement in xform_list: # apply each transform new_series = re.sub(pattern, replacement, md.series) if new_series != md.series: md.series = new_series new_name = caclulate_rename(ca, md, settings) #found a match. add to proposed list, and bail on this file modify_list.append((ca.path, new_name)) break print "{0} filenames to modify".format(len(modify_list)) if len(modify_list) > 0: if parsed_args.noconfirm: print "Not confirming before rename" else: for old_name, new_name in modify_list: print u"'{0}' -> '{1}'".format(os.path.basename(old_name), new_name) i = raw_input("Do you want to proceed with rename? [y/N] ") if i.lower() not in ('y', 'yes'): print "exiting without rename." sys.exit(0) perform_rename(modify_list)