mirror of
				https://github.com/RicterZ/nhentai.git
				synced 2025-11-04 02:50:55 +01:00 
			
		
		
		
	Merge pull request #21 from mentaterasmus/master
fixing issue 16 and adding functionalities
This commit is contained in:
		@@ -47,6 +47,8 @@ nhentai --login "username:password" --download
 | 
				
			|||||||
`--timeout`:指定下载图片的超时时间,默认为 30 秒。  
 | 
					`--timeout`:指定下载图片的超时时间,默认为 30 秒。  
 | 
				
			||||||
`--proxy`:指定下载的代理,例如: http://127.0.0.1:8080/
 | 
					`--proxy`:指定下载的代理,例如: http://127.0.0.1:8080/
 | 
				
			||||||
`--login`:nhentai 账号的“用户名:密码”组合
 | 
					`--login`:nhentai 账号的“用户名:密码”组合
 | 
				
			||||||
 | 
					`--nohtml`:nhentai Don't generate HTML
 | 
				
			||||||
 | 
					`--cbz`:nhentai Generate Comic Book CBZ file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### nHentai Mirror
 | 
					### nHentai Mirror
 | 
				
			||||||
如果想用自建镜像下载 nhentai 的本子,需要搭建 nhentai.net 和 i.nhentai.net 的反向代理。  
 | 
					如果想用自建镜像下载 nhentai 的本子,需要搭建 nhentai.net 和 i.nhentai.net 的反向代理。  
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -56,6 +56,12 @@ def cmd_parser():
 | 
				
			|||||||
    parser.add_option('--login', '-l', type='str', dest='login', action='store',
 | 
					    parser.add_option('--login', '-l', type='str', dest='login', action='store',
 | 
				
			||||||
                      help='username:password pair of nhentai account')
 | 
					                      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 Comic Book CBZ File')                      
 | 
				
			||||||
 | 
					                      
 | 
				
			||||||
    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))
 | 
				
			||||||
    except (NameError, TypeError):
 | 
					    except (NameError, TypeError):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,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
 | 
					from nhentai.utils import generate_html, generate_cbz
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
@@ -47,10 +47,13 @@ def main():
 | 
				
			|||||||
        for doujinshi in doujinshi_list:
 | 
					        for doujinshi in doujinshi_list:
 | 
				
			||||||
            doujinshi.downloader = downloader
 | 
					            doujinshi.downloader = downloader
 | 
				
			||||||
            doujinshi.download()
 | 
					            doujinshi.download()
 | 
				
			||||||
            generate_html(options.output_dir, doujinshi)
 | 
					            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':
 | 
					        if not platform.system() == 'Windows':
 | 
				
			||||||
            logger.log(15, '🍺 All done.')
 | 
					            logger.log(15, '? All done.')
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            logger.log(15, 'All done.')
 | 
					            logger.log(15, 'All done.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,7 +27,7 @@ def login_parser(username, password):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    s.get(constant.LOGIN_URL)
 | 
					    s.get(constant.LOGIN_URL)
 | 
				
			||||||
    content = s.get(constant.LOGIN_URL).content
 | 
					    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'})
 | 
					    csrf_token_elem = html.find('input', attrs={'name': 'csrfmiddlewaretoken'})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if not csrf_token_elem:
 | 
					    if not csrf_token_elem:
 | 
				
			||||||
@@ -44,7 +44,7 @@ def login_parser(username, password):
 | 
				
			|||||||
        logger.error('Login failed, please check your username and password')
 | 
					        logger.error('Login failed, please check your username and password')
 | 
				
			||||||
        exit(1)
 | 
					        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'})
 | 
					    count = html.find('span', attrs={'class': 'count'})
 | 
				
			||||||
    if not count:
 | 
					    if not count:
 | 
				
			||||||
        logger.error('Cannot get count of your favorites, maybe login failed.')
 | 
					        logger.error('Cannot get count of your favorites, maybe login failed.')
 | 
				
			||||||
@@ -95,7 +95,7 @@ def doujinshi_parser(id_):
 | 
				
			|||||||
        logger.critical(str(e))
 | 
					        logger.critical(str(e))
 | 
				
			||||||
        exit(1)
 | 
					        exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    doujinshi['name'] = response['title']['english']
 | 
					    doujinshi['name'] = str(response['title']['english'].encode('utf-8'))[2:]
 | 
				
			||||||
    doujinshi['subtitle'] = response['title']['japanese']
 | 
					    doujinshi['subtitle'] = response['title']['japanese']
 | 
				
			||||||
    doujinshi['img_id'] = response['media_id']
 | 
					    doujinshi['img_id'] = response['media_id']
 | 
				
			||||||
    doujinshi['ext'] = ''.join(map(lambda s: s['t'], response['images']['pages']))
 | 
					    doujinshi['ext'] = ''.join(map(lambda s: s['t'], response['images']['pages']))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,8 @@ from __future__ import unicode_literals, print_function
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import string
 | 
					import string
 | 
				
			||||||
 | 
					import zipfile
 | 
				
			||||||
 | 
					import shutil
 | 
				
			||||||
from nhentai.logger import logger
 | 
					from nhentai.logger import logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -35,7 +37,7 @@ def generate_html(output_dir='.', doujinshi_obj=None):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if doujinshi_obj is not None:
 | 
					    if doujinshi_obj is not None:
 | 
				
			||||||
        doujinshi_dir = os.path.join(output_dir, format_filename('%s-%s' % (doujinshi_obj.id,
 | 
					        doujinshi_dir = os.path.join(output_dir, format_filename('%s-%s' % (doujinshi_obj.id,
 | 
				
			||||||
                                                                            doujinshi_obj.name[:200])))
 | 
					                                                                            str(doujinshi_obj.name[:200]))))
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        doujinshi_dir = '.'
 | 
					        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')))
 | 
					    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):
 | 
					def format_filename(s):
 | 
				
			||||||
    """Take a string and return a valid filename constructed from the string.
 | 
					    """Take a string and return a valid filename constructed from the string.
 | 
				
			||||||
Uses a whitelist approach: any characters not present in valid_chars are
 | 
					Uses a whitelist approach: any characters not present in valid_chars are
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user