]> git.lizzy.rs Git - nhentai.git/blob - nhentai/utils.py
fix #191
[nhentai.git] / nhentai / utils.py
1 # coding: utf-8
2 from __future__ import unicode_literals, print_function
3
4 import sys
5 import re
6 import os
7 import zipfile
8 import shutil
9 import requests
10 import sqlite3
11
12 from nhentai import constant
13 from nhentai.logger import logger
14 from nhentai.serializer import serialize_json, serialize_comicxml, set_js_database
15
16
17 def request(method, url, **kwargs):
18     session = requests.Session()
19     session.headers.update({
20         'Referer': constant.LOGIN_URL,
21         'User-Agent': 'nhentai command line client (https://github.com/RicterZ/nhentai)',
22         'Cookie': constant.CONFIG['cookie']
23     })
24     return getattr(session, method)(url, proxies=constant.CONFIG['proxy'], verify=False, **kwargs)
25
26
27 def check_cookie():
28     response = request('get', constant.BASE_URL).text
29     username = re.findall('"/users/\d+/(.*?)"', response)
30     if not username:
31         logger.error('Cannot get your username, please check your cookie or use `nhentai --cookie` to set your cookie')
32     else:
33         logger.info('Login successfully! Your username: {}'.format(username[0]))
34
35
36 class _Singleton(type):
37     """ A metaclass that creates a Singleton base class when called. """
38     _instances = {}
39
40     def __call__(cls, *args, **kwargs):
41         if cls not in cls._instances:
42             cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
43         return cls._instances[cls]
44
45
46 class Singleton(_Singleton(str('SingletonMeta'), (object,), {})):
47     pass
48
49
50 def urlparse(url):
51     try:
52         from urlparse import urlparse
53     except ImportError:
54         from urllib.parse import urlparse
55
56     return urlparse(url)
57
58
59 def readfile(path):
60     loc = os.path.dirname(__file__)
61
62     with open(os.path.join(loc, path), 'r') as file:
63         return file.read()
64
65
66 def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
67     image_html = ''
68
69     if doujinshi_obj is not None:
70         doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
71     else:
72         doujinshi_dir = '.'
73
74     file_list = os.listdir(doujinshi_dir)
75     file_list.sort()
76
77     for image in file_list:
78         if not os.path.splitext(image)[1] in ('.jpg', '.png'):
79             continue
80
81         image_html += '<img src="{0}" class="image-item"/>\n'\
82             .format(image)
83     html = readfile('viewer/{}/index.html'.format(template))
84     css = readfile('viewer/{}/styles.css'.format(template))
85     js = readfile('viewer/{}/scripts.js'.format(template))
86
87     if doujinshi_obj is not None:
88         serialize_json(doujinshi_obj, doujinshi_dir)
89         name = doujinshi_obj.name
90         if sys.version_info < (3, 0):
91             name = doujinshi_obj.name.encode('utf-8')
92     else:
93         name = {'title': 'nHentai HTML Viewer'}
94
95     data = html.format(TITLE=name, IMAGES=image_html, SCRIPTS=js, STYLES=css)
96     try:
97         if sys.version_info < (3, 0):
98             with open(os.path.join(doujinshi_dir, 'index.html'), 'w') as f:
99                 f.write(data)
100         else:
101             with open(os.path.join(doujinshi_dir, 'index.html'), 'wb') as f:
102                 f.write(data.encode('utf-8'))
103
104         logger.log(15, 'HTML Viewer has been written to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html')))
105     except Exception as e:
106         logger.warning('Writing HTML Viewer failed ({})'.format(str(e)))
107
108
109 def generate_main_html(output_dir='./'):
110     """
111     Generate a main html to show all the contain doujinshi.
112     With a link to their `index.html`.
113     Default output folder will be the CLI path.
114     """
115
116     image_html = ''
117
118     main = readfile('viewer/main.html')
119     css = readfile('viewer/main.css')
120     js = readfile('viewer/main.js')
121
122     element = '\n\
123             <div class="gallery-favorite">\n\
124                 <div class="gallery">\n\
125                     <a href="./{FOLDER}/index.html" class="cover" style="padding:0 0 141.6% 0"><img\n\
126                             src="./{FOLDER}/{IMAGE}" />\n\
127                         <div class="caption">{TITLE}</div>\n\
128                     </a>\n\
129                 </div>\n\
130             </div>\n'
131
132     os.chdir(output_dir)
133     doujinshi_dirs = next(os.walk('.'))[1]
134
135     for folder in doujinshi_dirs:
136         files = os.listdir(folder)
137         files.sort()
138
139         if 'index.html' in files:
140             logger.info('Add doujinshi \'{}\''.format(folder))
141         else:
142             continue
143
144         image = files[0]  # 001.jpg or 001.png
145         if folder is not None:
146             title = folder.replace('_', ' ')
147         else:
148             title = 'nHentai HTML Viewer'
149
150         image_html += element.format(FOLDER=folder, IMAGE=image, TITLE=title)
151     if image_html == '':
152         logger.warning('No index.html found, --gen-main paused.')
153         return
154     try:
155         data = main.format(STYLES=css, SCRIPTS=js, PICTURE=image_html)
156         if sys.version_info < (3, 0):
157             with open('./main.html', 'w') as f:
158                 f.write(data)
159         else:
160             with open('./main.html', 'wb') as f:
161                 f.write(data.encode('utf-8'))
162         shutil.copy(os.path.dirname(__file__)+'/viewer/logo.png', './')
163         set_js_database()
164         logger.log(
165             15, 'Main Viewer has been written to \'{0}main.html\''.format(output_dir))
166     except Exception as e:
167         logger.warning('Writing Main Viewer failed ({})'.format(str(e)))
168
169
170 def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=False):
171     if doujinshi_obj is not None:
172         doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
173         if write_comic_info:
174             serialize_comicxml(doujinshi_obj, doujinshi_dir)
175         cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), '{}.cbz'.format(doujinshi_obj.filename))
176     else:
177         cbz_filename = './doujinshi.cbz'
178         doujinshi_dir = '.'
179
180     file_list = os.listdir(doujinshi_dir)
181     file_list.sort()
182
183     logger.info('Writing CBZ file to path: {}'.format(cbz_filename))
184     with zipfile.ZipFile(cbz_filename, 'w') as cbz_pf:
185         for image in file_list:
186             image_path = os.path.join(doujinshi_dir, image)
187             cbz_pf.write(image_path, image)
188
189     if rm_origin_dir:
190         shutil.rmtree(doujinshi_dir, ignore_errors=True)
191
192     logger.log(15, 'Comic Book CBZ file has been written to \'{0}\''.format(doujinshi_dir))
193
194
195 def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
196     try:
197         import img2pdf
198     except ImportError:
199         logger.error("Please install img2pdf package by using pip.")
200
201     """Write images to a PDF file using img2pdf."""
202     if doujinshi_obj is not None:
203         doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
204         pdf_filename = os.path.join(
205             os.path.join(doujinshi_dir, '..'),
206             '{}.pdf'.format(doujinshi_obj.filename)
207         )
208     else:
209         pdf_filename = './doujinshi.pdf'
210         doujinshi_dir = '.'
211
212     file_list = os.listdir(doujinshi_dir)
213     file_list.sort()
214
215     logger.info('Writing PDF file to path: {}'.format(pdf_filename))
216     with open(pdf_filename, 'wb') as pdf_f:
217         full_path_list = (
218             [os.path.join(doujinshi_dir, image) for image in file_list]
219         )
220         pdf_f.write(img2pdf.convert(full_path_list))
221
222     if rm_origin_dir:
223         shutil.rmtree(doujinshi_dir, ignore_errors=True)
224
225     logger.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir))
226
227
228 def unicode_truncate(s, length, encoding='utf-8'):
229     """https://stackoverflow.com/questions/1809531/truncating-unicode-so-it-fits-a-maximum-size-when-encoded-for-wire-transfer
230     """
231     encoded = s.encode(encoding)[:length]
232     return encoded.decode(encoding, 'ignore')
233
234
235 def format_filename(s):
236     """
237     It used to be a whitelist approach allowed only alphabet and a part of symbols.
238     but most doujinshi's names include Japanese 2-byte characters and these was rejected.
239     so it is using blacklist approach now.
240     if filename include forbidden characters (\'/:,;*?"<>|) ,it replace space character(' '). 
241     """
242     # maybe you can use `--format` to select a suitable filename
243     ban_chars = '\\\'/:,;*?"<>|'
244     filename = s.translate(str.maketrans(ban_chars, ' '*len(ban_chars))).strip()
245     while filename.endswith('.'):
246         filename = filename[:-1]
247
248     if len(filename) > 100:
249         filename = filename[:100] + u'…'
250
251     # Remove [] from filename
252     filename = filename.replace('[]', '').strip()
253     return filename
254
255
256 def signal_handler(signal, frame):
257     logger.error('Ctrl-C signal received. Stopping...')
258     exit(1)
259
260
261 def paging(page_string):
262     # 1,3-5,14 -> [1, 3, 4, 5, 14]
263     if not page_string:
264         return []
265
266     page_list = []
267     for i in page_string.split(','):
268         if '-' in i:
269             start, end = i.split('-')
270             if not (start.isdigit() and end.isdigit()):
271                 raise Exception('Invalid page number')
272             page_list.extend(list(range(int(start), int(end)+1)))
273         else:
274             if not i.isdigit():
275                 raise Exception('Invalid page number')
276             page_list.append(int(i))
277
278     return page_list
279
280
281 class DB(object):
282     conn = None
283     cur = None
284
285     def __enter__(self):
286         self.conn = sqlite3.connect(constant.NHENTAI_HISTORY)
287         self.cur = self.conn.cursor()
288         self.cur.execute('CREATE TABLE IF NOT EXISTS download_history (id text)')
289         self.conn.commit()
290         return self
291
292     def __exit__(self, exc_type, exc_val, exc_tb):
293         self.conn.close()
294
295     def clean_all(self):
296         self.cur.execute('DELETE FROM download_history WHERE 1')
297         self.conn.commit()
298
299     def add_one(self, data):
300         self.cur.execute('INSERT INTO download_history VALUES (?)', [data])
301         self.conn.commit()
302
303     def get_all(self):
304         data = self.cur.execute('SELECT id FROM download_history')
305         return [i[0] for i in data]