]> git.lizzy.rs Git - nhentai.git/blobdiff - nhentai/utils.py
FIX: Use of img2lib even if it is not installed
[nhentai.git] / nhentai / utils.py
index de40d944c712ac49f1fbd1f273b0697d970cd411..c28a3ee3c6da0dd00ecce58f1a759bee1b9f6f33 100644 (file)
@@ -1,5 +1,4 @@
 # coding: utf-8
-from __future__ import unicode_literals, print_function
 
 import sys
 import re
@@ -195,35 +194,35 @@ 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 = '.'
-
-    file_list = os.listdir(doujinshi_dir)
-    file_list.sort()
+        
+        """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 = '.'
 
-    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))
+        file_list = os.listdir(doujinshi_dir)
+        file_list.sort()
 
-    if rm_origin_dir:
-        shutil.rmtree(doujinshi_dir, ignore_errors=True)
+        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.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir))
+        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.")
 
 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
@@ -240,8 +239,11 @@ 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 = '\\\'/:,;*?"<>|'
+    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]