11#!/usr/bin/env python3
22
33import argparse
4- from os import path
4+ from os import path , listdir
5+ import tempfile
6+ from zipfile import ZipFile
57from modules .doi import Doi
68from modules .pdf import Pdf
79from modules .metadata import Metadata
@@ -23,6 +25,10 @@ def do_split(m, p, output_dir, doi):
2325 Metadata .write_metadata (doi_metadata , output_file_path )
2426
2527
28+ def get_tmp_dir ():
29+ return tempfile .mkdtemp ()
30+
31+
2632def run ():
2733 parser = argparse .ArgumentParser (description = 'chapter-splitter' )
2834
@@ -33,25 +39,39 @@ def run():
3339 parser .add_argument ('-d' , '--doi' ,
3440 help = 'The DOI (at book-level) you wish to parse' ,
3541 required = True )
42+ parser .add_argument ('-c' , '--compress-output' , dest = 'compress' ,
43+ action = 'store_true' ,
44+ help = 'If set it will output a single zip file' )
3645
3746 args = parser .parse_args ()
3847
48+ out_dir = args .output_folder
49+ tmp_dir = out_dir if not args .compress else get_tmp_dir ()
50+
3951 # Check parsed arguments
4052 file_checks (args .input_file )
41- path_checks (args . output_folder )
53+ path_checks (out_dir )
4254
4355 # Check dependencies
4456 dependencies_checks ()
4557
4658 # Discover chapter-level DOIs of the supplied --doi value
47- d = Doi (args .doi )
59+ d = Doi (args .doi . lower () )
4860 ch_dois = d .discover_ch_dois ()
4961
5062 m = Metadata ()
51- p = Pdf (args .input_file , args . output_folder )
63+ p = Pdf (args .input_file , tmp_dir )
5264
5365 for doi in ch_dois :
54- do_split (m , p , args .output_folder , doi )
66+ do_split (m , p , tmp_dir , doi )
67+
68+ if args .compress :
69+ out_file = '{}/{}.zip' .format (out_dir , d .book_level_doi_suffix )
70+ suffix = '_original'
71+ files = filter (lambda w : not w .endswith (suffix ), listdir (tmp_dir ))
72+ with ZipFile (out_file , 'w' ) as zipfile :
73+ for file in files :
74+ zipfile .write ('{}/{}' .format (tmp_dir , file ), file )
5575
5676
5777if __name__ == '__main__' :
0 commit comments