]> git.lizzy.rs Git - nhentai.git/blobdiff - nhentai/utils.py
fix #193
[nhentai.git] / nhentai / utils.py
index d4b818360c4766db8245545894b5f2d4f6130ca3..099f6a12042912900743a14e20bd1dc3e077766c 100644 (file)
@@ -1,15 +1,12 @@
 # coding: utf-8
-from __future__ import unicode_literals, print_function
 
 import sys
 import re
 import os
-import string
 import zipfile
 import shutil
 import requests
 import sqlite3
-import img2pdf
 
 from nhentai import constant
 from nhentai.logger import logger
@@ -21,9 +18,9 @@ def request(method, url, **kwargs):
     session.headers.update({
         'Referer': constant.LOGIN_URL,
         'User-Agent': 'nhentai command line client (https://github.com/RicterZ/nhentai)',
-        'Cookie': constant.COOKIE
+        'Cookie': constant.CONFIG['cookie']
     })
-    return getattr(session, method)(url, proxies=constant.PROXY, verify=False, **kwargs)
+    return getattr(session, method)(url, proxies=constant.CONFIG['proxy'], verify=False, **kwargs)
 
 
 def check_cookie():
@@ -65,7 +62,7 @@ def readfile(path):
         return file.read()
 
 
-def generate_html(output_dir='.', doujinshi_obj=None):
+def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
     image_html = ''
 
     if doujinshi_obj is not None:
@@ -82,9 +79,9 @@ def generate_html(output_dir='.', doujinshi_obj=None):
 
         image_html += '<img src="{0}" class="image-item"/>\n'\
             .format(image)
-    html = readfile('viewer/index.html')
-    css = readfile('viewer/styles.css')
-    js = readfile('viewer/scripts.js')
+    html = readfile('viewer/{}/index.html'.format(template))
+    css = readfile('viewer/{}/styles.css'.format(template))
+    js = readfile('viewer/{}/scripts.js'.format(template))
 
     if doujinshi_obj is not None:
         serialize_json(doujinshi_obj, doujinshi_dir)
@@ -195,6 +192,11 @@ 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)
@@ -222,22 +224,31 @@ def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
     logger.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir))
 
 
-def format_filename(s):
-    """Take a string and return a valid filename constructed from the string.
-Uses a whitelist approach: any characters not present in valid_chars are
-removed. Also spaces are replaced with underscores.
+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')
 
-Note: this method may produce invalid filenames such as ``, `.` or `..`
-When I use this method I prepend a date string like '2009_01_15_19_46_32_'
-and append a file extension like '.txt', so I avoid the potential of using
-an invalid filename.
 
-"""
+def format_filename(s):
+    """
+    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.
+    so it is using blacklist approach now.
+    if filename include forbidden characters (\'/:,;*?"<>|) ,it replace space character(' '). 
+    """
     # maybe you can use `--format` to select a suitable filename
-    valid_chars = "-_.()[] %s%s" % (string.ascii_letters, string.digits)
-    filename = ''.join(c for c in s if c in valid_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()
@@ -249,6 +260,26 @@ def signal_handler(signal, frame):
     exit(1)
 
 
+def paging(page_string):
+    # 1,3-5,14 -> [1, 3, 4, 5, 14]
+    if not page_string:
+        return []
+
+    page_list = []
+    for i in page_string.split(','):
+        if '-' in i:
+            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)))
+        else:
+            if not i.isdigit():
+                raise Exception('Invalid page number')
+            page_list.append(int(i))
+
+    return page_list
+
+
 class DB(object):
     conn = None
     cur = None