Skip to content

Instantly share code, notes, and snippets.

@cvanelteren
Last active May 25, 2020 12:55
Show Gist options
  • Save cvanelteren/3821b9728407f0991c47bdee35784600 to your computer and use it in GitHub Desktop.
Save cvanelteren/3821b9728407f0991c47bdee35784600 to your computer and use it in GitHub Desktop.
convert using k2pdfopt to e-reader friendly format
from subprocess import run
from multiprocessing import cpu_count
import os, click
@click.command()
@click.option('--source', default = "~/Phd/ereader")
@click.option('--target', default = "~/Calibre Library")
@click.option('--args', default = f"-as -w -ocr -nt {cpu_count()} -ocr -x -ui-")
def run_reader(source, target, args):
eReaderConverter(source, target, args).convert()
return
class eReaderConverter:
def __init__(self,
source,
target,
convertargs):
self.target = os.path.expanduser(target)
self.source = os.path.expanduser(source)
self.convertargs = "\t".join(i for i in convertargs.split())
def convert(self):
convert = self.novel_reads()
# add full path
convert = " ".join(str(os.path.join(self.source, i)) for i in convert)
# run and move
print(f"k2pdfopt\t{self.convertargs}\t{convert}".split('\t'))
run(f"k2pdfopt\t{self.convertargs}\t{convert}".split('\t'))
run(f"mv {os.path.join(self.target, '*k2pdfopt.*')} {self.source}".split())
def novel_reads(self):
toread = set(os.listdir(self.source))
on_reader = set(os.listdir(self.target))
return toread - on_reader
if __name__ == "__main__":
run_reader()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment