def get_callback(file_meta, user):
def callback(request_id, response, exception):
if exception is None:
file_name = file_meta['name'].replace(' ', '-')
created_time_str = file_meta['createdTime']
created_time = dateutil.parser.parse(created_time_str)
docx_dir = '{0}/{1}/docx'.format(settings.CONVERSION_DIR_ROOT,
user.email)
ensure_dir(docx_dir)
# write response to a docx file
f = open('{0}/{1}.docx'.format(docx_dir, file_name), 'w+')
f.write(response)
f.close()
md_dir = '{0}/{1}/md'.format(settings.CONVERSION_DIR_ROOT,
user.email)
ensure_dir(md_dir)
# construct yaml matter
template = loader.get_template('post_header_template.html')
yaml_matter = template.render({
"title": file_name,
"date": created_time_str,
"categories": ""
})
outputfile = '{0}/{1}-{2}.markdown'.format(md_dir,
created_time.strftime('%Y-%m-%d'),
slugify(file_name))
# convert file
pypandoc.convert_file(
'{0}/{1}.docx'.format(docx_dir, file_name),
'md',
outputfile=outputfile)
# prepend yaml_matter to converted file
# TODO: do this in-place, rather than in-memory
prepend2file(outputfile, yaml_matter)
else:
# handle error
pass
return callback
评论列表
文章目录