mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-04-20 11:01:17 +02:00
fix bug and add --html
This commit is contained in:
parent
2271b83d93
commit
45fb35b950
@ -1,3 +1,3 @@
|
|||||||
__version__ = '0.2.8'
|
__version__ = '0.2.9'
|
||||||
__author__ = 'Ricter'
|
__author__ = 'Ricter'
|
||||||
__email__ = 'ricterzheng@gmail.com'
|
__email__ = 'ricterzheng@gmail.com'
|
||||||
|
@ -8,7 +8,7 @@ except ImportError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
import nhentai.constant as constant
|
import nhentai.constant as constant
|
||||||
from nhentai.utils import urlparse
|
from nhentai.utils import urlparse, generate_html
|
||||||
from nhentai.logger import logger
|
from nhentai.logger import logger
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -49,6 +49,7 @@ def cmd_parser():
|
|||||||
help='timeout of download doujinshi')
|
help='timeout of download doujinshi')
|
||||||
parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
|
parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
|
||||||
help='use proxy, example: http://127.0.0.1:1080')
|
help='use proxy, example: http://127.0.0.1:1080')
|
||||||
|
parser.add_option('--html', dest='html_viewer', action='store_true', help='generate a html viewer at current directory')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
|
sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
|
||||||
@ -59,6 +60,10 @@ def cmd_parser():
|
|||||||
|
|
||||||
args, _ = parser.parse_args(sys.argv[1:])
|
args, _ = parser.parse_args(sys.argv[1:])
|
||||||
|
|
||||||
|
if args.html_viewer:
|
||||||
|
generate_html()
|
||||||
|
exit(0)
|
||||||
|
|
||||||
if args.tags:
|
if args.tags:
|
||||||
logger.warning('`--tags` is under construction')
|
logger.warning('`--tags` is under construction')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
@ -11,6 +11,7 @@ from nhentai.doujinshi import Doujinshi
|
|||||||
from nhentai.downloader import Downloader
|
from nhentai.downloader import Downloader
|
||||||
from nhentai.logger import logger
|
from nhentai.logger import logger
|
||||||
from nhentai.constant import BASE_URL
|
from nhentai.constant import BASE_URL
|
||||||
|
from nhentai.utils import generate_html
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -43,32 +44,7 @@ def main():
|
|||||||
for doujinshi in doujinshi_list:
|
for doujinshi in doujinshi_list:
|
||||||
doujinshi.downloader = downloader
|
doujinshi.downloader = downloader
|
||||||
doujinshi.download()
|
doujinshi.download()
|
||||||
|
generate_html(doujinshi, output_dir)
|
||||||
image_html = ''
|
|
||||||
previous = ''
|
|
||||||
|
|
||||||
doujinshi_dir = os.path.join(options.output_dir, str(doujinshi.id))
|
|
||||||
file_list = os.listdir(doujinshi_dir)
|
|
||||||
file_list.sort()
|
|
||||||
|
|
||||||
for index, image in enumerate(file_list):
|
|
||||||
try:
|
|
||||||
next_ = file_list[file_list.index(image) + 1]
|
|
||||||
except IndexError:
|
|
||||||
next_ = ''
|
|
||||||
|
|
||||||
image_html += '<img src="{0}" class="image-item {1}" attr-prev="{2}" attr-next="{3}">\n'\
|
|
||||||
.format(image, 'current' if index == 0 else '', previous, next_)
|
|
||||||
previous = image
|
|
||||||
|
|
||||||
with open(os.path.join(os.path.dirname(__file__), 'doujinshi.html'), 'r') as template:
|
|
||||||
html = template.read()
|
|
||||||
|
|
||||||
data = html.format(TITLE=doujinshi.name, IMAGES=image_html)
|
|
||||||
with open(os.path.join(doujinshi_dir, 'index.html'), 'w') as f:
|
|
||||||
f.write(data)
|
|
||||||
|
|
||||||
logger.log(15, 'HTML Viewer has been write to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html')))
|
|
||||||
|
|
||||||
if not platform.system() == 'Windows':
|
if not platform.system() == 'Windows':
|
||||||
logger.log(15, '🍺 All done.')
|
logger.log(15, '🍺 All done.')
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals, print_function
|
from __future__ import unicode_literals, print_function
|
||||||
|
|
||||||
|
import os
|
||||||
|
from nhentai.logger import logger
|
||||||
|
|
||||||
|
|
||||||
class _Singleton(type):
|
class _Singleton(type):
|
||||||
""" A metaclass that creates a Singleton base class when called. """
|
""" A metaclass that creates a Singleton base class when called. """
|
||||||
@ -23,3 +26,43 @@ def urlparse(url):
|
|||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
return urlparse(url)
|
return urlparse(url)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_html(output_dir='.', doujinshi_obj=None):
|
||||||
|
image_html = ''
|
||||||
|
previous = ''
|
||||||
|
|
||||||
|
if doujinshi_obj is not None:
|
||||||
|
doujinshi_dir = os.path.join(output_dir, str(doujinshi_obj.id))
|
||||||
|
else:
|
||||||
|
doujinshi_dir = '.'
|
||||||
|
|
||||||
|
file_list = os.listdir(doujinshi_dir)
|
||||||
|
file_list.sort()
|
||||||
|
|
||||||
|
for index, image in enumerate(file_list):
|
||||||
|
if not os.path.splitext(image)[1] in ('.jpg', '.png'):
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
next_ = file_list[file_list.index(image) + 1]
|
||||||
|
except IndexError:
|
||||||
|
next_ = ''
|
||||||
|
|
||||||
|
image_html += '<img src="{0}" class="image-item {1}" attr-prev="{2}" attr-next="{3}">\n'\
|
||||||
|
.format(image, 'current' if index == 0 else '', previous, next_)
|
||||||
|
previous = image
|
||||||
|
|
||||||
|
with open(os.path.join(os.path.dirname(__file__), 'doujinshi.html'), 'r') as template:
|
||||||
|
html = template.read()
|
||||||
|
|
||||||
|
if doujinshi_obj is not None:
|
||||||
|
title = doujinshi_obj.name
|
||||||
|
else:
|
||||||
|
title = 'nHentai HTML Viewer'
|
||||||
|
|
||||||
|
data = html.format(TITLE=title, IMAGES=image_html)
|
||||||
|
with open(os.path.join(doujinshi_dir, 'index.html'), 'w') as f:
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
|
logger.log(15, 'HTML Viewer has been write to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html')))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user