X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=nhentai%2Futils.py;h=835463785a18240fe875911b14f7a4687a4a48e2;hb=ea356a1ca24b99e3229a6c547a77a79221c201a9;hp=6a0d00bb848f620c8ce8ed686ba1b90c08274964;hpb=bacaa096e07dbea314924d5fce35ea6cdaf9d708;p=nhentai.git diff --git a/nhentai/utils.py b/nhentai/utils.py index 6a0d00b..8354637 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -1,33 +1,41 @@ # 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 from nhentai import constant from nhentai.logger import logger +from nhentai.serializer import serialize_json, serialize_comic_xml, set_js_database def request(method, url, **kwargs): session = requests.Session() session.headers.update({ 'Referer': constant.LOGIN_URL, - 'User-Agent': 'nhentai command line client (https://github.com/RicterZ/nhentai)', - 'Cookie': constant.COOKIE + 'User-Agent': constant.CONFIG['useragent'], + 'Cookie': constant.CONFIG['cookie'] }) - return getattr(session, method)(url, proxies=constant.PROXY, verify=False, **kwargs) + + if not kwargs.get('proxies', None): + kwargs['proxies'] = constant.CONFIG['proxy'] + + return getattr(session, method)(url, verify=False, **kwargs) def check_cookie(): - response = request('get', constant.BASE_URL).text - username = re.findall('"/users/\d+/(.*?)"', response) + response = request('get', constant.BASE_URL) + if response.status_code == 503 and 'cf-browser-verification' in response.text: + logger.error('Blocked by Cloudflare captcha, please set your cookie and useragent') + exit(-1) + + username = re.findall('"/users/\d+/(.*?)"', response.text) if not username: - logger.error('Cannot get your username, please check your cookie or use `nhentai --cookie` to set your cookie') + logger.warning('Cannot get your username, please check your cookie or use `nhentai --cookie` to set your cookie') else: logger.info('Login successfully! Your username: {}'.format(username[0])) @@ -62,7 +70,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: @@ -70,6 +78,13 @@ def generate_html(output_dir='.', doujinshi_obj=None): else: doujinshi_dir = '.' + if not os.path.exists(doujinshi_dir): + logger.warning('Path \'{0}\' does not exist, creating.'.format(doujinshi_dir)) + try: + os.makedirs(doujinshi_dir) + except EnvironmentError as e: + logger.critical('{0}'.format(str(e))) + file_list = os.listdir(doujinshi_dir) file_list.sort() @@ -77,21 +92,21 @@ def generate_html(output_dir='.', doujinshi_obj=None): if not os.path.splitext(image)[1] in ('.jpg', '.png'): continue - image_html += '\n'\ + image_html += '\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: - title = doujinshi_obj.name + serialize_json(doujinshi_obj, doujinshi_dir) + name = doujinshi_obj.name if sys.version_info < (3, 0): - title = title.encode('utf-8') + name = doujinshi_obj.name.encode('utf-8') else: - title = 'nHentai HTML Viewer' + name = {'title': 'nHentai HTML Viewer'} - data = html.format(TITLE=title, IMAGES=image_html, SCRIPTS=js, STYLES=css) + data = html.format(TITLE=name, IMAGES=image_html, SCRIPTS=js, STYLES=css) try: if sys.version_info < (3, 0): with open(os.path.join(doujinshi_dir, 'index.html'), 'w') as f: @@ -100,9 +115,9 @@ def generate_html(output_dir='.', doujinshi_obj=None): with open(os.path.join(doujinshi_dir, 'index.html'), 'wb') as f: f.write(data.encode('utf-8')) - logger.log(15, 'HTML Viewer has been write to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html'))) + logger.log(15, 'HTML Viewer has been written to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html'))) except Exception as e: - logger.warning('Writen HTML Viewer failed ({})'.format(str(e))) + logger.warning('Writing HTML Viewer failed ({})'.format(str(e))) def generate_main_html(output_dir='./'): @@ -112,10 +127,12 @@ def generate_main_html(output_dir='./'): Default output folder will be the CLI path. """ - count = 0 image_html = '' + main = readfile('viewer/main.html') css = readfile('viewer/main.css') + js = readfile('viewer/main.js') + element = '\n\