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