X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=nhentai%2Futils.py;h=835463785a18240fe875911b14f7a4687a4a48e2;hb=ea356a1ca24b99e3229a6c547a77a79221c201a9;hp=c076cf89744f411e2c3ac9125ea888df7ffd6f5e;hpb=2cf4e6718ecf5012d613cbc226696d3d88f2cdb7;p=nhentai.git diff --git a/nhentai/utils.py b/nhentai/utils.py index c076cf8..8354637 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -10,24 +10,32 @@ import sqlite3 from nhentai import constant from nhentai.logger import logger -from nhentai.serializer import serialize_json, serialize_comicxml, set_js_database +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)', + 'User-Agent': constant.CONFIG['useragent'], 'Cookie': constant.CONFIG['cookie'] }) - return getattr(session, method)(url, proxies=constant.CONFIG['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])) @@ -84,7 +92,7 @@ def generate_html(output_dir='.', doujinshi_obj=None, template='default'): if not os.path.splitext(image)[1] in ('.jpg', '.png'): continue - image_html += '\n'\ + image_html += '\n' \ .format(image) html = readfile('viewer/{}/index.html'.format(template)) css = readfile('viewer/{}/styles.css'.format(template)) @@ -165,7 +173,7 @@ def generate_main_html(output_dir='./'): else: with open('./main.html', 'wb') as f: f.write(data.encode('utf-8')) - shutil.copy(os.path.dirname(__file__)+'/viewer/logo.png', './') + shutil.copy(os.path.dirname(__file__) + '/viewer/logo.png', './') set_js_database() logger.log( 15, 'Main Viewer has been written to \'{0}main.html\''.format(output_dir)) @@ -177,7 +185,7 @@ def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_ if doujinshi_obj is not None: doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename) if write_comic_info: - serialize_comicxml(doujinshi_obj, doujinshi_dir) + serialize_comic_xml(doujinshi_obj, doujinshi_dir) cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), '{}.cbz'.format(doujinshi_obj.filename)) else: cbz_filename = './doujinshi.cbz' @@ -231,6 +239,7 @@ def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False): except ImportError: logger.error("Please install img2pdf package by using pip.") + 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 """ @@ -243,13 +252,12 @@ 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(' '). + 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 = s.translate(str.maketrans(ban_chars, ' ' * len(ban_chars))).strip() filename = ' '.join(filename.split()) - print(repr(filename)) while filename.endswith('.'): filename = filename[:-1] @@ -278,7 +286,7 @@ def paging(page_string): 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))) + page_list.extend(list(range(int(start), int(end) + 1))) else: if not i.isdigit(): raise Exception('Invalid page number') @@ -287,6 +295,34 @@ def paging(page_string): return page_list +def generate_metadata_file(output_dir, table, doujinshi_obj=None): + logger.info('Writing Metadata Info') + + if doujinshi_obj is not None: + doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename) + else: + doujinshi_dir = '.' + + logger.info(doujinshi_dir) + + f = open(os.path.join(doujinshi_dir, 'info.txt'), 'w', encoding='utf-8') + + fields = ['TITLE', 'ORIGINAL TITLE', 'AUTHOR', 'ARTIST', 'CIRCLE', 'SCANLATOR', + 'TRANSLATOR', 'PUBLISHER', 'DESCRIPTION', 'STATUS', 'CHAPTERS', 'PAGES', + 'TAGS', 'TYPE', 'LANGUAGE', 'RELEASED', 'READING DIRECTION', 'CHARACTERS', + 'SERIES', 'PARODY', 'URL'] + special_fields = ['PARODY', 'TITLE', 'ORIGINAL TITLE', 'CHARACTERS', 'AUTHOR', + 'LANGUAGE', 'TAGS', 'URL', 'PAGES'] + + for i in range(len(fields)): + f.write('{}: '.format(fields[i])) + if fields[i] in special_fields: + f.write(str(table[special_fields.index(fields[i])][1])) + f.write('\n') + + f.close() + + class DB(object): conn = None cur = None