From ceacdbbb1b25a9f5ea396ef8cd6153ee4c4d303e Mon Sep 17 00:00:00 2001 From: Patrick Nicholls Date: Sun, 16 Apr 2017 12:16:59 +1200 Subject: [PATCH 1/3] Refactor decrypt book & add 'all' option to CLI --- Other_Tools/Kobo/obok.py | 58 +++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/Other_Tools/Kobo/obok.py b/Other_Tools/Kobo/obok.py index d2188e3..7a50ba9 100644 --- a/Other_Tools/Kobo/obok.py +++ b/Other_Tools/Kobo/obok.py @@ -665,41 +665,16 @@ class KoboFile(object): contents = contents[:-padding] return contents -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) - - num_string = raw_input(u"Convert book number... ") - try: - num = int(num_string) - book = lib.books[num - 1] - except (ValueError, IndexError): - exit() - +def decrypt_book(book, lib): print u"Converting {0}".format(book.title) - zin = zipfile.ZipFile(book.filename, "r") # 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)) - if (book.type == 'drm-free'): print u"DRM-free book, conversion is not needed" shutil.copyfile(book.filename, outname) print u"Book saved as {0}".format(os.path.join(os.getcwd(), outname)) exit(0) - result = 1 for userkey in lib.userkeys: print u"Trying key: {0}".format(userkey.encode('hex_codec')) @@ -722,13 +697,42 @@ def cli_main(): print u"Decryption failed." zout.close() os.remove(outname) - 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() if result != 0: print u"Could not decrypt book with any of the keys found." return result + if __name__ == '__main__': sys.stdout=SafeUnbuffered(sys.stdout) sys.stderr=SafeUnbuffered(sys.stderr) From 6f0c36b67abb574136d973943ea7d54678bc52b5 Mon Sep 17 00:00:00 2001 From: Patrick Nicholls Date: Sun, 16 Apr 2017 12:27:34 +1200 Subject: [PATCH 2/3] Implement decrypting of all books --- Other_Tools/Kobo/obok.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Other_Tools/Kobo/obok.py b/Other_Tools/Kobo/obok.py index 7a50ba9..37ee286 100644 --- a/Other_Tools/Kobo/obok.py +++ b/Other_Tools/Kobo/obok.py @@ -674,7 +674,7 @@ def decrypt_book(book, lib): print u"DRM-free book, conversion is not needed" shutil.copyfile(book.filename, outname) print u"Book saved as {0}".format(os.path.join(os.getcwd(), outname)) - exit(0) + return 0 result = 1 for userkey in lib.userkeys: print u"Trying key: {0}".format(userkey.encode('hex_codec')) @@ -718,19 +718,23 @@ def cli_main(): 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() + choice = raw_input(u"Convert book number... ") + if choice == u'all': + books = list(lib.books) + else: + try: + num = int(choice) + books = [lib.books[num - 1]] + except (ValueError, IndexError): + print u"Invalid choice. Exiting..." + exit() - result = decrypt_book(book, lib) + results = [decrypt_book(book, lib) for book in books] lib.close() - if result != 0: + overall_result = all(result != 0 for result in results) + if overall_result != 0: print u"Could not decrypt book with any of the keys found." - return result + return overall_result if __name__ == '__main__': From 691a3d6955f4a18e264c1eaa56c9a563a65d2dc2 Mon Sep 17 00:00:00 2001 From: Patrick Nicholls Date: Mon, 24 Apr 2017 21:48:53 +1200 Subject: [PATCH 3/3] Added --all flag to sys.args --- Other_Tools/Kobo/obok.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) mode change 100644 => 100755 Other_Tools/Kobo/obok.py diff --git a/Other_Tools/Kobo/obok.py b/Other_Tools/Kobo/obok.py old mode 100644 new mode 100755 index 37ee286..033c67a --- a/Other_Tools/Kobo/obok.py +++ b/Other_Tools/Kobo/obok.py @@ -706,6 +706,7 @@ def cli_main(): 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") + parser.add_argument('--all', action='store_true', help="flag for converting all books on device") args = vars(parser.parse_args()) serials = [] devicedir = u"" @@ -714,20 +715,23 @@ def cli_main(): lib = KoboLibrary(serials, devicedir) - for i, book in enumerate(lib.books): - print u"{0}: {1}".format(i + 1, book.title) - print u"Or 'all'" - - choice = raw_input(u"Convert book number... ") - if choice == u'all': - books = list(lib.books) + if args['all']: + books = lib.books else: - try: - num = int(choice) - books = [lib.books[num - 1]] - except (ValueError, IndexError): - print u"Invalid choice. Exiting..." - exit() + for i, book in enumerate(lib.books): + print u"{0}: {1}".format(i + 1, book.title) + print u"Or 'all'" + + choice = raw_input(u"Convert book number... ") + if choice == u'all': + books = list(lib.books) + else: + try: + num = int(choice) + books = [lib.books[num - 1]] + except (ValueError, IndexError): + print u"Invalid choice. Exiting..." + exit() results = [decrypt_book(book, lib) for book in books] lib.close()