# Include an html helpfile in the plugin's zipfile with the following name.
RESOURCE_NAME=PLUGIN_NAME+'_Help.htm'
importsys,os,re
importtime
importzipfile
importtraceback
fromzipfileimportZipFile
classDeDRMError(Exception):
pass
fromcalibre.customizeimportFileTypePlugin
fromcalibre.constantsimportiswindows,isosx
fromcalibre.gui2importis_ok_to_use_qt
fromcalibre.utils.configimportconfig_dir
# Wrap a stream so that output gets flushed immediately
# and also make sure that any unicode strings get safely
# encoded using "replace" before writing them.
classSafeUnbuffered:
def__init__(self,stream):
self.stream=stream
self.encoding=stream.encoding
ifself.encoding==None:
self.encoding="utf-8"
defwrite(self,data):
ifisinstance(data,unicode):
data=data.encode(self.encoding,"replace")
self.stream.write(data)
self.stream.flush()
def__getattr__(self,attr):
returngetattr(self.stream,attr)
classDeDRM(FileTypePlugin):
name=PLUGIN_NAME
description=u"Removes DRM from Amazon Kindle, Adobe Adept (including Kobo), Barnes & Noble, Mobipocket and eReader ebooks. Credit given to i♥cabbages and The Dark Reverser for the original stand-alone scripts."
supported_platforms=['linux','osx','windows']
author=u"DiapDealer, Apprentice Alf, The Dark Reverser and i♥cabbages"
version=PLUGIN_VERSION_TUPLE
minimum_calibre_version=(0,7,55)# Compiled python libraries cannot be imported in earlier versions.
# Return the modified PersistentTemporary file to calibre.
returnof.name
printu"{0} v{1}: Failed to decrypt with key {2:s} after {3:.1f} seconds.".format(PLUGIN_NAME,PLUGIN_VERSION,keyname_masked,time.time()-self.starttime)
printu"{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,time.time()-self.starttime)
raiseDeDRMError(u"{0} v{1}: Ultimately failed to decrypt “{2}” after {3:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,os.path.basename(path_to_ebook),time.time()-self.starttime))
# import the Adobe Adept ePub handler
importcalibre_plugins.dedrm.ineptepubasineptepub
ifineptepub.adeptBook(inf.name):
printu"{0} v{1}: {2} is a secure Adobe Adept ePub.".format(PLUGIN_NAME,PLUGIN_VERSION,os.path.basename(path_to_ebook))
# Attempt to decrypt epub with each encryption key (generated or provided).
# Return the modified PersistentTemporary file to calibre.
returnof.name
printu"{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds.".format(PLUGIN_NAME,PLUGIN_VERSION,time.time()-self.starttime)
exceptException,e:
pass
# Something went wrong with decryption.
printu"{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,time.time()-self.starttime)
raiseDeDRMError(u"{0} v{1}: Ultimately failed to decrypt “{2}” after {3:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,os.path.basename(path_to_ebook),time.time()-self.starttime))
# Not a Barnes & Noble nor an Adobe Adept
# Import the fixed epub.
printu"{0} v{1}: “{2}” is neither an Adobe Adept nor a Barnes & Noble encrypted ePub.".format(PLUGIN_NAME,PLUGIN_VERSION,os.path.basename(path_to_ebook))
returninf.name
defPDFDecrypt(self,path_to_ebook):
importcalibre_plugins.dedrm.configasconfig
importcalibre_plugins.dedrm.ineptpdf
# Attempt to decrypt epub with each encryption key (generated or provided).
printu"{0} v{1}: {2} is a PDF ebook.".format(PLUGIN_NAME,PLUGIN_VERSION,os.path.basename(path_to_ebook))
# Return the modified PersistentTemporary file to calibre.
returnof.name
printu"{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds.".format(PLUGIN_NAME,PLUGIN_VERSION,time.time()-self.starttime)
exceptException,e:
pass
# Something went wrong with decryption.
printu"{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,time.time()-self.starttime)
raiseDeDRMError(u"{0} v{1}: Ultimately failed to decrypt “{2}” after {3:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,os.path.basename(path_to_ebook),time.time()-self.starttime))
defKindleMobiDecrypt(self,path_to_ebook):
# add the alfcrypto directory to sys.path so alfcrypto.py
# will be able to locate the custom lib(s) for CDLL import.
sys.path.insert(0,self.alfdir)
# Had to move this import here so the custom libs can be
# extracted to the appropriate places beforehand these routines
#if you reached here then no luck raise and exception
printu"{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,time.time()-self.starttime)
traceback.print_exc()
raiseDeDRMError(u"{0} v{1}: Ultimately failed to decrypt “{4}” after {3:.1f} seconds with error: {2}\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,e.args[0],time.time()-self.starttime,os.path.basename(path_to_ebook)))
of=self.temporary_file(book.getBookExtension())
book.getFile(of.name)
of.close()
book.cleanup()
returnof.name
defeReaderDecrypt(self,path_to_ebook):
importcalibre_plugins.dedrm.configasconfig
importcalibre_plugins.dedrm.erdr2pml
# Attempt to decrypt epub with each encryption key (generated or provided).
# Decryption was successful return the modified PersistentTemporary
# file to Calibre's import process.
ifresult==0:
returnof.name
printu"{0} v{1}: Failed to decrypt with key {2:s} after {3:.1f} seconds.".format(PLUGIN_NAME,PLUGIN_VERSION,keyname_masked,time.time()-self.starttime)
printu"{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,time.time()-self.starttime)
raiseDeDRMError(u"{0} v{1}: Ultimately failed to decrypt “{2}” after {3:.1f} seconds.\nRead the FAQs at Alf's blog: http://apprenticealf.wordpress.com/".format(PLUGIN_NAME,PLUGIN_VERSION,os.path.basename(path_to_ebook),time.time()-self.starttime))
defrun(self,path_to_ebook):
# make sure any unicode output gets converted safely with 'replace'
sys.stdout=SafeUnbuffered(sys.stdout)
sys.stderr=SafeUnbuffered(sys.stderr)
printu"{0} v{1}: Trying to decrypt {2}.".format(PLUGIN_NAME,PLUGIN_VERSION,os.path.basename(path_to_ebook))