Created
March 21, 2014 17:06
-
-
Save Mause/9690853 to your computer and use it in GitHub Desktop.
Interface with http://docverter.com
This file contains 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
import re | |
import requests | |
from os.path import basename | |
from urllib.parse import urljoin | |
def generate_files(filenames): | |
return [ | |
( | |
'input_files[]', | |
( | |
basename(filename), | |
file_data, | |
'application/octet-stream' | |
) | |
) | |
for filename, file_data in filenames | |
] | |
def get_args(): | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('input_files', nargs='+') | |
parser.add_argument('output_filename') | |
parser.add_argument( | |
'--mend-relative-links', | |
'-m' | |
) | |
return parser.parse_args() | |
LINK_RE = re.compile(r'\[([^\]]*)\]\(([^\)]*)\)') | |
def mend_relative_links(data, url): | |
return LINK_RE.sub( | |
lambda match: '[{}]({})'.format( | |
match.groups()[0], | |
urljoin(url, match.groups()[1]) | |
), | |
data | |
) | |
def get_pdf(files): | |
r = requests.post( | |
'http://c.docverter.com/convert', | |
files=files, | |
data={ | |
'from': 'markdown', | |
'to': 'pdf' | |
} | |
) | |
if r.headers['content-type'] != 'application/pdf': | |
raise Exception(r.json()['error']) | |
return r.content | |
def read(path): | |
if path.startswith('http'): | |
return requests.get(path).text | |
else: | |
with open(path) as fh: | |
return fh.read() | |
def main(): | |
args = get_args() | |
print(args.input_files) | |
input_files = [ | |
(filename, read(filename)) | |
for filename in args.input_files | |
] | |
if args.mend_relative_links is not None: | |
input_files = [ | |
( | |
filename, | |
mend_relative_links(file_data, args.mend_relative_links) | |
) | |
for filename, file_data in input_files | |
] | |
files = generate_files(input_files) | |
pdf = get_pdf(files) | |
with open(args.output_filename, 'wb') as fh: | |
fh.write(pdf) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment