mirror of
				https://github.com/RicterZ/nhentai.git
				synced 2025-11-03 18:50:53 +01:00 
			
		
		
		
	Add the option to perform a dry-run and only download meta-data / generate file structure
This commit is contained in:
		@@ -89,6 +89,7 @@ def cmd_parser():
 | 
				
			|||||||
    parser.add_option('--file',  '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
 | 
					    parser.add_option('--file',  '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
 | 
				
			||||||
    parser.add_option('--format', type='string', dest='name_format', action='store',
 | 
					    parser.add_option('--format', type='string', dest='name_format', action='store',
 | 
				
			||||||
                      help='format the saved folder name', default='[%i][%a][%t]')
 | 
					                      help='format the saved folder name', default='[%i][%a][%t]')
 | 
				
			||||||
 | 
					    parser.add_option('--dry-run', '-r', action='store_true', dest='dryrun', help='Dry run, skip file download.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # generate options
 | 
					    # generate options
 | 
				
			||||||
    parser.add_option('--html', dest='html_viewer', action='store_true',
 | 
					    parser.add_option('--html', dest='html_viewer', action='store_true',
 | 
				
			||||||
@@ -214,4 +215,8 @@ def cmd_parser():
 | 
				
			|||||||
        logger.critical('Maximum number of used threads is 15')
 | 
					        logger.critical('Maximum number of used threads is 15')
 | 
				
			||||||
        exit(1)
 | 
					        exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if args.dryrun and (args.is_cbz or args.is_pdf):
 | 
				
			||||||
 | 
					        logger.critical('Cannot generate PDF or CBZ during dry-run')
 | 
				
			||||||
 | 
					        exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return args
 | 
					    return args
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -89,9 +89,10 @@ def main():
 | 
				
			|||||||
                                timeout=options.timeout, delay=options.delay)
 | 
					                                timeout=options.timeout, delay=options.delay)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for doujinshi in doujinshi_list:
 | 
					        for doujinshi in doujinshi_list:
 | 
				
			||||||
 | 
					            if not options.dryrun:
 | 
				
			||||||
 | 
					                doujinshi.downloader = downloader
 | 
				
			||||||
 | 
					                doujinshi.download()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            doujinshi.downloader = downloader
 | 
					 | 
				
			||||||
            doujinshi.download()
 | 
					 | 
				
			||||||
            if options.is_save_download_history:
 | 
					            if options.is_save_download_history:
 | 
				
			||||||
                with DB() as db:
 | 
					                with DB() as db:
 | 
				
			||||||
                    db.add_one(doujinshi.id)
 | 
					                    db.add_one(doujinshi.id)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -70,6 +70,13 @@ def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
 | 
				
			|||||||
    else:
 | 
					    else:
 | 
				
			||||||
        doujinshi_dir = '.'
 | 
					        doujinshi_dir = '.'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if not os.path.exists(doujinshi_dir):
 | 
				
			||||||
 | 
					        logger.warning('Path \'{0}\' does not exist, creating.'.format(doujinshi_dir))
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            os.makedirs(doujinshi_dir)
 | 
				
			||||||
 | 
					        except EnvironmentError as e:
 | 
				
			||||||
 | 
					            logger.critical('{0}'.format(str(e)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    file_list = os.listdir(doujinshi_dir)
 | 
					    file_list = os.listdir(doujinshi_dir)
 | 
				
			||||||
    file_list.sort()
 | 
					    file_list.sort()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -194,7 +201,7 @@ def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_
 | 
				
			|||||||
def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
 | 
					def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        import img2pdf
 | 
					        import img2pdf
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
        """Write images to a PDF file using img2pdf."""
 | 
					        """Write images to a PDF file using img2pdf."""
 | 
				
			||||||
        if doujinshi_obj is not None:
 | 
					        if doujinshi_obj is not None:
 | 
				
			||||||
            doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
 | 
					            doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
 | 
				
			||||||
@@ -220,7 +227,7 @@ def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
 | 
				
			|||||||
            shutil.rmtree(doujinshi_dir, ignore_errors=True)
 | 
					            shutil.rmtree(doujinshi_dir, ignore_errors=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        logger.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir))
 | 
					        logger.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir))
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
    except ImportError:
 | 
					    except ImportError:
 | 
				
			||||||
        logger.error("Please install img2pdf package by using pip.")
 | 
					        logger.error("Please install img2pdf package by using pip.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -236,7 +243,7 @@ def format_filename(s):
 | 
				
			|||||||
    It used to be a whitelist approach allowed only alphabet and a part of symbols.
 | 
					    It used to be a whitelist approach allowed only alphabet and a part of symbols.
 | 
				
			||||||
    but most doujinshi's names include Japanese 2-byte characters and these was rejected.
 | 
					    but most doujinshi's names include Japanese 2-byte characters and these was rejected.
 | 
				
			||||||
    so it is using blacklist approach now.
 | 
					    so it is using blacklist approach now.
 | 
				
			||||||
    if filename include forbidden characters (\'/:,;*?"<>|) ,it replace space character(' '). 
 | 
					    if filename include forbidden characters (\'/:,;*?"<>|) ,it replace space character(' ').
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    # maybe you can use `--format` to select a suitable filename
 | 
					    # maybe you can use `--format` to select a suitable filename
 | 
				
			||||||
    ban_chars = '\\\'/:,;*?"<>|\t'
 | 
					    ban_chars = '\\\'/:,;*?"<>|\t'
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user