mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-07-01 07:59:29 +02:00
Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
45fb35b950 | |||
2271b83d93 | |||
0ee000edeb | |||
a47359f411 | |||
48c6fadc98 | |||
dbc834ea2e | |||
71177ff94e | |||
d1ed9b6980 | |||
42a09e2c1e | |||
e306d50b7e | |||
043f391d04 | |||
9549c5f5a2 | |||
5592b30be4 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,4 +4,4 @@ build
|
||||
dist/
|
||||
*.egg-info
|
||||
.python-version
|
||||
|
||||
.DS_Store
|
||||
|
@ -1,2 +1,3 @@
|
||||
include README.md
|
||||
include requirements.txt
|
||||
include nhentai/doujinshi.html
|
||||
|
@ -47,6 +47,7 @@ nhentai
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
### License
|
||||
MIT
|
||||
|
0
images/image.jpg
Executable file → Normal file
0
images/image.jpg
Executable file → Normal file
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
BIN
images/viewer.png
Normal file
BIN
images/viewer.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 311 KiB |
@ -1,3 +1,3 @@
|
||||
__version__ = '0.2.2'
|
||||
__version__ = '0.2.9'
|
||||
__author__ = 'Ricter'
|
||||
__email__ = 'ricterzheng@gmail.com'
|
||||
|
@ -8,7 +8,7 @@ except ImportError:
|
||||
pass
|
||||
|
||||
import nhentai.constant as constant
|
||||
from nhentai.utils import urlparse
|
||||
from nhentai.utils import urlparse, generate_html
|
||||
from nhentai.logger import logger
|
||||
|
||||
try:
|
||||
@ -30,9 +30,12 @@ def banner():
|
||||
|
||||
|
||||
def cmd_parser():
|
||||
parser = OptionParser()
|
||||
parser.add_option('--download', dest='is_download', action='store_true', help='download doujinshi or not')
|
||||
parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information.')
|
||||
parser = OptionParser('\n nhentai --search [keyword] --download'
|
||||
'\n NHENTAI=http://h.loli.club nhentai --id [ID ...]'
|
||||
'\n\nEnvironment Variable:\n'
|
||||
' NHENTAI nhentai mirror url')
|
||||
parser.add_option('--download', dest='is_download', action='store_true', help='download doujinshi (for search result)')
|
||||
parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
|
||||
parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
|
||||
parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
|
||||
parser.add_option('--page', type='int', dest='page', action='store', default=1,
|
||||
@ -46,6 +49,7 @@ def cmd_parser():
|
||||
help='timeout of download doujinshi')
|
||||
parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
|
||||
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:
|
||||
sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
|
||||
@ -56,6 +60,10 @@ def cmd_parser():
|
||||
|
||||
args, _ = parser.parse_args(sys.argv[1:])
|
||||
|
||||
if args.html_viewer:
|
||||
generate_html()
|
||||
exit(0)
|
||||
|
||||
if args.tags:
|
||||
logger.warning('`--tags` is under construction')
|
||||
exit(0)
|
||||
@ -76,8 +84,8 @@ def cmd_parser():
|
||||
if args.threads <= 0:
|
||||
args.threads = 1
|
||||
|
||||
elif args.threads > 10:
|
||||
logger.critical('Maximum number of used threads is 10')
|
||||
elif args.threads > 15:
|
||||
logger.critical('Maximum number of used threads is 15')
|
||||
exit(0)
|
||||
|
||||
if args.proxy:
|
||||
|
@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python2.7
|
||||
# coding: utf-8
|
||||
from __future__ import unicode_literals, print_function
|
||||
import os
|
||||
import signal
|
||||
import platform
|
||||
|
||||
@ -10,6 +11,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
|
||||
|
||||
|
||||
def main():
|
||||
@ -42,6 +44,7 @@ def main():
|
||||
for doujinshi in doujinshi_list:
|
||||
doujinshi.downloader = downloader
|
||||
doujinshi.download()
|
||||
generate_html(doujinshi, output_dir)
|
||||
|
||||
if not platform.system() == 'Windows':
|
||||
logger.log(15, '🍺 All done.')
|
||||
|
126
nhentai/doujinshi.html
Normal file
126
nhentai/doujinshi.html
Normal file
@ -0,0 +1,126 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{TITLE}</title>
|
||||
<style>
|
||||
html, body {{
|
||||
background-color: #e8e6e6;
|
||||
height: 100%;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
}}
|
||||
|
||||
.container img {{
|
||||
display: block;
|
||||
width: 100%;
|
||||
margin: 30px 0;
|
||||
padding: 10px;
|
||||
cursor: pointer;
|
||||
}}
|
||||
|
||||
.container {{
|
||||
height: 100%;
|
||||
overflow: scroll;
|
||||
background: #e8e6e6;
|
||||
width: 200px;
|
||||
padding: 30px;
|
||||
float: left;
|
||||
}}
|
||||
|
||||
.image {{
|
||||
margin-left: 260px;
|
||||
height: 100%;
|
||||
background: #222;
|
||||
text-align: center;
|
||||
}}
|
||||
|
||||
.image img {{
|
||||
height: 100%;
|
||||
}}
|
||||
|
||||
.i a {{
|
||||
display: block;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
width: 50%;
|
||||
height: 100%;
|
||||
}}
|
||||
|
||||
.i {{
|
||||
position: relative;
|
||||
height: 100%;
|
||||
}}
|
||||
|
||||
.current {{
|
||||
background: #BBB;
|
||||
border-radius: 10px;
|
||||
}}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
function cursorfocus(elem) {{
|
||||
var container = document.getElementsByClassName('container')[0];
|
||||
container.scrollTop = elem.offsetTop - 500;
|
||||
}}
|
||||
|
||||
function getImage(type) {{
|
||||
var current = document.getElementsByClassName("current")[0];
|
||||
current.className = "image-item";
|
||||
var img_src = type == 1 ? current.getAttribute('attr-next') : current.getAttribute('attr-prev');
|
||||
if (img_src === "") {{
|
||||
img_src = current.src;
|
||||
}}
|
||||
|
||||
var img_list = document.getElementsByClassName("image-item");
|
||||
for (i=0; i<img_list.length; i++) {{
|
||||
if (img_list[i].src.endsWith(img_src)) {{
|
||||
img_list[i].className = "image-item current";
|
||||
cursorfocus(img_list[i]);
|
||||
break;
|
||||
}}
|
||||
}}
|
||||
var display = document.getElementById("dest");
|
||||
display.src = img_src;
|
||||
display.focus();
|
||||
}}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
{IMAGES}</div>
|
||||
<div class="image">
|
||||
<div class="i">
|
||||
<img src="" id="dest">
|
||||
<a href="javascript:getImage(-1)" style="left: 0;"></a>
|
||||
<a href="javascript:getImage(1)" style="left: 50%;"></a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
var img_list = document.getElementsByClassName("image-item");
|
||||
|
||||
var display = document.getElementById("dest");
|
||||
display.src = img_list[0].src;
|
||||
|
||||
for (var i = 0; i < img_list.length; i++) {{
|
||||
img_list[i].addEventListener('click', function() {{
|
||||
var current = document.getElementsByClassName("current")[0];
|
||||
current.className = "image-item";
|
||||
this.className = "image-item current";
|
||||
var display = document.getElementById("dest");
|
||||
display.src = this.src;
|
||||
display.focus();
|
||||
}}, false);
|
||||
}}
|
||||
|
||||
document.onkeypress = function(e) {{
|
||||
if (e.keyCode == 32) {{
|
||||
getImage(1);
|
||||
}}
|
||||
}}
|
||||
</script>
|
||||
</html>
|
@ -14,45 +14,65 @@ from nhentai.parser import request
|
||||
from nhentai.utils import Singleton
|
||||
|
||||
|
||||
requests.packages.urllib3.disable_warnings()
|
||||
|
||||
|
||||
class NhentaiImageNotExistException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Downloader(Singleton):
|
||||
|
||||
def __init__(self, path='', thread=1, timeout=30):
|
||||
if not isinstance(thread, (int, )) or thread < 1 or thread > 10:
|
||||
if not isinstance(thread, (int, )) or thread < 1 or thread > 15:
|
||||
raise ValueError('Invalid threads count')
|
||||
self.path = str(path)
|
||||
self.thread_count = thread
|
||||
self.threads = []
|
||||
self.timeout = timeout
|
||||
|
||||
def _download(self, url, folder='', filename='', retried=False):
|
||||
def _download(self, url, folder='', filename='', retried=0):
|
||||
logger.info('Start downloading: {0} ...'.format(url))
|
||||
filename = filename if filename else os.path.basename(urlparse(url).path)
|
||||
base_filename, extension = os.path.splitext(filename)
|
||||
try:
|
||||
with open(os.path.join(folder, base_filename.zfill(3) + extension), "wb") as f:
|
||||
response = request('get', url, stream=True, timeout=self.timeout)
|
||||
if response.status_code != 200:
|
||||
raise NhentaiImageNotExistException
|
||||
length = response.headers.get('content-length')
|
||||
if length is None:
|
||||
f.write(response.content)
|
||||
else:
|
||||
for chunk in response.iter_content(2048):
|
||||
f.write(chunk)
|
||||
except requests.HTTPError as e:
|
||||
if not retried:
|
||||
logger.error('Error: {0}, retrying'.format(str(e)))
|
||||
return self._download(url=url, folder=folder, filename=filename, retried=True)
|
||||
|
||||
except (requests.HTTPError, requests.Timeout) as e:
|
||||
if retried < 3:
|
||||
logger.warning('Warning: {0}, retrying({1}) ...'.format(str(e), retried))
|
||||
return 0, self._download(url=url, folder=folder, filename=filename, retried=retried+1)
|
||||
else:
|
||||
return None
|
||||
return 0, None
|
||||
|
||||
except NhentaiImageNotExistException as e:
|
||||
os.remove(os.path.join(folder, base_filename.zfill(3) + extension))
|
||||
return -1, url
|
||||
|
||||
except Exception as e:
|
||||
logger.critical(str(e))
|
||||
return None
|
||||
return url
|
||||
return 0, None
|
||||
|
||||
return 1, url
|
||||
|
||||
def _download_callback(self, request, result):
|
||||
if not result:
|
||||
logger.critical('Too many errors occurred, quit.')
|
||||
exit(1)
|
||||
logger.log(15, '{0} download successfully'.format(result))
|
||||
result, data = result
|
||||
if result == 0:
|
||||
logger.warning('fatal errors occurred, ignored')
|
||||
# exit(1)
|
||||
elif result == -1:
|
||||
logger.warning('url {} return status code 404'.format(data))
|
||||
else:
|
||||
logger.log(15, '{0} download successfully'.format(data))
|
||||
|
||||
def download(self, queue, folder=''):
|
||||
if not isinstance(folder, (text)):
|
||||
@ -66,7 +86,7 @@ class Downloader(Singleton):
|
||||
try:
|
||||
os.makedirs(folder)
|
||||
except EnvironmentError as e:
|
||||
logger.critical('Error: {0}'.format(str(e)))
|
||||
logger.critical('{0}'.format(str(e)))
|
||||
exit(1)
|
||||
else:
|
||||
logger.warn('Path \'{0}\' already exist.'.format(folder))
|
||||
|
@ -14,7 +14,7 @@ def request(method, url, **kwargs):
|
||||
if not hasattr(requests, method):
|
||||
raise AttributeError('\'requests\' object has no attribute \'{0}\''.format(method))
|
||||
|
||||
return requests.__dict__[method](url, proxies=constant.PROXY, **kwargs)
|
||||
return requests.__dict__[method](url, proxies=constant.PROXY, verify=False, **kwargs)
|
||||
|
||||
|
||||
def doujinshi_parser(id_):
|
||||
@ -43,7 +43,7 @@ def doujinshi_parser(id_):
|
||||
doujinshi['subtitle'] = subtitle.text if subtitle else ''
|
||||
|
||||
doujinshi_cover = html.find('div', attrs={'id': 'cover'})
|
||||
img_id = re.search('/galleries/([\d]+)/cover\.(jpg|png)$', doujinshi_cover.a.img['src'])
|
||||
img_id = re.search('/galleries/([\d]+)/cover\.(jpg|png)$', doujinshi_cover.a.img.attrs['data-src'])
|
||||
if not img_id:
|
||||
logger.critical('Tried yo get image id failed')
|
||||
exit(1)
|
||||
|
@ -1,6 +1,9 @@
|
||||
# coding: utf-8
|
||||
from __future__ import unicode_literals, print_function
|
||||
|
||||
import os
|
||||
from nhentai.logger import logger
|
||||
|
||||
|
||||
class _Singleton(type):
|
||||
""" A metaclass that creates a Singleton base class when called. """
|
||||
@ -23,3 +26,43 @@ def urlparse(url):
|
||||
from urllib.parse import urlparse
|
||||
|
||||
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')))
|
||||
|
Reference in New Issue
Block a user