Skip to content

Instantly share code, notes, and snippets.

@amorri40
Created July 4, 2015 13:28
Show Gist options
  • Save amorri40/960691d33f5a078dc9a6 to your computer and use it in GitHub Desktop.
Save amorri40/960691d33f5a078dc9a6 to your computer and use it in GitHub Desktop.
Rename cbz/cbr files to standard format
import os,re
import glob
# def getFiles(dirname, suffixPattern=None):
# dirname=os.path.normpath(dirname)
# retDirs, retFiles=[], []
# for root, dirs, files in os.walk(dirname):
# for i in dirs:
# retDirs.append(os.path.join(root, i))
# for i in files:
# if suffixPattern is None or \
# suffixPattern.search(i) is not None:
# retFiles.append((root, i))
# return (retDirs, retFiles)
def get_scan_date(file_name):
m = re.search('\[(.+?)\]', file_name)
if m:
found = m.group(1)
if found == 'huckyc':
print "Need to rename file due to [huckyc]:", file_name
return found
return "Unknown"
def fix_scan_date(file_date):
split_date = file_date.split('-')
year=None
month=None
day=None
for date_partial in split_date:
date_partial_as_number = int(date_partial)
if date_partial_as_number > 31:
year=date_partial
elif date_partial_as_number > 12:
day = date_partial
elif day != None:
#
# We have already set the day and it can't be a year since they are always >31
#
month = date_partial
elif year !=None and month == None:
#
# #This was a year first date so next will be month
#
month = date_partial
elif year == None and day == None:
#
# This was a year last date so first will be day
#
day = date_partial
elif day == None and year!=None and month!=None:
#
# Must be day as the others are set
#
day = date_partial
else:
#
# Finally just a guess with a debug message
#
print "DEBUG: Year", year, " Month:", month, " Day:",day
day = date_partial
if (year < 1900):
year += 1900
return ""+year+"-"+month+"-"+day
this_path = os.path.dirname(os.path.realpath(__file__))
suffixPattern = re.compile(r'\.(cbz|cbr)$', re.IGNORECASE)
files= glob.glob("./*.cb?")
#(dirs,files) = getFiles('./', suffixPattern)
has_to_start_with_THE = True
number_has_to_be_4_digits = True
for file_path in files:
print "\n"
original_name = file_path[2:] #ignore the ./ at start
file_path = original_name
if has_to_start_with_THE:
if not file_path.startswith('The'):
file_path= "The "+file_path
if "-2.cb" in file_path:
print "WARN: Possible Duplicate "+file_path
if "huckyc" in file_path and not "(huckyc)" in file_path:
file_path = file_path.replace("huckyc", " (huckyc)")
#
# # Make sure we have spaces around the square brackets
#
file_path = file_path.replace("["," [")
file_path = file_path.replace("]","] ")
#
# Parse the date
#
scan_date = get_scan_date(file_path)
new_scan_date = fix_scan_date(scan_date)
file_path = file_path.replace(scan_date, new_scan_date)
#
# # Now strip all extra spaces that might have been added in previous steps
#
file_path = file_path.replace(" "," ")
if original_name == file_path:
print original_name+" should stay the same"
else:
print original_name + " -> " +file_path
os.rename(original_name, file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment