mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-07-01 16:09:28 +02:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
263dba51f3 | |||
049ab4d9ad | |||
b173a6c28f | |||
b64b718c88 | |||
8317662664 | |||
13e60a69e9 | |||
b5acbc76fd | |||
1eb1b5c04c | |||
2acb6a1249 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@ dist/
|
||||
*.egg-info
|
||||
.python-version
|
||||
.DS_Store
|
||||
output/
|
||||
|
@ -13,9 +13,9 @@ install:
|
||||
|
||||
script:
|
||||
- echo 268642 > /tmp/test.txt
|
||||
- NHENTAI=https://nhentai.net nhentai --cookie '__cfduid=da09f237ceb0f51c75980b0b3fda3ce571558179357; _ga=GA1.2.2000087053.1558179358; _gid=GA1.2.717818542.1558179358; csrftoken=iSxrTFOjrujJqauhAqWvTTI9dl3sfWnxdEFoMuqgmlBrbMin5Gj9wJW4r61cmH1X; sessionid=ewuaayfewbzpiukrarx9d52oxwlz2esd'
|
||||
- NHENTAI=https://nhentai.net nhentai --search umaru
|
||||
- NHENTAI=https://nhentai.net nhentai --id=152503,146134 -t 10 --output=/tmp/
|
||||
- NHENTAI=https://nhentai.net nhentai -l nhentai_test:nhentai --download --output=/tmp/
|
||||
- NHENTAI=https://nhentai.net nhentai --id=152503,146134 -t 10 --output=/tmp/ --cbz
|
||||
- NHENTAI=https://nhentai.net nhentai --tag lolicon
|
||||
- NHENTAI=https://nhentai.net nhentai --id 92066 --output=/tmp/ --cbz
|
||||
- NHENTAI=https://nhentai.net nhentai -F
|
||||
- NHENTAI=https://nhentai.net nhentai --file /tmp/test.txt
|
||||
|
@ -7,7 +7,7 @@ nhentai
|
||||
|_| |_|_| |_|\___|_| |_|\__\__,_|_|
|
||||
|
||||
あなたも変態。 いいね?
|
||||
[](https://travis-ci.org/RicterZ/nhentai) 
|
||||
[](https://travis-ci.org/RicterZ/nhentai)  [](https://github.com/RicterZ/nhentai/blob/master/LICENSE)
|
||||
|
||||
|
||||
nHentai is a CLI tool for downloading doujinshi from [nhentai.net](http://nhentai.net).
|
||||
@ -26,6 +26,8 @@ nHentai is a CLI tool for downloading doujinshi from [nhentai.net](http://nhenta
|
||||
### Usage
|
||||
**IMPORTANT**: To bypass the nhentai frequency limit, you should use `--login` option to log into nhentai.net.
|
||||
|
||||
*The default download folder will be the path where you run the command (CLI path).*
|
||||
|
||||
Download specified doujinshi:
|
||||
```bash
|
||||
nhentai --id=123855,123866
|
||||
@ -78,8 +80,5 @@ NHENTAI=http://h.loli.club nhentai --id 123456
|
||||

|
||||

|
||||
|
||||
### License
|
||||
MIT
|
||||
|
||||
### あなたも変態
|
||||

|
||||
|
@ -1,14 +1,15 @@
|
||||
# coding: utf-8
|
||||
from __future__ import print_function
|
||||
import os
|
||||
import sys
|
||||
from optparse import OptionParser
|
||||
from nhentai import __version__
|
||||
try:
|
||||
from itertools import ifilter as filter
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
import nhentai.constant as constant
|
||||
from nhentai import __version__
|
||||
from nhentai.utils import urlparse, generate_html
|
||||
from nhentai.logger import logger
|
||||
|
||||
@ -40,16 +41,25 @@ def cmd_parser():
|
||||
'\n nhentai --file [filename]'
|
||||
'\n\nEnvironment Variable:\n'
|
||||
' NHENTAI nhentai mirror url')
|
||||
# operation options
|
||||
parser.add_option('--download', dest='is_download', action='store_true',
|
||||
help='download doujinshi (for search results)')
|
||||
parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
|
||||
parser.add_option('--show', dest='is_show', action='store_true', help='just show the doujinshi information')
|
||||
|
||||
# doujinshi options
|
||||
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('--tag', type='string', dest='tag', action='store', help='download doujinshi by tag')
|
||||
parser.add_option('--favorites', '-F', action='store_true', dest='favorites',
|
||||
help='list or download your favorites.')
|
||||
|
||||
# page options
|
||||
parser.add_option('--page', type='int', dest='page', action='store', default=1,
|
||||
help='page number of search results')
|
||||
parser.add_option('--tag', type='string', dest='tag', action='store', help='download doujinshi by tag')
|
||||
parser.add_option('--max-page', type='int', dest='max_page', action='store', default=1,
|
||||
help='The max page when recursive download tagged doujinshi')
|
||||
|
||||
# download options
|
||||
parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
|
||||
help='output dir')
|
||||
parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
|
||||
@ -58,20 +68,21 @@ def cmd_parser():
|
||||
help='timeout for downloading doujinshi')
|
||||
parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
|
||||
help='uses a proxy, for example: http://127.0.0.1:1080')
|
||||
parser.add_option('--file', '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
|
||||
|
||||
# generate options
|
||||
parser.add_option('--html', dest='html_viewer', action='store_true',
|
||||
help='generate a html viewer at current directory')
|
||||
|
||||
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')
|
||||
|
||||
help='don\'t generate HTML')
|
||||
parser.add_option('--cbz', dest='is_cbz', action='store_true',
|
||||
help='Generate Comic Book CBZ File')
|
||||
help='generate Comic Book CBZ File')
|
||||
parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
|
||||
help='Remove downloaded doujinshi dir when generated CBZ file.')
|
||||
parser.add_option('--file', '-f', type='string', dest='file', action='store', help='Read gallery IDs from file.')
|
||||
help='remove downloaded doujinshi dir when generated CBZ file.')
|
||||
|
||||
# nhentai options
|
||||
parser.add_option('--cookie', type='str', dest='cookie', action='store',
|
||||
help='set cookie of nhentai to bypass Google recaptcha')
|
||||
|
||||
try:
|
||||
sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
|
||||
@ -86,6 +97,25 @@ def cmd_parser():
|
||||
generate_html()
|
||||
exit(0)
|
||||
|
||||
if os.path.exists(os.path.join(constant.NHENTAI_HOME, 'cookie')):
|
||||
with open(os.path.join(constant.NHENTAI_HOME, 'cookie'), 'r') as f:
|
||||
constant.COOKIE = f.read()
|
||||
|
||||
if args.cookie:
|
||||
try:
|
||||
if not os.path.exists(constant.NHENTAI_HOME):
|
||||
os.mkdir(constant.NHENTAI_HOME)
|
||||
|
||||
with open(os.path.join(constant.NHENTAI_HOME, 'cookie'), 'w') as f:
|
||||
f.write(args.cookie)
|
||||
except Exception as e:
|
||||
logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
|
||||
exit(1)
|
||||
|
||||
logger.info('Cookie saved.')
|
||||
exit(0)
|
||||
|
||||
'''
|
||||
if args.login:
|
||||
try:
|
||||
_, _ = args.login.split(':', 1)
|
||||
@ -95,9 +125,15 @@ def cmd_parser():
|
||||
|
||||
if not args.is_download:
|
||||
logger.warning('YOU DO NOT SPECIFY `--download` OPTION !!!')
|
||||
'''
|
||||
|
||||
if args.favorites:
|
||||
if not constant.COOKIE:
|
||||
logger.warning('Cookie has not been set, please use `nhentai --cookie \'COOKIE\'` to set it.')
|
||||
exit(1)
|
||||
|
||||
if args.id:
|
||||
_ = map(lambda id: id.strip(), args.id.split(','))
|
||||
_ = map(lambda id_: id_.strip(), args.id.split(','))
|
||||
args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
|
||||
|
||||
if args.file:
|
||||
@ -106,12 +142,12 @@ def cmd_parser():
|
||||
args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
|
||||
|
||||
if (args.is_download or args.is_show) and not args.id and not args.keyword and \
|
||||
not args.login and not args.tag:
|
||||
not args.tag and not args.favorites:
|
||||
logger.critical('Doujinshi id(s) are required for downloading')
|
||||
parser.print_help()
|
||||
exit(1)
|
||||
|
||||
if not args.keyword and not args.id and not args.login and not args.tag:
|
||||
if not args.keyword and not args.id and not args.tag and not args.favorites:
|
||||
parser.print_help()
|
||||
exit(1)
|
||||
|
||||
|
@ -5,7 +5,7 @@ import signal
|
||||
import platform
|
||||
|
||||
from nhentai.cmdline import cmd_parser, banner
|
||||
from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, login_parser, tag_parser, login
|
||||
from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, favorites_parser, tag_parser, login
|
||||
from nhentai.doujinshi import Doujinshi
|
||||
from nhentai.downloader import Downloader
|
||||
from nhentai.logger import logger
|
||||
@ -21,19 +21,21 @@ def main():
|
||||
doujinshi_ids = []
|
||||
doujinshi_list = []
|
||||
|
||||
if options.login:
|
||||
username, password = options.login.split(':', 1)
|
||||
logger.info('Logging in to nhentai using credential pair \'%s:%s\'' % (username, '*' * len(password)))
|
||||
login(username, password)
|
||||
if options.favorites:
|
||||
if not options.is_download:
|
||||
logger.warning('You do not specify --download option')
|
||||
|
||||
if options.is_download:
|
||||
for doujinshi_info in login_parser():
|
||||
doujinshi_list.append(Doujinshi(**doujinshi_info))
|
||||
for doujinshi_info in favorites_parser():
|
||||
doujinshi_list.append(Doujinshi(**doujinshi_info))
|
||||
|
||||
if not options.is_download:
|
||||
print_doujinshi([{'id': i.id, 'title': i.name} for i in doujinshi_list])
|
||||
exit(0)
|
||||
|
||||
if options.tag:
|
||||
doujinshis = tag_parser(options.tag, max_page=options.max_page)
|
||||
print_doujinshi(doujinshis)
|
||||
if options.is_download:
|
||||
if options.is_download and doujinshis:
|
||||
doujinshi_ids = map(lambda d: d['id'], doujinshis)
|
||||
|
||||
if options.keyword:
|
||||
|
@ -1,6 +1,7 @@
|
||||
# coding: utf-8
|
||||
from __future__ import unicode_literals, print_function
|
||||
import os
|
||||
import tempfile
|
||||
from nhentai.utils import urlparse
|
||||
|
||||
BASE_URL = os.getenv('NHENTAI', 'https://nhentai.net')
|
||||
@ -20,4 +21,8 @@ FAV_URL = '%s/favorites/' % BASE_URL
|
||||
u = urlparse(BASE_URL)
|
||||
IMAGE_URL = '%s://i.%s/galleries' % (u.scheme, u.hostname)
|
||||
|
||||
NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai')
|
||||
|
||||
PROXY = {}
|
||||
|
||||
COOKIE = ''
|
||||
|
@ -48,7 +48,7 @@ class Doujinshi(object):
|
||||
["Characters", self.info.character],
|
||||
["Authors", self.info.artist],
|
||||
["Language", self.info.language],
|
||||
["Tags", ', '.join(self.info.tag.keys())],
|
||||
["Tags", self.info.tags],
|
||||
["URL", self.url],
|
||||
["Pages", self.pages],
|
||||
]
|
||||
|
@ -25,6 +25,7 @@ def request(method, url, **kwargs):
|
||||
if not hasattr(session, method):
|
||||
raise AttributeError('\'requests.Session\' object has no attribute \'{0}\''.format(method))
|
||||
|
||||
session.headers.update({'Cookie': constant.COOKIE})
|
||||
return getattr(session, method)(url, proxies=constant.PROXY, verify=False, **kwargs)
|
||||
|
||||
|
||||
@ -37,6 +38,7 @@ def _get_csrf_token(content):
|
||||
|
||||
|
||||
def login(username, password):
|
||||
logger.warning('This feature is deprecated, please use --cookie to set your cookie.')
|
||||
csrf_token = _get_csrf_token(request('get', url=constant.LOGIN_URL).text)
|
||||
if os.getenv('DEBUG'):
|
||||
logger.info('Getting CSRF token ...')
|
||||
@ -51,7 +53,7 @@ def login(username, password):
|
||||
}
|
||||
resp = request('post', url=constant.LOGIN_URL, data=login_dict)
|
||||
|
||||
if 'You\'re loading pages way too quickly.' in resp.text:
|
||||
if 'You\'re loading pages way too quickly.' in resp.text or 'Really, slow down' in resp.text:
|
||||
csrf_token = _get_csrf_token(resp.text)
|
||||
resp = request('post', url=resp.url, data={'csrfmiddlewaretoken': csrf_token, 'next': '/'})
|
||||
|
||||
@ -59,18 +61,17 @@ def login(username, password):
|
||||
logger.error('Login failed, please check your username and password')
|
||||
exit(1)
|
||||
|
||||
if 'You\'re loading pages way too quickly.' in resp.text:
|
||||
logger.error('You meet challenge insistently, please submit a issue'
|
||||
' at https://github.com/RicterZ/nhentai/issues')
|
||||
if 'You\'re loading pages way too quickly.' in resp.text or 'Really, slow down' in resp.text:
|
||||
logger.error('Using nhentai --cookie \'YOUR_COOKIE_HERE\' to save your Cookie.')
|
||||
exit(2)
|
||||
|
||||
|
||||
def login_parser():
|
||||
def favorites_parser():
|
||||
html = BeautifulSoup(request('get', constant.FAV_URL).content, 'html.parser')
|
||||
count = html.find('span', attrs={'class': 'count'})
|
||||
if not count:
|
||||
logger.error("Can't get your number of favorited doujins. Did the login failed?")
|
||||
return
|
||||
return []
|
||||
|
||||
count = int(count.text.strip('(').strip(')').replace(',', ''))
|
||||
if count == 0:
|
||||
@ -91,19 +92,15 @@ def login_parser():
|
||||
ret = []
|
||||
doujinshi_id = re.compile('data-id="([\d]+)"')
|
||||
|
||||
def _callback(request, result):
|
||||
ret.append(result)
|
||||
|
||||
thread_pool = threadpool.ThreadPool(5)
|
||||
|
||||
for page in range(1, pages + 1):
|
||||
try:
|
||||
logger.info('Getting doujinshi ids of page %d' % page)
|
||||
resp = request('get', constant.FAV_URL + '?page=%d' % page).text
|
||||
ids = doujinshi_id.findall(resp)
|
||||
requests_ = threadpool.makeRequests(doujinshi_parser, ids, _callback)
|
||||
[thread_pool.putRequest(req) for req in requests_]
|
||||
thread_pool.wait()
|
||||
|
||||
for i in ids:
|
||||
ret.append(doujinshi_parser(i))
|
||||
|
||||
except Exception as e:
|
||||
logger.error('Error: %s, continue', str(e))
|
||||
|
||||
@ -121,7 +118,14 @@ def doujinshi_parser(id_):
|
||||
url = '{0}/{1}/'.format(constant.DETAIL_URL, id_)
|
||||
|
||||
try:
|
||||
response = request('get', url).content
|
||||
response = request('get', url)
|
||||
if response.status_code in (200, ):
|
||||
response = response.content
|
||||
else:
|
||||
logger.debug('Slow down and retry ({}) ...'.format(id_))
|
||||
time.sleep(1)
|
||||
return doujinshi_parser(str(id_))
|
||||
|
||||
except Exception as e:
|
||||
logger.critical(str(e))
|
||||
raise SystemExit
|
||||
|
@ -46,17 +46,33 @@ document.getElementById('image-container').onclick = event => {
|
||||
document.onkeypress = event => {
|
||||
switch (event.key.toLowerCase()) {
|
||||
// Previous Image
|
||||
case 'arrowleft':
|
||||
case 'a':
|
||||
changePage(currentPage - 1);
|
||||
break;
|
||||
|
||||
// Next Image
|
||||
case ' ':
|
||||
case 'esc': // future close page function
|
||||
case 'enter':
|
||||
case 'arrowright':
|
||||
case 'd':
|
||||
changePage(currentPage + 1);
|
||||
break;
|
||||
}// remove arrow cause it won't work
|
||||
};
|
||||
|
||||
document.onkeydown = event =>{
|
||||
switch (event.keyCode) {
|
||||
case 37: //left
|
||||
changePage(currentPage - 1);
|
||||
break;
|
||||
case 38: //up
|
||||
changePage(currentPage - 1);
|
||||
break;
|
||||
case 39: //right
|
||||
changePage(currentPage + 1);
|
||||
break;
|
||||
case 40: //down
|
||||
changePage(currentPage + 1);
|
||||
break;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user