Comic To PDF / buildKindleComic.py

#!/usr/bin/env python

#The rar library used in this project can be found @ http://pypi.python.org/pypi/rarfile/2.2

import os
import sys
import glob
import shutil
import time

from rarfile import *
from zipfile import *
from pyPdf import PdfFileWriter, PdfFileReader
import Image
from progress_bar import ProgressBar

def validateComicStructure(comic):
	'''
		Do a quick check to make sure we have a dir to work with
		This prevents files from lying in the root folder
	'''
	files = comic.namelist()
	files.sort()	
	dirCheck = files[len(files) -1]
	#This handles dir with a dir option that would otherwise be missed
	if dirCheck.find('\\') != -1:
		return True

	for page in files:
		if page.find(dirCheck) == -1:
			return False

	return True

def extractFile(comicFileName,fileExtension):
	''' Extracts all the images from a rar/cbr file '''
	if comicFileName.endswith('.cbz'):
		comic = ZipFile(comicFileName)
	else:
		comic = RarFile(comicFileName)

	if not validateComicStructure(comic):
		cleanComicName = comicFileName.replace(fileExtension,"")
		os.mkdir(cleanComicName)
		extractedFolder = cleanComicName
		comic.extractall(path=extractedFolder)
	else:
		files = comic.namelist()
		comic.extractall()
		extractedFolder = files[len(files) -1]
		if extractedFolder.find('\\') != -1:
			extractedFolder = extractedFolder[:extractedFolder.find('\\')]
			 

	comic.close()
	return extractedFolder

def compilePdfFile(directory,originalFileName,fileExtension):
	''' Compiles a directory containing kindle formated image files into a single pdf '''
	#Need to add alot of error checking here , so much can go wrong , also remove extensions from the original files
	filesToCompile = glob.glob('kindle*.pdf')
	filesToCompile.sort()

	output = PdfFileWriter()

	for kindleFile in filesToCompile:
		if kindleFile.startswith('kindle'):
			input1 = PdfFileReader(file(kindleFile, "rb"))
			output.addPage(input1.getPage(0))
			os.remove(kindleFile)	

	outputStream = file(originalFileName.replace(fileExtension,".pdf"), "wb")

	output.write(outputStream)
	try:
		shutil.move(originalFileName.replace(fileExtension,".pdf"),'..')
	except Exception:
		print 'File Already Exists will not overwrite please remove manually ' + originalFileName.replace('.cbr',".pdf")

def formatImageForKindle(fileName):
	''' Formats a image for the kindle ,this mostly involes resizing the image '''
	im1 = Image.open(fileName)
	resizedImage = im1.resize((600, 800), Image.ANTIALIAS)
	resizedImage.save("kindle_"+fileName+".pdf")

def processComics(filterType,fileExtension):
	''' Check that we have a dir to scan provided '''
	dirList = glob.glob(filterType)
	dirList.sort()
	for comic in dirList:
		extractedFolder = extractFile(comic,fileExtension)
		os.chdir(extractedFolder)
		comicPages = os.listdir('.') 
		comicPages.sort()
		
		if len(comicPages) > 0:
			comicName = comic.replace(fileExtension,".pdf")
			print 'Compiling ' + comicName
			pageCount = 1
			p = ProgressBar(len(comicPages))
			for page in comicPages:
				#Make sure we end in a picture format or we have more problems
				if page.endswith('jpg') or page.endswith('png'):
					pageCount += 1
					p.update_time(pageCount)
					formatImageForKindle(page)
					os.system('clear')
					print 'Compiling '+ comicName
					print p
					#print 'Formated : ' + page
			
				
			compilePdfFile(os.path.abspath('.'),comic,fileExtension)	
			
			print 'PDF File Compiled ' + comic.replace(fileExtension,".pdf")
			os.chdir('..')
			shutil.rmtree(extractedFolder)
		else:
			print 'Nothing Extracted'


def main():
	comics = glob.glob('*.cbr')
	comics += glob.glob('*.cbz')
	print 'The Following Comics Will be processed'
	for comic in comics:
		print comic

	decision = raw_input('Do you want to continue ? (Y/n)')
	if decision == 'y' or decision == '':
		#First Handle CBR's then CBZ's
		processComics('*.cbr','.cbr')	
		processComics('*.cbz','.cbz')	

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.