def extract_ebook(plain_output: bool) -> int: """ Entry point for `se extract-ebook` """ parser = argparse.ArgumentParser(description="Extract an .epub, .mobi, or .azw3 ebook into ./FILENAME.extracted/ or a target directory.") parser.add_argument("-o", "--output-dir", type=str, help="a target directory to extract into") parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity") parser.add_argument("targets", metavar="TARGET", nargs="+", help="an epub, mobi, or azw3 file") args = parser.parse_args() console = Console(highlight=False, theme=se.RICH_THEME, force_terminal=se.is_called_from_parallel()) # Syntax highlighting will do weird things when printing paths; force_terminal prints colors when called from GNU Parallel if args.output_dir and len(args.targets) > 1: se.print_error("The [bash]--output-dir[/] option can’t be used when more than one ebook target is specified.", plain_output=plain_output) return se.InvalidArgumentsException.code for target in args.targets: target = Path(target).resolve() if args.verbose: console.print(se.prep_output(f"Processing [path][link=file://{target}]{target}[/][/] ...", plain_output), end="") if not path.isfile(target): se.print_error(f"Not a file: [path][link=file://{target}]{target}[/][/].", plain_output=plain_output) return se.InvalidInputException.code if args.output_dir is None: extracted_path = Path(target.name + ".extracted") else: extracted_path = Path(args.output_dir) if extracted_path.exists(): se.print_error(f"Directory already exists: [path][link=file://{extracted_path}]{extracted_path}[/][/].", plain_output=plain_output) return se.FileExistsException.code with open(target, "rb") as binary_file: file_bytes = binary_file.read() if _is_mobi(file_bytes): # kindleunpack uses print() so just capture that output here old_stdout = sys.stdout sys.stdout = TextIOWrapper(BytesIO(), sys.stdout.encoding) kindleunpack.unpackBook(str(target), str(extracted_path)) # Restore stdout sys.stdout.close() sys.stdout = old_stdout elif _is_epub(file_bytes): with zipfile.ZipFile(target, "r") as file: file.extractall(extracted_path) else: se.print_error("File doesn’t look like an epub, mobi, or azw3 file.") return se.InvalidFileException.code if args.verbose: console.print(" OK") return 0
def extract_ebook() -> int: """ Entry point for `se extract-ebook` """ import zipfile from io import TextIOWrapper, BytesIO import magic from se.vendor.kindleunpack import kindleunpack parser = argparse.ArgumentParser(description="Extract an epub, mobi, or azw3 ebook into ./FILENAME.extracted/ or a target directory.") parser.add_argument("-o", "--output-dir", type=str, help="a target directory to extract into") parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity") parser.add_argument("targets", metavar="TARGET", nargs="+", help="an epub, mobi, or azw3 file") args = parser.parse_args() for target in args.targets: target = Path(target).resolve() if args.verbose: print("Processing {} ...".format(target), end="", flush=True) if args.output_dir is None: extracted_path = Path(target.name + ".extracted") else: extracted_path = Path(args.output_dir) if extracted_path.exists(): se.print_error("Directory already exists: {}".format(extracted_path)) return se.FileExistsException.code mime_type = magic.from_file(str(target)) if "Mobipocket E-book" in mime_type: # kindleunpack uses print() so just capture that output here old_stdout = sys.stdout sys.stdout = TextIOWrapper(BytesIO(), sys.stdout.encoding) kindleunpack.unpackBook(target, extracted_path) # Restore stdout sys.stdout.close() sys.stdout = old_stdout elif "EPUB document" in mime_type: with zipfile.ZipFile(target, "r") as file: file.extractall(extracted_path) else: se.print_error("Couldn’t understand file type: {}".format(mime_type)) return se.InvalidFileException.code if args.verbose: print(" OK") return 0
def extract_ebook() -> int: """ Entry point for `se extract-ebook` """ parser = argparse.ArgumentParser( description= "Extract an epub, mobi, or azw3 ebook into ./FILENAME.extracted/ or a target directory." ) parser.add_argument("-o", "--output-dir", type=str, help="a target directory to extract into") parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity") parser.add_argument("targets", metavar="TARGET", nargs="+", help="an epub, mobi, or azw3 file") args = parser.parse_args() for target in args.targets: target = Path(target).resolve() if args.verbose: print(f"Processing {target} ...", end="", flush=True) if not path.isfile(target): se.print_error(f"Not a file: `{target}`") return se.InvalidInputException.code if args.output_dir is None: extracted_path = Path(target.name + ".extracted") else: extracted_path = Path(args.output_dir) if extracted_path.exists(): se.print_error(f"Directory already exists: `{extracted_path}`") return se.FileExistsException.code with open(target, "rb") as binary_file: file_bytes = binary_file.read() if _is_mobi(file_bytes): # kindleunpack uses print() so just capture that output here old_stdout = sys.stdout sys.stdout = TextIOWrapper(BytesIO(), sys.stdout.encoding) kindleunpack.unpackBook(str(target), str(extracted_path)) # Restore stdout sys.stdout.close() sys.stdout = old_stdout elif _is_epub(file_bytes): with zipfile.ZipFile(target, "r") as file: file.extractall(extracted_path) else: se.print_error( "File doesn’t look like an epub, mobi, or azw3 file.") return se.InvalidFileException.code if args.verbose: print(" OK") return 0