mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-07-01 16:09:28 +02:00
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
043f391d04 | |||
9549c5f5a2 | |||
5592b30be4 |
@ -1,3 +1,3 @@
|
|||||||
__version__ = '0.2.2'
|
__version__ = '0.2.4'
|
||||||
__author__ = 'Ricter'
|
__author__ = 'Ricter'
|
||||||
__email__ = 'ricterzheng@gmail.com'
|
__email__ = 'ricterzheng@gmail.com'
|
||||||
|
@ -30,9 +30,12 @@ def banner():
|
|||||||
|
|
||||||
|
|
||||||
def cmd_parser():
|
def cmd_parser():
|
||||||
parser = OptionParser()
|
parser = OptionParser('\n nhentai --search [keyword] --download'
|
||||||
parser.add_option('--download', dest='is_download', action='store_true', help='download doujinshi or not')
|
'\n NHENTAI=http://h.loli.club nhentai --id [ID ...]'
|
||||||
parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information.')
|
'\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('--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('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
|
||||||
parser.add_option('--page', type='int', dest='page', action='store', default=1,
|
parser.add_option('--page', type='int', dest='page', action='store', default=1,
|
||||||
@ -76,8 +79,8 @@ def cmd_parser():
|
|||||||
if args.threads <= 0:
|
if args.threads <= 0:
|
||||||
args.threads = 1
|
args.threads = 1
|
||||||
|
|
||||||
elif args.threads > 10:
|
elif args.threads > 15:
|
||||||
logger.critical('Maximum number of used threads is 10')
|
logger.critical('Maximum number of used threads is 15')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
if args.proxy:
|
if args.proxy:
|
||||||
|
@ -14,45 +14,62 @@ from nhentai.parser import request
|
|||||||
from nhentai.utils import Singleton
|
from nhentai.utils import Singleton
|
||||||
|
|
||||||
|
|
||||||
|
class NhentaiImageNotExistException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Downloader(Singleton):
|
class Downloader(Singleton):
|
||||||
|
|
||||||
def __init__(self, path='', thread=1, timeout=30):
|
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')
|
raise ValueError('Invalid threads count')
|
||||||
self.path = str(path)
|
self.path = str(path)
|
||||||
self.thread_count = thread
|
self.thread_count = thread
|
||||||
self.threads = []
|
self.threads = []
|
||||||
self.timeout = timeout
|
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))
|
logger.info('Start downloading: {0} ...'.format(url))
|
||||||
filename = filename if filename else os.path.basename(urlparse(url).path)
|
filename = filename if filename else os.path.basename(urlparse(url).path)
|
||||||
base_filename, extension = os.path.splitext(filename)
|
base_filename, extension = os.path.splitext(filename)
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(folder, base_filename.zfill(3) + extension), "wb") as f:
|
with open(os.path.join(folder, base_filename.zfill(3) + extension), "wb") as f:
|
||||||
response = request('get', url, stream=True, timeout=self.timeout)
|
response = request('get', url, stream=True, timeout=self.timeout)
|
||||||
|
if response.status_code != 200:
|
||||||
|
raise NhentaiImageNotExistException
|
||||||
length = response.headers.get('content-length')
|
length = response.headers.get('content-length')
|
||||||
if length is None:
|
if length is None:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
else:
|
else:
|
||||||
for chunk in response.iter_content(2048):
|
for chunk in response.iter_content(2048):
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
|
|
||||||
except requests.HTTPError as e:
|
except requests.HTTPError as e:
|
||||||
if not retried:
|
if retried < 3:
|
||||||
logger.error('Error: {0}, retrying'.format(str(e)))
|
logger.warning('Warning: {0}, retrying({1}) ...'.format(str(e), retried))
|
||||||
return self._download(url=url, folder=folder, filename=filename, retried=True)
|
return 0, self._download(url=url, folder=folder, filename=filename, retried=retried+1)
|
||||||
else:
|
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:
|
except Exception as e:
|
||||||
logger.critical(str(e))
|
logger.critical(str(e))
|
||||||
return None
|
return 0, None
|
||||||
return url
|
|
||||||
|
return 1, url
|
||||||
|
|
||||||
def _download_callback(self, request, result):
|
def _download_callback(self, request, result):
|
||||||
if not result:
|
result, data = result
|
||||||
logger.critical('Too many errors occurred, quit.')
|
if result == 0:
|
||||||
|
logger.critical('fatal errors occurred, quit.')
|
||||||
exit(1)
|
exit(1)
|
||||||
logger.log(15, '{0} download successfully'.format(result))
|
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=''):
|
def download(self, queue, folder=''):
|
||||||
if not isinstance(folder, (text)):
|
if not isinstance(folder, (text)):
|
||||||
@ -66,7 +83,7 @@ class Downloader(Singleton):
|
|||||||
try:
|
try:
|
||||||
os.makedirs(folder)
|
os.makedirs(folder)
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
logger.critical('Error: {0}'.format(str(e)))
|
logger.critical('{0}'.format(str(e)))
|
||||||
exit(1)
|
exit(1)
|
||||||
else:
|
else:
|
||||||
logger.warn('Path \'{0}\' already exist.'.format(folder))
|
logger.warn('Path \'{0}\' already exist.'.format(folder))
|
||||||
|
@ -14,7 +14,7 @@ def request(method, url, **kwargs):
|
|||||||
if not hasattr(requests, method):
|
if not hasattr(requests, method):
|
||||||
raise AttributeError('\'requests\' object has no attribute \'{0}\''.format(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_):
|
def doujinshi_parser(id_):
|
||||||
|
Reference in New Issue
Block a user