Created
November 6, 2016 22:38
-
-
Save EsotericAlgorithm/26e8417ce13e9495daa46dbd2a94255c to your computer and use it in GitHub Desktop.
A script to process azw3, epub, and mobi into text files using calibre ebook-convert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
directories = ['azw3', 'epub', 'mobi'] | |
conversion_space = "convert" | |
sink = open('/dev/null', 'w') | |
def convert_book(input, output): | |
subprocess.call("ebook-convert" + " " + input + " " + output, shell=True, stdout=sink, stderr=sink) | |
for directory in directories: | |
conversion_directory = os.path.join(directory, conversion_space) | |
if not os.path.exists(conversion_directory): | |
os.makedirs(conversion_directory) | |
print("Creating {} directory".format(directory)) | |
# Convert all files ending in the directory name | |
files_to_convert = [file for file in os.listdir(directory) if directory in file] | |
for file in files_to_convert: | |
# Remove extension for conversion to TXT | |
base = file.split("." + directory)[0] | |
output = base + ".txt" | |
full_input = shlex.quote(os.path.join(directory, file)) | |
full_output = shlex.quote(os.path.join(directory, os.path.join(conversion_space, output))) | |
print("Creating {}".format(output)) | |
convert_book(full_input, full_output) | |
sink.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment