# coding: utf-8
-from __future__ import unicode_literals, print_function
import sys
import re
import os
-import string
import zipfile
import shutil
import requests
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:
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)
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)
- 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.")
- 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):
- """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.
+ """
+ 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
+ ban_chars = '\\\'/:,;*?"<>|\t'
+ filename = s.translate(str.maketrans(ban_chars, ' '*len(ban_chars))).strip()
+ filename = ' '.join(filename.split())
+ print(repr(filename))
-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.
+ while filename.endswith('.'):
+ filename = filename[:-1]
-"""
- # 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)
if len(filename) > 100:
- filename = filename[:100] + '...]'
+ filename = filename[:100] + u'…'
# Remove [] from filename
filename = filename.replace('[]', '').strip()