]> git.lizzy.rs Git - nhentai.git/commitdiff
fix format pull/221
authorRicterZ <ricterzheng@gmail.com>
Mon, 7 Jun 2021 09:17:54 +0000 (17:17 +0800)
committerRicterZ <ricterzheng@gmail.com>
Mon, 7 Jun 2021 09:17:54 +0000 (17:17 +0800)
nhentai/cmdline.py
nhentai/command.py
nhentai/doujinshi.py
nhentai/utils.py

index c953e871c04b3d43c638801aa6a07d5ae1d14b30..3085f2aa8c6b23db90447f0728fbf59250714d64 100644 (file)
@@ -86,7 +86,7 @@ def cmd_parser():
                       help='slow down between downloading every doujinshi')
     parser.add_option('--proxy', type='string', dest='proxy', action='store',
                       help='store a proxy, for example: -p \'http://127.0.0.1:1080\'')
-    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',
                       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.')
@@ -104,6 +104,8 @@ def cmd_parser():
                       help='generate PDF file')
     parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
                       help='remove downloaded doujinshi dir when generated CBZ or PDF file.')
+    parser.add_option('--meta', dest='generate_metadata', action='store_true',
+                      help='generate a metadata file in doujinshi format')
 
     # nhentai options
     parser.add_option('--cookie', type='str', dest='cookie', action='store',
@@ -118,9 +120,6 @@ def cmd_parser():
                       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]
@@ -207,7 +206,7 @@ def cmd_parser():
         parser.print_help()
         exit(1)
 
-    if not args.keyword and not args.id and not  args.favorites:
+    if not args.keyword and not args.id and not args.favorites:
         parser.print_help()
         exit(1)
 
index fe746cb014f80f1fba8e72ebadb85ab02ee32d90..a6c91d050bb598620ceb9b5dc60fba28f54535e6 100644 (file)
@@ -13,7 +13,7 @@ from nhentai.doujinshi import Doujinshi
 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, generate_metadatafile, \
+from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, generate_metadata_file, \
     paging, check_cookie, signal_handler, DB
 
 
@@ -84,8 +84,6 @@ 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)
@@ -94,14 +92,13 @@ def main():
             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)
-                       
+                table = doujinshi.table
+                generate_metadata_file(options.output_dir, table, doujinshi)
 
             if options.is_save_download_history:
                 with DB() as db:
@@ -124,11 +121,9 @@ def main():
 
     else:
         [doujinshi.show() for doujinshi in doujinshi_list]
-        
 
 
 signal.signal(signal.SIGINT, signal_handler)
 
-
 if __name__ == '__main__':
     main()
index 06a7706d61a75506abe62a9e0308fdb053e2ae23..3dea1044daf2aae62adf82f4f8765242cf732aaf 100644 (file)
@@ -6,7 +6,6 @@ from nhentai.constant import DETAIL_URL, IMAGE_URL
 from nhentai.logger import logger
 from nhentai.utils import format_filename
 
-
 EXT_MAP = {
     'j': 'jpg',
     'p': 'png',
@@ -41,9 +40,8 @@ class Doujinshi(object):
         name_format = name_format.replace('%t', self.name)
         name_format = name_format.replace('%s', self.info.subtitle)
         self.filename = format_filename(name_format)
-               
-               
-        self.table =[
+
+        self.table = [
             ["Parodies", self.info.parodies],
             ["Doujinshi", self.name],
             ["Subtitle", self.info.subtitle],
@@ -54,15 +52,13 @@ class Doujinshi(object):
             ["URL", self.url],
             ["Pages", self.pages],
         ]
-               
 
     def __repr__(self):
         return '<Doujinshi: {0}>'.format(self.name)
 
     def show(self):
-        
+
         logger.info(u'Print doujinshi information of {0}\n{1}'.format(self.id, tabulate(self.table)))
-        
 
     def download(self):
         logger.info('Starting to download doujinshi: %s' % self.name)
@@ -73,7 +69,7 @@ class Doujinshi(object):
                 logger.warning('Page count and ext count do not equal')
 
             for i in range(1, min(self.pages, len(self.ext)) + 1):
-                download_queue.append('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i, self.ext[i-1]))
+                download_queue.append('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i, self.ext[i - 1]))
 
             self.downloader.download(download_queue, self.filename)
 
index 7524e926d62cdc0ea419663f7082a80a5704da2e..f7532bf12e3900bbde6683f49fd5b9676d2fe2ee 100644 (file)
@@ -88,7 +88,7 @@ def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
         if not os.path.splitext(image)[1] in ('.jpg', '.png'):
             continue
 
-        image_html += '<img src="{0}" class="image-item"/>\n'\
+        image_html += '<img src="{0}" class="image-item"/>\n' \
             .format(image)
     html = readfile('viewer/{}/index.html'.format(template))
     css = readfile('viewer/{}/styles.css'.format(template))
@@ -169,7 +169,7 @@ def generate_main_html(output_dir='./'):
         else:
             with open('./main.html', 'wb') as f:
                 f.write(data.encode('utf-8'))
-        shutil.copy(os.path.dirname(__file__)+'/viewer/logo.png', './')
+        shutil.copy(os.path.dirname(__file__) + '/viewer/logo.png', './')
         set_js_database()
         logger.log(
             15, 'Main Viewer has been written to \'{0}main.html\''.format(output_dir))
@@ -235,6 +235,7 @@ def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
     except ImportError:
         logger.error("Please install img2pdf package by using pip.")
 
+
 def unicode_truncate(s, length, encoding='utf-8'):
     """https://stackoverflow.com/questions/1809531/truncating-unicode-so-it-fits-a-maximum-size-when-encoded-for-wire-transfer
     """
@@ -251,7 +252,7 @@ def format_filename(s):
     """
     # maybe you can use `--format` to select a suitable filename
     ban_chars = '\\\'/:,;*?"<>|\t'
-    filename = s.translate(str.maketrans(ban_chars, ' '*len(ban_chars))).strip()
+    filename = s.translate(str.maketrans(ban_chars, ' ' * len(ban_chars))).strip()
     filename = ' '.join(filename.split())
     print(repr(filename))
 
@@ -282,7 +283,7 @@ def paging(page_string):
             start, end = i.split('-')
             if not (start.isdigit() and end.isdigit()):
                 raise Exception('Invalid page number')
-            page_list.extend(list(range(int(start), int(end)+1)))
+            page_list.extend(list(range(int(start), int(end) + 1)))
         else:
             if not i.isdigit():
                 raise Exception('Invalid page number')
@@ -290,58 +291,34 @@ def paging(page_string):
 
     return page_list
 
-def generate_metadatafile(output_dir, table, doujinshi_obj=None):
-    logger.info("Writing Metadata Info")
-   
-       
+
+def generate_metadata_file(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)
+        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")
-    
+        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']
+    special_fields = ['PARODY', 'TITLE', 'ORIGINAL TITLE', 'CHARACTERS', 'AUTHOR',
+                      'LANGUAGE', 'TAGS', 'URL', 'PAGES']
+
+    for i in range(len(fields)):
+        f.write('{}: '.format(fields[i]))
+        if fields[i] in special_fields:
+            f.write(str(table[special_fields.index(fields[i])][1]))
+        f.write('\n')
+
     f.close()
-    
-    
-    
+
 
 class DB(object):
     conn = None
@@ -368,6 +345,3 @@ class DB(object):
     def get_all(self):
         data = self.cur.execute('SELECT id FROM download_history')
         return [i[0] for i in data]
-
-        
-        
\ No newline at end of file