def GetDecryptedBook(infile, kDatabases, serials, pids, starttime = time.time()): # handle the obvious cases at the beginning if not os.path.isfile(infile): raise DrmException(u"Input file does not exist.") mobi = True magic3 = open(infile,'rb').read(3) if magic3 == 'TPZ': mobi = False if mobi: mb = mobidedrm.MobiBook(infile) else: mb = topazextract.TopazBook(infile) bookname = unescape(mb.getBookTitle()) print u"Decrypting {1} ebook: {0}".format(bookname, mb.getBookType()) # copy list of pids totalpids = list(pids) # extend PID list with book-specific PIDs md1, md2 = mb.getPIDMetaInfo() totalpids.extend(kgenpids.getPidList(md1, md2, serials, kDatabases)) print u"Found {1:d} keys to try after {0:.1f} seconds".format(time.time()-starttime, len(totalpids)) try: mb.processBook(totalpids) except: mb.cleanup raise print u"Decryption succeeded after {0:.1f} seconds".format(time.time()-starttime) return mb
def GetDecryptedBook(infile, kDatabases, serials, pids, starttime=time.time()): # handle the obvious cases at the beginning if not os.path.isfile(infile): raise DrmException(u"Input file does not exist.") mobi = True magic3 = open(infile, 'rb').read(3) if magic3 == 'TPZ': mobi = False if mobi: mb = mobidedrm.MobiBook(infile) else: mb = topazextract.TopazBook(infile) bookname = unescape(mb.getBookTitle()) print u"Decrypting {1} ebook: {0}".format(bookname, mb.getBookType()) # copy list of pids totalpids = list(pids) # extend PID list with book-specific PIDs md1, md2 = mb.getPIDMetaInfo() totalpids.extend(kgenpids.getPidList(md1, md2, serials, kDatabases)) print u"Found {1:d} keys to try after {0:.1f} seconds".format( time.time() - starttime, len(totalpids)) try: mb.processBook(totalpids) except: mb.cleanup raise print u"Decryption succeeded after {0:.1f} seconds".format(time.time() - starttime) return mb
def GetDecryptedBook(infile, kDatabases, androidFiles, serials, pids, starttime=time.time()): # handle the obvious cases at the beginning if not os.path.isfile(infile): raise DrmException(u"Input file does not exist.") mobi = True magic8 = open(infile, 'rb').read(8) if magic8 == '\xeaDRMION\xee': raise DrmException( u"The .kfx DRMION file cannot be decrypted by itself. A .kfx-zip archive containing a DRM voucher is required." ) magic3 = magic8[:3] if magic3 == 'TPZ': mobi = False if magic8[:4] == 'PK\x03\x04': mb = kfxdedrm.KFXZipBook(infile) elif mobi: mb = mobidedrm.MobiBook(infile) else: mb = topazextract.TopazBook(infile) bookname = unescape(mb.getBookTitle()) print u"Decrypting {1} ebook: {0}".format(bookname, mb.getBookType()) # copy list of pids totalpids = list(pids) # extend list of serials with serials from android databases for aFile in androidFiles: serials.extend(androidkindlekey.get_serials(aFile)) # extend PID list with book-specific PIDs from seriala and kDatabases md1, md2 = mb.getPIDMetaInfo() totalpids.extend(kgenpids.getPidList(md1, md2, serials, kDatabases)) # remove any duplicates totalpids = list(set(totalpids)) print u"Found {1:d} keys to try after {0:.1f} seconds".format( time.time() - starttime, len(totalpids)) #print totalpids try: mb.processBook(totalpids) except: mb.cleanup raise print u"Decryption succeeded after {0:.1f} seconds".format(time.time() - starttime) return mb
def GetDecryptedBook(infile, kDatabases, androidFiles, serials, pids, starttime = time.time()): # handle the obvious cases at the beginning if not os.path.isfile(infile): raise DrmException(u"Input file does not exist.") mobi = True magic8 = open(infile,'rb').read(8) if magic8 == '\xeaDRMION\xee': raise DrmException(u"KFX format detected. This format cannot be decrypted yet.") magic3 = magic8[:3] if magic3 == 'TPZ': mobi = False if mobi: mb = mobidedrm.MobiBook(infile) else: mb = topazextract.TopazBook(infile) bookname = unescape(mb.getBookTitle()) print u"Decrypting {1} ebook: {0}".format(bookname, mb.getBookType()) # copy list of pids totalpids = list(pids) # extend list of serials with serials from android databases for aFile in androidFiles: serials.extend(androidkindlekey.get_serials(aFile)) # extend PID list with book-specific PIDs from seriala and kDatabases md1, md2 = mb.getPIDMetaInfo() totalpids.extend(kgenpids.getPidList(md1, md2, serials, kDatabases)) # remove any duplicates totalpids = list(set(totalpids)) print u"Found {1:d} keys to try after {0:.1f} seconds".format(time.time()-starttime, len(totalpids)) #print totalpids try: mb.processBook(totalpids) except: mb.cleanup raise print u"Decryption succeeded after {0:.1f} seconds".format(time.time()-starttime) return mb
if o == '-p': if a == None: raise DrmException("Invalid parameter for -p") pids = a.split(',') if o == '-s': if a == None: raise DrmException("Invalid parameter for -s") serials = [serial.replace(" ", "") for serial in a.split(',')] bookname = os.path.splitext(os.path.basename(infile))[0] tb = TopazBook(infile) title = tb.getBookTitle() print(u"Processing Book: {0}".format(title)) md1, md2 = tb.getPIDMetaInfo() pids.extend(kgenpids.getPidList(md1, md2, serials, kDatabaseFiles)) try: print(u"Decrypting Book") tb.processBook(pids) print(u" Creating HTML ZIP Archive") zipname = os.path.join(outdir, bookname + u"_nodrm.htmlz") tb.getFile(zipname) print(u" Creating SVG ZIP Archive") zipname = os.path.join(outdir, bookname + u"_SVG.zip") tb.getSVGZip(zipname) # removing internal temporary directory of pieces tb.cleanup()
def cli_main(): argv = unicode_argv() progname = os.path.basename(argv[0]) print("TopazExtract v{0}.".format(__version__)) try: opts, args = getopt.getopt(argv[1:], "k:p:s:x") except getopt.GetoptError as err: print("Error in options or arguments: {0}".format(err.args[0])) usage(progname) return 1 if len(args) < 2: usage(progname) return 1 infile = args[0] outdir = args[1] if not os.path.isfile(infile): print("Input File {0} Does Not Exist.".format(infile)) return 1 if not os.path.exists(outdir): print("Output Directory {0} Does Not Exist.".format(outdir)) return 1 kDatabaseFiles = [] serials = [] pids = [] for o, a in opts: if o == '-k': if a == None: raise DrmException("Invalid parameter for -k") kDatabaseFiles.append(a) if o == '-p': if a == None: raise DrmException("Invalid parameter for -p") pids = a.split(',') if o == '-s': if a == None: raise DrmException("Invalid parameter for -s") serials = [serial.replace(" ", "") for serial in a.split(',')] bookname = os.path.splitext(os.path.basename(infile))[0] tb = TopazBook(infile) title = tb.getBookTitle() print("Processing Book: {0}".format(title)) md1, md2 = tb.getPIDMetaInfo() pids.extend(kgenpids.getPidList(md1, md2, serials, kDatabaseFiles)) try: print("Decrypting Book") tb.processBook(pids) print(" Creating HTML ZIP Archive") zipname = os.path.join(outdir, bookname + "_nodrm.htmlz") tb.getFile(zipname) print(" Creating SVG ZIP Archive") zipname = os.path.join(outdir, bookname + "_SVG.zip") tb.getSVGZip(zipname) # removing internal temporary directory of pieces tb.cleanup() except DrmException as e: print("Decryption failed\n{0}".format(traceback.format_exc())) try: tb.cleanup() except: pass return 1 except Exception as e: print("Decryption failed\n{0}".format(traceback.format_exc())) try: tb.cleanup() except: pass return 1 return 0
if o == '-p': if a == None : raise DrmException("Invalid parameter for -p") pids = a.split(',') if o == '-s': if a == None : raise DrmException("Invalid parameter for -s") serials = [serial.replace(" ","") for serial in a.split(',')] bookname = os.path.splitext(os.path.basename(infile))[0] tb = TopazBook(infile) title = tb.getBookTitle() print u"Processing Book: {0}".format(title) md1, md2 = tb.getPIDMetaInfo() pids.extend(kgenpids.getPidList(md1, md2, serials, kDatabaseFiles)) try: print u"Decrypting Book" tb.processBook(pids) print u" Creating HTML ZIP Archive" zipname = os.path.join(outdir, bookname + u"_nodrm.htmlz") tb.getFile(zipname) print u" Creating SVG ZIP Archive" zipname = os.path.join(outdir, bookname + u"_SVG.zip") tb.getSVGZip(zipname) # removing internal temporary directory of pieces tb.cleanup()