]> git.lizzy.rs Git - nhentai.git/blobdiff - nhentai/utils.py
fix format
[nhentai.git] / nhentai / utils.py
index 1ee6e7e485f680b5697911dead207015b01e9829..f7532bf12e3900bbde6683f49fd5b9676d2fe2ee 100644 (file)
@@ -1,10 +1,8 @@
 # coding: utf-8
-from __future__ import unicode_literals, print_function
 
 import sys
 import re
 import os
-import string
 import zipfile
 import shutil
 import requests
@@ -22,7 +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():
@@ -72,6 +74,13 @@ def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
     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()
 
@@ -79,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))
@@ -160,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))
@@ -168,7 +177,7 @@ def generate_main_html(output_dir='./'):
         logger.warning('Writing Main Viewer failed ({})'.format(str(e)))
 
 
-def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=False):
+def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=True):
     if doujinshi_obj is not None:
         doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
         if write_comic_info:
@@ -196,34 +205,42 @@ 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):
     try:
         import img2pdf
-    except ImportError:
-        logger.error("Please install img2pdf package by using pip.")
 
-    """Write images to a PDF file using img2pdf."""
-    if doujinshi_obj is not None:
-        doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
-        pdf_filename = os.path.join(
-            os.path.join(doujinshi_dir, '..'),
-            '{}.pdf'.format(doujinshi_obj.filename)
-        )
-    else:
-        pdf_filename = './doujinshi.pdf'
-        doujinshi_dir = '.'
+        """Write images to a PDF file using img2pdf."""
+        if doujinshi_obj is not None:
+            doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
+            pdf_filename = os.path.join(
+                os.path.join(doujinshi_dir, '..'),
+                '{}.pdf'.format(doujinshi_obj.filename)
+            )
+        else:
+            pdf_filename = './doujinshi.pdf'
+            doujinshi_dir = '.'
 
-    file_list = os.listdir(doujinshi_dir)
-    file_list.sort()
+        file_list = os.listdir(doujinshi_dir)
+        file_list.sort()
 
-    logger.info('Writing PDF file to path: {}'.format(pdf_filename))
-    with open(pdf_filename, 'wb') as pdf_f:
-        full_path_list = (
-            [os.path.join(doujinshi_dir, image) for image in file_list]
-        )
-        pdf_f.write(img2pdf.convert(full_path_list))
+        logger.info('Writing PDF file to path: {}'.format(pdf_filename))
+        with open(pdf_filename, 'wb') as pdf_f:
+            full_path_list = (
+                [os.path.join(doujinshi_dir, image) for image in file_list]
+            )
+            pdf_f.write(img2pdf.convert(full_path_list))
+
+        if rm_origin_dir:
+            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.")
 
-    if rm_origin_dir:
-        shutil.rmtree(doujinshi_dir, ignore_errors=True)
 
-    logger.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir))
+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
+    """
+    encoded = s.encode(encoding)[:length]
+    return encoded.decode(encoding, 'ignore')
 
 
 def format_filename(s):
@@ -234,10 +251,16 @@ def format_filename(s):
     if filename include forbidden characters (\'/:,;*?"<>|) ,it replace space character(' '). 
     """
     # maybe you can use `--format` to select a suitable filename
-    ban_chars = '\\\'/:,;*?"<>|'
-    filename = s.translate(str.maketrans(ban_chars, ' '*len(ban_chars)))
+    ban_chars = '\\\'/:,;*?"<>|\t'
+    filename = s.translate(str.maketrans(ban_chars, ' ' * len(ban_chars))).strip()
+    filename = ' '.join(filename.split())
+    print(repr(filename))
+
+    while filename.endswith('.'):
+        filename = filename[:-1]
+
     if len(filename) > 100:
-        filename = filename[:100] + '...]'
+        filename = filename[:100] + u'…'
 
     # Remove [] from filename
     filename = filename.replace('[]', '').strip()
@@ -260,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')
@@ -269,6 +292,34 @@ def paging(page_string):
     return page_list
 
 
+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)
+    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']
+    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
     cur = None