Skip to content

Instantly share code, notes, and snippets.

@wuzhenda
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuzhenda/88b0c50fc74ad214bfbd to your computer and use it in GitHub Desktop.
Save wuzhenda/88b0c50fc74ad214bfbd to your computer and use it in GitHub Desktop.
# coding: utf-8
#!/bin/python
import os
import sys
import json
import base64
import binascii
import codecs
from Crypto.Cipher import AES
from zipfile import ZipFile
import string
targetDir=""
targetJsonFile="book.json"
targetEpubDir="epub"
targetOebpsDir="epub/OEBPS"
targetMetaDir="epub/META-INF"
targetEpubFile="Book.epub"
KEY=""
def initHandle():
args = sys.argv
args=args[1:]
curPath=os.getcwd()
global targetDir
targetDir=curPath+os.sep+args[0]
print targetDir
global targetJsonFile
targetJsonFile=targetDir+os.sep +targetJsonFile
global targetEpubDir
targetEpubDir=targetDir+os.sep +targetEpubDir
global targetOebpsDir
targetOebpsDir=targetDir+os.sep+targetOebpsDir
print targetOebpsDir
global targetMetaDir
targetMetaDir=targetDir+os.sep+targetMetaDir
print targetMetaDir
global targetEpubFile
targetEpubFile=curPath+os.sep+targetEpubFile
print targetEpubFile
def generateKey(jsonFile):
f=file(jsonFile)
s = json.load(f)
print s["key"]
global KEY
KEY=s["key"]
KEY=binascii.unhexlify(KEY)
f.close
def decodeWithFilePath(key,filePath):
filetmp='tmp.bak'
filename=filePath
os.rename(filePath,filetmp)
file_org=open(filetmp,'r')
file_org_content=file_org.read()
decBase64Data=base64.decodestring(file_org_content)
decryptor=AES.new(key,AES.MODE_ECB)
decAesData=decryptor.decrypt(decBase64Data)
decAesData=decAesData.strip()
decAesData=filter(lambda x: x in string.printable,decAesData)
#print decAesData
file_type = filename.rsplit('.',1)[1]
if 'css' in file_type :
decAesData= decAesData.replace(".rs-chapter-content","")
if not "body" in decAesData:
decAesData= decAesData.replace("html,","html,body")
decAesData=decAesData+"html {-webkit-overflow-scrolling:touch;}"
f=open(filename,'w')
f.write(decAesData)
f.close
file_org.close
os.remove(filetmp)
def formatXml(xmlFile):
filetmp='tmp.bak'
filename=xmlFile
os.rename(xmlFile,filetmp)
file_org=open(filetmp,'r')
file_org_content=file_org.read()
file_org_content=file_org_content.strip()
file_org_content_strip= file_org_content.strip('\t')
print file_org_content_strip
f=open(filename,'w')
f.write(file_org_content_strip)
f.close
file_org.close
os.remove(filetmp)
#decode css and js
#format opf ncx
def decodeCssJsFile(targetDir):
os.chdir(targetDir)
for d,ds,fs in os.walk('.'):
for f in fs:
if not '.' in f:
continue
file_type = f.rsplit('.',1)[1]
#sufix = os.path.splitext(f)[1][1:]
if 'css' in file_type or 'js' in file_type or 'xhtml' in file_type:
print os.path.join(d,f)
decodeWithFilePath(KEY,f)
if 'opf' in file_type or 'ncx' in file_type:
print os.path.join(d,f)
formatXml(f)
print 'Finished!'
def checkMetaFile(metaDir):
metaFile="container.xml"
targetfile=metaDir+os.sep+metaFile
if os.path.exists(targetfile):
print "%s exist"%targetfile
else:
f=open(targetfile,'w')
content='''<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf"
media-type="application/oebps-package+xml" />
</rootfiles>
</container>
'''
f.write(content)
f.close
def zipFile(dirName):
ebookname=targetEpubFile
epubfile=ZipFile(ebookname,'w')
os.chdir(dirName)
for d,ds,fs in os.walk('.'):
for f in fs:
print os.path.join(d,f)
epubfile.write(os.path.join(d,f))
epubfile.close()
print 'zip Finished!'
initHandle()
generateKey(targetJsonFile)
decodeCssJsFile(targetOebpsDir)
print targetEpubDir
print targetMetaDir
checkMetaFile(targetMetaDir)
zipFile(targetEpubDir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment