]> git.lizzy.rs Git - nhentai.git/commitdiff
Merge branch 'pull/221' into master
authorRicter Zheng <RicterZheng@gmail.com>
Mon, 7 Jun 2021 08:01:54 +0000 (16:01 +0800)
committerGitHub <noreply@github.com>
Mon, 7 Jun 2021 08:01:54 +0000 (16:01 +0800)
1  2 
nhentai/cmdline.py
nhentai/command.py
nhentai/utils.py

diff --combined nhentai/cmdline.py
index 9107c208253e25517447c567107e1b55bd129912,c7246d20d1e23f2425b726e5eaa8b363db09691c..c953e871c04b3d43c638801aa6a07d5ae1d14b30
@@@ -89,6 -89,7 +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('--format', type='string', dest='name_format', action='store',
                        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
      parser.add_option('--html', dest='html_viewer', action='store_true',
                        help='clean download history')
      parser.add_option('--template', dest='viewer_template', action='store',
                        help='set viewer template', default='')
 +      
 +    parser.add_option('--meta', dest='generate_metadata', action='store_true', help='generate a Metadata File in HDoujin Format')     
 +                                        
  
      try:
          sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
          logger.critical('Maximum number of used threads is 15')
          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
diff --combined nhentai/command.py
index 76365d8b8855d4674b2b9a8a627fd2a0d2fa168d,92b6793e8a950d2b9b780bf46d168266baac024c..fe746cb014f80f1fba8e72ebadb85ab02ee32d90
@@@ -13,7 -13,7 +13,7 @@@ from nhentai.doujinshi import Doujinsh
  from nhentai.downloader import Downloader
  from nhentai.logger import logger
  from nhentai.constant import BASE_URL
 -from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, \
 +from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, generate_metadatafile, \
      paging, check_cookie, signal_handler, DB
  
  
@@@ -84,22 -84,15 +84,25 @@@ def main()
              if (i + 1) % 10 == 0:
                  logger.info('Progress: %d / %d' % (i + 1, len(doujinshi_ids)))
  
 +      
 +              
      if not options.is_show:
          downloader = Downloader(path=options.output_dir, size=options.threads,
                                  timeout=options.timeout, delay=options.delay)
  
          for doujinshi in doujinshi_list:
+             if not options.dryrun:
+                 doujinshi.downloader = downloader
+                 doujinshi.download()
 +                                              
 +            doujinshi.downloader = downloader
 +            doujinshi.download()
 +                      
 +            if options.generate_metadata:
 +              table=doujinshi.table            
 +              generate_metadatafile(options.output_dir,table,doujinshi)
 +                      
              if options.is_save_download_history:
                  with DB() as db:
                      db.add_one(doujinshi.id)
  
      else:
          [doujinshi.show() for doujinshi in doujinshi_list]
 +        
  
  
  signal.signal(signal.SIGINT, signal_handler)
diff --combined nhentai/utils.py
index 34c36b733996e9b46c795d5ab0510028d385d634,3364d0d4b675b40b6a13141d47bf9cb170dd18c0..7524e926d62cdc0ea419663f7082a80a5704da2e
@@@ -20,7 -20,11 +20,11 @@@ def request(method, url, **kwargs)
          'User-Agent': 'nhentai command line client (https://github.com/RicterZ/nhentai)',
          'Cookie': constant.CONFIG['cookie']
      })
-     return getattr(session, method)(url, proxies=constant.CONFIG['proxy'], verify=False, **kwargs)
+     if not kwargs.get('proxies', None):
+         kwargs['proxies'] = constant.CONFIG['proxy']
+     return getattr(session, method)(url, verify=False, **kwargs)
  
  
  def check_cookie():
@@@ -70,6 -74,13 +74,13 @@@ def generate_html(output_dir='.', douji
      else:
          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.sort()
  
@@@ -194,7 -205,7 +205,7 @@@ def generate_cbz(output_dir='.', doujin
  def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
      try:
          import img2pdf
-         
          """Write images to a PDF file using img2pdf."""
          if doujinshi_obj is not None:
              doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
              shutil.rmtree(doujinshi_dir, ignore_errors=True)
  
          logger.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir))
-         
      except ImportError:
          logger.error("Please install img2pdf package by using pip.")
  
@@@ -279,58 -290,6 +290,58 @@@ def paging(page_string)
  
      return page_list
  
 +def generate_metadatafile(output_dir, table, doujinshi_obj=None):
 +    logger.info("Writing Metadata Info")
 +   
 +      
 +    if doujinshi_obj is not None:
 +      doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
 +    else:
 +      doujinshi_dir = '.'
 +    
 +    logger.info(doujinshi_dir)        
 +      
 +    f = open(os.path.join(doujinshi_dir, 'info.txt'), "w", encoding="utf-8")
 +
 +    fields = ["TITLE", "ORIGINAL TITLE", "AUTHOR", "ARTIST", "CIRCLE", "SCANLATOR", "TRANSLATOR", "PUBLISHER", "DESCRIPTION", "STATUS", "CHAPTERS", "PAGES", "TAGS", "TYPE", "LANGUAGE", "RELEASED", "READING DIRECTION", "CHARACTERS", "SERIES", "PARODY", "URL"]
 +
 +               
 +    for i in range(21):
 +      f.write("%s: " % fields[i])
 +        
 +      if(i==19):
 +        f.write("%s" % table[0][1])
 +
 +      if(i==0):
 +        f.write("%s" % table[1][1])
 +    
 +      if(i==1):
 +        f.write("%s" % table[2][1])
 +    
 +      if(i==17):
 +        f.write("%s" % table[3][1])
 +    
 +      if(i==2):
 +        f.write("%s" % table[4][1])
 +    
 +      if(i==14):
 +        f.write("%s" % table[5][1])
 +    
 +      if(i==12):
 +        f.write("%s" % table[6][1])
 +    
 +      if(i==20):
 +        f.write("%s" % table[7][1])
 +    
 +      if(i==11):
 +        f.write("%s" % table[8][1])
 +    
 +      f.write("\n")
 +    
 +    f.close()
 +    
 +    
 +    
  
  class DB(object):
      conn = None
      def get_all(self):
          data = self.cur.execute('SELECT id FROM download_history')
          return [i[0] for i in data]
 +
 +        
 +