fixing issue 16 and adding functionalities

This commit is contained in:
Mentat Erasmus 2018-05-09 15:42:12 -03:00
parent 967e0b4ff5
commit 147eec57cf
4 changed files with 44 additions and 6 deletions

View File

@ -56,6 +56,12 @@ def cmd_parser():
parser.add_option('--login', '-l', type='str', dest='login', action='store',
help='username:password pair of nhentai account')
parser.add_option('--nohtml', dest='is_nohtml', action='store_true',
help='Don\'t generate HTML')
parser.add_option('--cbz', dest='is_cbz', action='store_true',
help='Generate ComicBook CBZ File')
try:
sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
except (NameError, TypeError):

View File

@ -10,7 +10,7 @@ from nhentai.doujinshi import Doujinshi
from nhentai.downloader import Downloader
from nhentai.logger import logger
from nhentai.constant import BASE_URL
from nhentai.utils import generate_html
from nhentai.utils import generate_html, generate_cbz
def main():
@ -47,10 +47,13 @@ def main():
for doujinshi in doujinshi_list:
doujinshi.downloader = downloader
doujinshi.download()
if not options.is_nohtml and not options.is_cbz:
generate_html(options.output_dir, doujinshi)
elif options.is_cbz:
generate_cbz(options.output_dir, doujinshi)
if not platform.system() == 'Windows':
logger.log(15, '🍺 All done.')
logger.log(15, '? All done.')
else:
logger.log(15, 'All done.')

View File

@ -27,7 +27,7 @@ def login_parser(username, password):
s.get(constant.LOGIN_URL)
content = s.get(constant.LOGIN_URL).content
html = BeautifulSoup(content, 'html.parser')
html = BeautifulSoup(content, 'html.parser').encode("ascii")
csrf_token_elem = html.find('input', attrs={'name': 'csrfmiddlewaretoken'})
if not csrf_token_elem:
@ -44,7 +44,7 @@ def login_parser(username, password):
logger.error('Login failed, please check your username and password')
exit(1)
html = BeautifulSoup(s.get(constant.FAV_URL).content, 'html.parser')
html = BeautifulSoup(s.get(constant.FAV_URL).content, 'html.parser').encode("ascii")
count = html.find('span', attrs={'class': 'count'})
if not count:
logger.error('Cannot get count of your favorites, maybe login failed.')
@ -95,7 +95,7 @@ def doujinshi_parser(id_):
logger.critical(str(e))
exit(1)
doujinshi['name'] = response['title']['english']
doujinshi['name'] = str(response['title']['english'].encode('utf-8'))[2:]
doujinshi['subtitle'] = response['title']['japanese']
doujinshi['img_id'] = response['media_id']
doujinshi['ext'] = ''.join(map(lambda s: s['t'], response['images']['pages']))

View File

@ -3,6 +3,8 @@ from __future__ import unicode_literals, print_function
import os
import string
import zipfile
import shutil
from nhentai.logger import logger
@ -35,7 +37,7 @@ def generate_html(output_dir='.', doujinshi_obj=None):
if doujinshi_obj is not None:
doujinshi_dir = os.path.join(output_dir, format_filename('%s-%s' % (doujinshi_obj.id,
doujinshi_obj.name[:200])))
str(doujinshi_obj.name[:200]))))
else:
doujinshi_dir = '.'
@ -70,6 +72,33 @@ def generate_html(output_dir='.', doujinshi_obj=None):
logger.log(15, 'HTML Viewer has been write to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html')))
def generate_cbz(output_dir='.', doujinshi_obj=None):
if doujinshi_obj is not None:
doujinshi_dir = os.path.join(output_dir, format_filename('%s-%s' % (doujinshi_obj.id,
str(doujinshi_obj.name[:200]))))
cbz_filename = os.path.join(output_dir, format_filename('%s-%s.cbz' % (doujinshi_obj.id,
str(doujinshi_obj.name[:200]))))
else:
cbz_filename = './doujinshi.cbz'
doujinshi_dir = '.'
file_list = os.listdir(doujinshi_dir)
file_list.sort()
with zipfile.ZipFile(cbz_filename, 'w') as cbz_pf:
for image in file_list:
image_path = os.path.join(doujinshi_dir, image)
cbz_pf.write(image_path, image)
shutil.rmtree(doujinshi_dir, ignore_errors=True)
logger.log(15, 'Comic Book CBZ file has been write to \'{0}\''.format(doujinshi_dir))
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