Refactor decrypt book & add 'all' option to CLI

This commit is contained in:
Patrick Nicholls 2017-04-16 12:16:59 +12:00
parent ff03c68674
commit ceacdbbb1b
1 changed files with 31 additions and 27 deletions

View File

@ -665,41 +665,16 @@ class KoboFile(object):
contents = contents[:-padding] contents = contents[:-padding]
return contents return contents
def cli_main(): def decrypt_book(book, lib):
description = __about__
epilog = u"Parsing of arguments failed."
parser = argparse.ArgumentParser(prog=sys.argv[0], description=description, epilog=epilog)
parser.add_argument('--devicedir', default='/media/KOBOeReader', help="directory of connected Kobo device")
args = vars(parser.parse_args())
serials = []
devicedir = u""
if args['devicedir']:
devicedir = args['devicedir']
lib = KoboLibrary(serials, devicedir)
for i, book in enumerate(lib.books):
print u"{0}: {1}".format(i + 1, book.title)
num_string = raw_input(u"Convert book number... ")
try:
num = int(num_string)
book = lib.books[num - 1]
except (ValueError, IndexError):
exit()
print u"Converting {0}".format(book.title) print u"Converting {0}".format(book.title)
zin = zipfile.ZipFile(book.filename, "r") zin = zipfile.ZipFile(book.filename, "r")
# make filename out of Unicode alphanumeric and whitespace equivalents from title # make filename out of Unicode alphanumeric and whitespace equivalents from title
outname = u"{0}.epub".format(re.sub('[^\s\w]', '_', book.title, 0, re.UNICODE)) outname = u"{0}.epub".format(re.sub('[^\s\w]', '_', book.title, 0, re.UNICODE))
if (book.type == 'drm-free'): if (book.type == 'drm-free'):
print u"DRM-free book, conversion is not needed" print u"DRM-free book, conversion is not needed"
shutil.copyfile(book.filename, outname) shutil.copyfile(book.filename, outname)
print u"Book saved as {0}".format(os.path.join(os.getcwd(), outname)) print u"Book saved as {0}".format(os.path.join(os.getcwd(), outname))
exit(0) exit(0)
result = 1 result = 1
for userkey in lib.userkeys: for userkey in lib.userkeys:
print u"Trying key: {0}".format(userkey.encode('hex_codec')) print u"Trying key: {0}".format(userkey.encode('hex_codec'))
@ -722,13 +697,42 @@ def cli_main():
print u"Decryption failed." print u"Decryption failed."
zout.close() zout.close()
os.remove(outname) os.remove(outname)
zin.close() zin.close()
return result
def cli_main():
description = __about__
epilog = u"Parsing of arguments failed."
parser = argparse.ArgumentParser(prog=sys.argv[0], description=description, epilog=epilog)
parser.add_argument('--devicedir', default='/media/KOBOeReader', help="directory of connected Kobo device")
args = vars(parser.parse_args())
serials = []
devicedir = u""
if args['devicedir']:
devicedir = args['devicedir']
lib = KoboLibrary(serials, devicedir)
for i, book in enumerate(lib.books):
print u"{0}: {1}".format(i + 1, book.title)
print u"Or 'all'"
num_string = raw_input(u"Convert book number... ")
try:
num = int(num_string)
book = lib.books[num - 1]
except (ValueError, IndexError):
print u"Invalid choice. Exiting..."
exit()
result = decrypt_book(book, lib)
lib.close() lib.close()
if result != 0: if result != 0:
print u"Could not decrypt book with any of the keys found." print u"Could not decrypt book with any of the keys found."
return result return result
if __name__ == '__main__': if __name__ == '__main__':
sys.stdout=SafeUnbuffered(sys.stdout) sys.stdout=SafeUnbuffered(sys.stdout)
sys.stderr=SafeUnbuffered(sys.stderr) sys.stderr=SafeUnbuffered(sys.stderr)