More generic 3.0 changes, to be tested.

This commit is contained in:
Apprentice Harper
2020-09-27 11:54:49 +01:00
parent 6920f79a26
commit de50a02af9
42 changed files with 882 additions and 1028 deletions

View File

@@ -1,13 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# erdr2pml.py
# Copyright © 2008 The Dark Reverser
# Copyright © 2008-2020 The Dark Reverser, Apprentice Harper et al.
#
# Modified 20082012 by some_updates, DiapDealer and Apprentice Alf
# This is a python script. You need a Python interpreter to run it.
# For example, ActiveState Python, which exists for windows.
# Changelog
#
# Based on ereader2html version 0.08 plus some later small fixes
@@ -89,10 +85,10 @@ class SafeUnbuffered:
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
if isinstance(data,bytes):
if isinstance(data,str):
data = data.encode(self.encoding,"replace")
self.stream.write(data)
self.stream.flush()
self.stream.buffer.write(data)
self.stream.buffer.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
@@ -130,7 +126,7 @@ def unicode_argv():
range(start, argc.value)]
# if we don't have any arguments at all, just pass back script name
# this should never happen
return [u"mobidedrm.py"]
return ["mobidedrm.py"]
else:
argvencoding = sys.stdin.encoding
if argvencoding == None:
@@ -230,16 +226,16 @@ class Sectionizer(object):
# and with some (heavily edited) code from Paul Durrant's kindlenamer.py
def sanitizeFileName(name):
# substitute filename unfriendly characters
name = name.replace(u"<",u"[").replace(u">",u"]").replace(u" : ",u" ").replace(u": ",u" ").replace(u":",u"").replace(u"/",u"_").replace(u"\\",u"_").replace(u"|",u"_").replace(u"\"",u"\'")
name = name.replace("<","[").replace(">","]").replace(" : "," ").replace(": "," ").replace(":","").replace("/","_").replace("\\","_").replace("|","_").replace("\"","\'")
# delete control characters
name = u"".join(char for char in name if ord(char)>=32)
name = "".join(char for char in name if ord(char)>=32)
# white space to single space, delete leading and trailing while space
name = re.sub(r"\s", u" ", name).strip()
name = re.sub(r"\s", " ", name).strip()
# remove leading dots
while len(name)>0 and name[0] == u".":
while len(name)>0 and name[0] == ".":
name = name[1:]
# remove trailing dots (Windows doesn't like them)
if name.endswith(u'.'):
if name.endswith("."):
name = name[:-1]
return name
@@ -472,35 +468,35 @@ def decryptBook(infile, outpath, make_pmlz, user_key):
# outpath is actually pmlz name
pmlzname = outpath
outdir = tempfile.mkdtemp()
imagedirpath = os.path.join(outdir,u"images")
imagedirpath = os.path.join(outdir,"images")
else:
pmlzname = None
outdir = outpath
imagedirpath = os.path.join(outdir,bookname + u"_img")
imagedirpath = os.path.join(outdir,bookname + "_img")
try:
if not os.path.exists(outdir):
os.makedirs(outdir)
print(u"Decoding File")
sect = Sectionizer(infile, 'PNRdPPrs')
print("Decoding File")
sect =Sectionizer(infile, 'PNRdPPrs')
er = EreaderProcessor(sect, user_key)
if er.getNumImages() > 0:
print(u"Extracting images")
print("Extracting images")
if not os.path.exists(imagedirpath):
os.makedirs(imagedirpath)
for i in range(er.getNumImages()):
name, contents = er.getImage(i)
open(os.path.join(imagedirpath, name), 'wb').write(contents)
print(u"Extracting pml")
print("Extracting pml")
pml_string = er.getText()
pmlfilename = bookname + ".pml"
open(os.path.join(outdir, pmlfilename),'wb').write(cleanPML(pml_string))
if pmlzname is not None:
import zipfile
import shutil
print(u"Creating PMLZ file {0}".format(os.path.basename(pmlzname)))
print("Creating PMLZ file {0}".format(os.path.basename(pmlzname)))
myZipFile = zipfile.ZipFile(pmlzname,'w',zipfile.ZIP_STORED, False)
list = os.listdir(outdir)
for filename in list:
@@ -519,33 +515,33 @@ def decryptBook(infile, outpath, make_pmlz, user_key):
myZipFile.close()
# remove temporary directory
shutil.rmtree(outdir, True)
print(u"Output is {0}".format(pmlzname))
else :
print(u"Output is in {0}".format(outdir))
print("Output is {0}".format(pmlzname))
else
print("Output is in {0}".format(outdir))
print("done")
except ValueError as e:
print(u"Error: {0}".format(e))
print("Error: {0}".format(e))
traceback.print_exc()
return 1
return 0
def usage():
print(u"Converts DRMed eReader books to PML Source")
print(u"Usage:")
print(u" erdr2pml [options] infile.pdb [outpath] \"your name\" credit_card_number")
print(u" ")
print(u"Options: ")
print(u" -h prints this message")
print(u" -p create PMLZ instead of source folder")
print(u" --make-pmlz create PMLZ instead of source folder")
print(u" ")
print(u"Note:")
print(u" if outpath is ommitted, creates source in 'infile_Source' folder")
print(u" if outpath is ommitted and pmlz option, creates PMLZ 'infile.pmlz'")
print(u" if source folder created, images are in infile_img folder")
print(u" if pmlz file created, images are in images folder")
print(u" It's enough to enter the last 8 digits of the credit card number")
print("Converts DRMed eReader books to PML Source")
print("Usage:")
print(" erdr2pml [options] infile.pdb [outpath] \"your name\" credit_card_number")
print(" ")
print("Options: ")
print(" -h prints this message")
print(" -p create PMLZ instead of source folder")
print(" --make-pmlz create PMLZ instead of source folder")
print(" ")
print("Note:")
print(" if outpath is ommitted, creates source in 'infile_Source' folder")
print(" if outpath is ommitted and pmlz option, creates PMLZ 'infile.pmlz'")
print(" if source folder created, images are in infile_img folder")
print(" if pmlz file created, images are in images folder")
print(" It's enough to enter the last 8 digits of the credit card number")
return
def getuser_key(name,cc):
@@ -554,7 +550,7 @@ def getuser_key(name,cc):
return struct.pack('>LL', binascii.crc32(newname) & 0xffffffff,binascii.crc32(cc[-8:])& 0xffffffff)
def cli_main():
print(u"eRdr2Pml v{0}. Copyright © 20092012 The Dark Reverser et al.".format(__version__))
print("eRdr2Pml v{0}. Copyright © 20092020 The Dark Reverser et al.".format(__version__))
argv=unicode_argv()
try:
@@ -580,9 +576,9 @@ def cli_main():
if len(args)==3:
infile, name, cc = args
if make_pmlz:
outpath = os.path.splitext(infile)[0] + u".pmlz"
outpath = os.path.splitext(infile)[0] + ".pmlz"
else:
outpath = os.path.splitext(infile)[0] + u"_Source"
outpath = os.path.splitext(infile)[0] + "_Source"
elif len(args)==4:
infile, outpath, name, cc = args