mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-07-01 16:09:28 +02:00
Compare commits
104 Commits
Author | SHA1 | Date | |
---|---|---|---|
31b95fe2dd | |||
be8c97f8d4 | |||
348e51676e | |||
ea356a1ca2 | |||
5a4dfb8a76 | |||
4b15744ceb | |||
b05fa16286 | |||
0879486881 | |||
c66ba730d3 | |||
606c5e0ffd | |||
ba04f81a6f | |||
6519e6f221 | |||
7594625d72 | |||
4948c8f0c5 | |||
e22a99fa8c | |||
19a1d5c404 | |||
ad1e876611 | |||
1de7e1f998 | |||
b97e707817 | |||
6ef2189bfe | |||
24be2d37d4 | |||
d9d2a6fb91 | |||
bd38294bb7 | |||
2cf4e6718e | |||
8cd4b948e7 | |||
f884384eb3 | |||
87afab46c4 | |||
c7b1d7e6a8 | |||
ad02371158 | |||
7c9d55e0ee | |||
00aad774ae | |||
373086b459 | |||
3a83f99771 | |||
00627ab36a | |||
592e163891 | |||
84523475b0 | |||
5f5461c902 | |||
05e6ceb3cd | |||
db59426503 | |||
74197f8f90 | |||
6d91a39533 | |||
e181e0b9dd | |||
6fed1f94cb | |||
9cfb23c8ec | |||
fc347cdadf | |||
1cdebaab61 | |||
9513141ccf | |||
bdc9fa113e | |||
36946111db | |||
ce8ae54536 | |||
7aedb905d6 | |||
8b8b5f193e | |||
fc99d91ac1 | |||
ba141efba7 | |||
f78d8750f3 | |||
08bb8ffda4 | |||
af379c825c | |||
2f9386f22c | |||
3667bc34b7 | |||
84749c56bd | |||
24f79e0945 | |||
edc46a9531 | |||
72035a14e6 | |||
472528e464 | |||
3f5915fd2a | |||
0cd2576dab | |||
445a8c052e | |||
7a75afef0a | |||
a5813e19b1 | |||
8462d2f2aa | |||
51074ee948 | |||
9c7354be32 | |||
7f48b3edd1 | |||
d84b827241 | |||
4ac161a38c | |||
648b6f87bf | |||
2ec1283ba8 | |||
a9bd46b426 | |||
c52bc271fc | |||
f2d22f8e7d | |||
ea6089ff31 | |||
670d14c3f3 | |||
b46106a5bc | |||
f04359e486 | |||
6861cbcbc1 | |||
e0938c5a0e | |||
641f8e4c51 | |||
b2fae226f9 | |||
4aa34c668a | |||
f157ac3246 | |||
139e01d3ca | |||
4d870e36a1 | |||
74b0df26a9 | |||
1746e731ec | |||
8ad60d9838 | |||
be05b9c0eb | |||
9054b98934 | |||
b82201ff27 | |||
532c74e075 | |||
5a50a5b1ba | |||
b5fe48746e | |||
94d8da655a | |||
6ff2816d95 | |||
0a94ef9cf1 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,3 +7,4 @@ dist/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
output/
|
output/
|
||||||
venv/
|
venv/
|
||||||
|
.vscode/
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
include README.md
|
include README.md
|
||||||
include requirements.txt
|
include requirements.txt
|
||||||
include nhentai/viewer/index.html
|
include nhentai/viewer/*
|
||||||
include nhentai/viewer/styles.css
|
include nhentai/viewer/default/*
|
||||||
include nhentai/viewer/scripts.js
|
include nhentai/viewer/minimal/*
|
||||||
include nhentai/viewer/main.html
|
|
||||||
include nhentai/viewer/main.css
|
|
||||||
include nhentai/viewer/main.js
|
|
22
README.rst
22
README.rst
@ -50,7 +50,14 @@ Installation (Gentoo)
|
|||||||
|
|
||||||
layman -fa glicOne
|
layman -fa glicOne
|
||||||
sudo emerge net-misc/nhentai
|
sudo emerge net-misc/nhentai
|
||||||
|
|
||||||
|
=====================
|
||||||
|
Installation (NixOs)
|
||||||
|
=====================
|
||||||
|
.. code-block::
|
||||||
|
|
||||||
|
nix-env -iA nixos.nhentai
|
||||||
|
|
||||||
=====
|
=====
|
||||||
Usage
|
Usage
|
||||||
=====
|
=====
|
||||||
@ -65,7 +72,19 @@ Set your nhentai cookie against captcha:
|
|||||||
|
|
||||||
nhentai --cookie "YOUR COOKIE FROM nhentai.net"
|
nhentai --cookie "YOUR COOKIE FROM nhentai.net"
|
||||||
|
|
||||||
**NOTE**: The format of the cookie is `"csrftoken=TOKEN; sessionid=ID"`
|
**NOTE**
|
||||||
|
|
||||||
|
- The format of the cookie is `"csrftoken=TOKEN; sessionid=ID; cf_clearance=CLOUDFLARE"`
|
||||||
|
- `cf_clearance` cookie and useragent must be set if you encounter "blocked by cloudflare captcha" error. Make sure you use the same IP and useragent as when you got it
|
||||||
|
|
||||||
|
| To get csrftoken and sessionid, first login to your nhentai account in web browser, then:
|
||||||
|
| (Chrome) |ve| |ld| More tools |ld| Developer tools |ld| Application |ld| Storage |ld| Cookies |ld| https://nhentai.net
|
||||||
|
| (Firefox) |hv| |ld| Web Developer |ld| Web Developer Tools |ld| Storage |ld| Cookies |ld| https://nhentai.net
|
||||||
|
|
|
||||||
|
|
||||||
|
.. |hv| unicode:: U+2630 .. https://www.compart.com/en/unicode/U+2630
|
||||||
|
.. |ve| unicode:: U+22EE .. https://www.compart.com/en/unicode/U+22EE
|
||||||
|
.. |ld| unicode:: U+2014 .. https://www.compart.com/en/unicode/U+2014
|
||||||
|
|
||||||
Download specified doujinshi:
|
Download specified doujinshi:
|
||||||
|
|
||||||
@ -112,6 +131,7 @@ Supported doujinshi folder formatter:
|
|||||||
- %t: Doujinshi name
|
- %t: Doujinshi name
|
||||||
- %s: Doujinshi subtitle (translated name)
|
- %s: Doujinshi subtitle (translated name)
|
||||||
- %a: Doujinshi authors' name
|
- %a: Doujinshi authors' name
|
||||||
|
- %p: Doujinshi pretty name
|
||||||
|
|
||||||
|
|
||||||
Other options:
|
Other options:
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
__version__ = '0.4.2'
|
__version__ = '0.4.17'
|
||||||
__author__ = 'RicterZ'
|
__author__ = 'RicterZ'
|
||||||
__email__ = 'ricterzheng@gmail.com'
|
__email__ = 'ricterzheng@gmail.com'
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import print_function
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import json
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from itertools import ifilter as filter
|
from itertools import ifilter as filter
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -13,17 +15,6 @@ from nhentai import __version__
|
|||||||
from nhentai.utils import urlparse, generate_html, generate_main_html, DB
|
from nhentai.utils import urlparse, generate_html, generate_main_html, DB
|
||||||
from nhentai.logger import logger
|
from nhentai.logger import logger
|
||||||
|
|
||||||
try:
|
|
||||||
if sys.version_info < (3, 0, 0):
|
|
||||||
import codecs
|
|
||||||
import locale
|
|
||||||
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
|
|
||||||
sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
|
|
||||||
|
|
||||||
except NameError:
|
|
||||||
# python3
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def banner():
|
def banner():
|
||||||
logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
|
logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
|
||||||
@ -35,7 +26,29 @@ def banner():
|
|||||||
''' % __version__)
|
''' % __version__)
|
||||||
|
|
||||||
|
|
||||||
|
def load_config():
|
||||||
|
if not os.path.exists(constant.NHENTAI_CONFIG_FILE):
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(constant.NHENTAI_CONFIG_FILE, 'r') as f:
|
||||||
|
constant.CONFIG.update(json.load(f))
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logger.error('Failed to load config file.')
|
||||||
|
write_config()
|
||||||
|
|
||||||
|
|
||||||
|
def write_config():
|
||||||
|
if not os.path.exists(constant.NHENTAI_HOME):
|
||||||
|
os.mkdir(constant.NHENTAI_HOME)
|
||||||
|
|
||||||
|
with open(constant.NHENTAI_CONFIG_FILE, 'w') as f:
|
||||||
|
f.write(json.dumps(constant.CONFIG))
|
||||||
|
|
||||||
|
|
||||||
def cmd_parser():
|
def cmd_parser():
|
||||||
|
load_config()
|
||||||
|
|
||||||
parser = OptionParser('\n nhentai --search [keyword] --download'
|
parser = OptionParser('\n nhentai --search [keyword] --download'
|
||||||
'\n NHENTAI=http://h.loli.club nhentai --id [ID ...]'
|
'\n NHENTAI=http://h.loli.club nhentai --id [ID ...]'
|
||||||
'\n nhentai --file [filename]'
|
'\n nhentai --file [filename]'
|
||||||
@ -54,16 +67,16 @@ def cmd_parser():
|
|||||||
help='list or download your favorites.')
|
help='list or download your favorites.')
|
||||||
|
|
||||||
# page options
|
# page options
|
||||||
parser.add_option('--page', type='int', dest='page', action='store', default=1,
|
parser.add_option('--page-all', dest='page_all', action='store_true', default=False,
|
||||||
help='page number of search results')
|
help='all search results')
|
||||||
parser.add_option('--page-range', type='string', dest='page_range', action='store',
|
parser.add_option('--page', '--page-range', type='string', dest='page', action='store', default='',
|
||||||
help='page range of favorites. e.g. 1,2-5,14')
|
help='page number of search results. e.g. 1,2-5,14')
|
||||||
parser.add_option('--sorting', dest='sorting', action='store', default='recent',
|
parser.add_option('--sorting', dest='sorting', action='store', default='recent',
|
||||||
help='sorting of doujinshi (recent / popular / popular-[today|week])',
|
help='sorting of doujinshi (recent / popular / popular-[today|week])',
|
||||||
choices=['recent', 'popular', 'popular-today', 'popular-week'])
|
choices=['recent', 'popular', 'popular-today', 'popular-week'])
|
||||||
|
|
||||||
# download options
|
# download options
|
||||||
parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='',
|
parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='./',
|
||||||
help='output dir')
|
help='output dir')
|
||||||
parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
|
parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
|
||||||
help='thread count for downloading doujinshi')
|
help='thread count for downloading doujinshi')
|
||||||
@ -71,11 +84,12 @@ def cmd_parser():
|
|||||||
help='timeout for downloading doujinshi')
|
help='timeout for downloading doujinshi')
|
||||||
parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
|
parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
|
||||||
help='slow down between downloading every doujinshi')
|
help='slow down between downloading every doujinshi')
|
||||||
parser.add_option('--proxy', '-p', type='string', dest='proxy', action='store', default='',
|
parser.add_option('--proxy', type='string', dest='proxy', action='store',
|
||||||
help='store a proxy, for example: -p \'http://127.0.0.1:1080\'')
|
help='store a proxy, for example: -p \'http://127.0.0.1:1080\'')
|
||||||
parser.add_option('--file', '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
|
parser.add_option('--file', '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
|
||||||
parser.add_option('--format', type='string', dest='name_format', action='store',
|
parser.add_option('--format', type='string', dest='name_format', action='store',
|
||||||
help='format the saved folder name', default='[%i][%a][%t]')
|
help='format the saved folder name', default='[%i][%a][%t]')
|
||||||
|
parser.add_option('--dry-run', '-r', action='store_true', dest='dryrun', help='Dry run, skip file download.')
|
||||||
|
|
||||||
# generate options
|
# generate options
|
||||||
parser.add_option('--html', dest='html_viewer', action='store_true',
|
parser.add_option('--html', dest='html_viewer', action='store_true',
|
||||||
@ -90,20 +104,27 @@ def cmd_parser():
|
|||||||
help='generate PDF file')
|
help='generate PDF file')
|
||||||
parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
|
parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
|
||||||
help='remove downloaded doujinshi dir when generated CBZ or PDF file.')
|
help='remove downloaded doujinshi dir when generated CBZ or PDF file.')
|
||||||
|
parser.add_option('--meta', dest='generate_metadata', action='store_true',
|
||||||
|
help='generate a metadata file in doujinshi format')
|
||||||
|
|
||||||
# nhentai options
|
# nhentai options
|
||||||
parser.add_option('--cookie', type='str', dest='cookie', action='store',
|
parser.add_option('--cookie', type='str', dest='cookie', action='store',
|
||||||
help='set cookie of nhentai to bypass Google recaptcha')
|
help='set cookie of nhentai to bypass Cloudflare captcha')
|
||||||
|
parser.add_option('--useragent', type='str', dest='useragent', action='store',
|
||||||
|
help='set useragent to bypass Cloudflare captcha')
|
||||||
parser.add_option('--language', type='str', dest='language', action='store',
|
parser.add_option('--language', type='str', dest='language', action='store',
|
||||||
help='set default language to parse doujinshis')
|
help='set default language to parse doujinshis')
|
||||||
|
parser.add_option('--clean-language', dest='clean_language', action='store_true', default=False,
|
||||||
|
help='set DEFAULT as language to parse doujinshis')
|
||||||
parser.add_option('--save-download-history', dest='is_save_download_history', action='store_true',
|
parser.add_option('--save-download-history', dest='is_save_download_history', action='store_true',
|
||||||
default=False, help='save downloaded doujinshis, whose will be skipped if you re-download them')
|
default=False, help='save downloaded doujinshis, whose will be skipped if you re-download them')
|
||||||
parser.add_option('--clean-download-history', action='store_true', default=False, dest='clean_download_history',
|
parser.add_option('--clean-download-history', action='store_true', default=False, dest='clean_download_history',
|
||||||
help='clean download history')
|
help='clean download history')
|
||||||
|
parser.add_option('--template', dest='viewer_template', action='store',
|
||||||
|
help='set viewer template', default='')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
|
sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
|
||||||
print()
|
|
||||||
except (NameError, TypeError):
|
except (NameError, TypeError):
|
||||||
pass
|
pass
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
@ -112,7 +133,7 @@ def cmd_parser():
|
|||||||
args, _ = parser.parse_args(sys.argv[1:])
|
args, _ = parser.parse_args(sys.argv[1:])
|
||||||
|
|
||||||
if args.html_viewer:
|
if args.html_viewer:
|
||||||
generate_html()
|
generate_html(template=constant.CONFIG['template'])
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
if args.main_viewer and not args.id and not args.keyword and not args.favorites:
|
if args.main_viewer and not args.id and not args.keyword and not args.favorites:
|
||||||
@ -126,69 +147,54 @@ def cmd_parser():
|
|||||||
logger.info('Download history cleaned.')
|
logger.info('Download history cleaned.')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
if os.path.exists(constant.NHENTAI_COOKIE):
|
# --- set config ---
|
||||||
with open(constant.NHENTAI_COOKIE, 'r') as f:
|
if args.cookie is not None:
|
||||||
constant.COOKIE = f.read()
|
constant.CONFIG['cookie'] = args.cookie
|
||||||
|
write_config()
|
||||||
if args.cookie:
|
|
||||||
try:
|
|
||||||
if not os.path.exists(constant.NHENTAI_HOME):
|
|
||||||
os.mkdir(constant.NHENTAI_HOME)
|
|
||||||
|
|
||||||
with open(constant.NHENTAI_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.')
|
logger.info('Cookie saved.')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
elif args.useragent is not None:
|
||||||
if os.path.exists(constant.NHENTAI_LANGUAGE) and not args.language:
|
constant.CONFIG['useragent'] = args.useragent
|
||||||
with open(constant.NHENTAI_LANGUAGE, 'r') as f:
|
write_config()
|
||||||
constant.LANGUAGE = f.read()
|
logger.info('Useragent saved.')
|
||||||
args.language = f.read()
|
|
||||||
|
|
||||||
if args.language:
|
|
||||||
try:
|
|
||||||
if not os.path.exists(constant.NHENTAI_HOME):
|
|
||||||
os.mkdir(constant.NHENTAI_HOME)
|
|
||||||
|
|
||||||
with open(constant.NHENTAI_LANGUAGE, 'w') as f:
|
|
||||||
f.write(args.language)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
logger.info('Default language now is {}.'.format(args.language))
|
|
||||||
exit(0)
|
exit(0)
|
||||||
|
elif args.language is not None:
|
||||||
if os.path.exists(constant.NHENTAI_PROXY):
|
constant.CONFIG['language'] = args.language
|
||||||
with open(constant.NHENTAI_PROXY, 'r') as f:
|
write_config()
|
||||||
link = f.read()
|
logger.info('Default language now set to \'{0}\''.format(args.language))
|
||||||
constant.PROXY = {'http': link, 'https': link}
|
|
||||||
|
|
||||||
if args.proxy:
|
|
||||||
try:
|
|
||||||
if not os.path.exists(constant.NHENTAI_HOME):
|
|
||||||
os.mkdir(constant.NHENTAI_HOME)
|
|
||||||
|
|
||||||
proxy_url = urlparse(args.proxy)
|
|
||||||
if proxy_url.scheme not in ('http', 'https'):
|
|
||||||
logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
|
|
||||||
else:
|
|
||||||
with open(constant.NHENTAI_PROXY, 'w') as f:
|
|
||||||
f.write(args.proxy)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
logger.info('Proxy \'{0}\' saved.'.format(args.proxy))
|
|
||||||
exit(0)
|
exit(0)
|
||||||
|
# TODO: search without language
|
||||||
|
|
||||||
|
if args.proxy is not None:
|
||||||
|
proxy_url = urlparse(args.proxy)
|
||||||
|
if not args.proxy == '' and proxy_url.scheme not in ('http', 'https', 'socks5', 'socks5h', 'socks4', 'socks4a'):
|
||||||
|
logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
|
||||||
|
exit(0)
|
||||||
|
else:
|
||||||
|
constant.CONFIG['proxy'] = {
|
||||||
|
'http': args.proxy,
|
||||||
|
'https': args.proxy,
|
||||||
|
}
|
||||||
|
logger.info('Proxy now set to \'{0}\'.'.format(args.proxy))
|
||||||
|
write_config()
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
if args.viewer_template is not None:
|
||||||
|
if not args.viewer_template:
|
||||||
|
args.viewer_template = 'default'
|
||||||
|
|
||||||
|
if not os.path.exists(os.path.join(os.path.dirname(__file__),
|
||||||
|
'viewer/{}/index.html'.format(args.viewer_template))):
|
||||||
|
logger.error('Template \'{}\' does not exists'.format(args.viewer_template))
|
||||||
|
exit(1)
|
||||||
|
else:
|
||||||
|
constant.CONFIG['template'] = args.viewer_template
|
||||||
|
write_config()
|
||||||
|
|
||||||
|
# --- end set config ---
|
||||||
|
|
||||||
if args.favorites:
|
if args.favorites:
|
||||||
if not constant.COOKIE:
|
if not constant.CONFIG['cookie']:
|
||||||
logger.warning('Cookie has not been set, please use `nhentai --cookie \'COOKIE\'` to set it.')
|
logger.warning('Cookie has not been set, please use `nhentai --cookie \'COOKIE\'` to set it.')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
@ -206,7 +212,7 @@ def cmd_parser():
|
|||||||
parser.print_help()
|
parser.print_help()
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
if not args.keyword and not args.id and not args.favorites:
|
if not args.keyword and not args.id and not args.favorites:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
@ -217,4 +223,8 @@ def cmd_parser():
|
|||||||
logger.critical('Maximum number of used threads is 15')
|
logger.critical('Maximum number of used threads is 15')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
if args.dryrun and (args.is_cbz or args.is_pdf):
|
||||||
|
logger.critical('Cannot generate PDF or CBZ during dry-run')
|
||||||
|
exit(1)
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
@ -1,28 +1,40 @@
|
|||||||
#!/usr/bin/env python2.7
|
#!/usr/bin/env python2.7
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals, print_function
|
|
||||||
|
import sys
|
||||||
import signal
|
import signal
|
||||||
import platform
|
import platform
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from nhentai import constant
|
||||||
from nhentai.cmdline import cmd_parser, banner
|
from nhentai.cmdline import cmd_parser, banner
|
||||||
from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, favorites_parser
|
from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, favorites_parser
|
||||||
from nhentai.doujinshi import Doujinshi
|
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, generate_cbz, generate_main_html, generate_pdf, check_cookie, signal_handler, DB
|
from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, generate_metadata_file, \
|
||||||
|
paging, check_cookie, signal_handler, DB
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
banner()
|
banner()
|
||||||
|
|
||||||
|
if sys.version_info < (3, 0, 0):
|
||||||
|
logger.error('nhentai now only support Python 3.x')
|
||||||
|
exit(1)
|
||||||
|
|
||||||
options = cmd_parser()
|
options = cmd_parser()
|
||||||
logger.info('Using mirror: {0}'.format(BASE_URL))
|
logger.info('Using mirror: {0}'.format(BASE_URL))
|
||||||
|
|
||||||
from nhentai.constant import PROXY
|
# CONFIG['proxy'] will be changed after cmd_parser()
|
||||||
# constant.PROXY will be changed after cmd_parser()
|
if constant.CONFIG['proxy']['http']:
|
||||||
if PROXY:
|
logger.info('Using proxy: {0}'.format(constant.CONFIG['proxy']['http']))
|
||||||
logger.info('Using proxy: {0}'.format(PROXY))
|
|
||||||
|
if not constant.CONFIG['template']:
|
||||||
|
constant.CONFIG['template'] = 'default'
|
||||||
|
|
||||||
|
logger.info('Using viewer template "{}"'.format(constant.CONFIG['template']))
|
||||||
|
|
||||||
# check your cookie
|
# check your cookie
|
||||||
check_cookie()
|
check_cookie()
|
||||||
@ -31,18 +43,20 @@ def main():
|
|||||||
doujinshi_ids = []
|
doujinshi_ids = []
|
||||||
doujinshi_list = []
|
doujinshi_list = []
|
||||||
|
|
||||||
|
page_list = paging(options.page)
|
||||||
|
|
||||||
if options.favorites:
|
if options.favorites:
|
||||||
if not options.is_download:
|
if not options.is_download:
|
||||||
logger.warning('You do not specify --download option')
|
logger.warning('You do not specify --download option')
|
||||||
|
|
||||||
doujinshis = favorites_parser(options.page_range)
|
doujinshis = favorites_parser(page=page_list)
|
||||||
|
|
||||||
elif options.keyword:
|
elif options.keyword:
|
||||||
from nhentai.constant import LANGUAGE
|
if constant.CONFIG['language']:
|
||||||
if LANGUAGE:
|
logger.info('Using default language: {0}'.format(constant.CONFIG['language']))
|
||||||
logger.info('Using deafult language: {0}'.format(LANGUAGE))
|
options.keyword += ' language:{}'.format(constant.CONFIG['language'])
|
||||||
options.keyword += ', language:{}'.format(LANGUAGE)
|
doujinshis = search_parser(options.keyword, sorting=options.sorting, page=page_list,
|
||||||
doujinshis = search_parser(options.keyword, sorting=options.sorting, page=options.page)
|
is_page_all=options.page_all)
|
||||||
|
|
||||||
elif not doujinshi_ids:
|
elif not doujinshi_ids:
|
||||||
doujinshi_ids = options.id
|
doujinshi_ids = options.id
|
||||||
@ -51,11 +65,11 @@ def main():
|
|||||||
if options.is_download and doujinshis:
|
if options.is_download and doujinshis:
|
||||||
doujinshi_ids = [i['id'] for i in doujinshis]
|
doujinshi_ids = [i['id'] for i in doujinshis]
|
||||||
|
|
||||||
if options.is_save_download_history:
|
if options.is_save_download_history:
|
||||||
with DB() as db:
|
with DB() as db:
|
||||||
data = set(db.get_all())
|
data = map(int, db.get_all())
|
||||||
|
|
||||||
doujinshi_ids = list(set(doujinshi_ids) - data)
|
doujinshi_ids = list(set(map(int, doujinshi_ids)) - set(data))
|
||||||
|
|
||||||
if doujinshi_ids:
|
if doujinshi_ids:
|
||||||
for i, id_ in enumerate(doujinshi_ids):
|
for i, id_ in enumerate(doujinshi_ids):
|
||||||
@ -75,15 +89,20 @@ def main():
|
|||||||
timeout=options.timeout, delay=options.delay)
|
timeout=options.timeout, delay=options.delay)
|
||||||
|
|
||||||
for doujinshi in doujinshi_list:
|
for doujinshi in doujinshi_list:
|
||||||
|
if not options.dryrun:
|
||||||
|
doujinshi.downloader = downloader
|
||||||
|
doujinshi.download()
|
||||||
|
|
||||||
|
if options.generate_metadata:
|
||||||
|
table = doujinshi.table
|
||||||
|
generate_metadata_file(options.output_dir, table, doujinshi)
|
||||||
|
|
||||||
doujinshi.downloader = downloader
|
|
||||||
doujinshi.download()
|
|
||||||
if options.is_save_download_history:
|
if options.is_save_download_history:
|
||||||
with DB() as db:
|
with DB() as db:
|
||||||
db.add_one(doujinshi.id)
|
db.add_one(doujinshi.id)
|
||||||
|
|
||||||
if not options.is_nohtml and not options.is_cbz and not options.is_pdf:
|
if not options.is_nohtml and not options.is_cbz and not options.is_pdf:
|
||||||
generate_html(options.output_dir, doujinshi)
|
generate_html(options.output_dir, doujinshi, template=constant.CONFIG['template'])
|
||||||
elif options.is_cbz:
|
elif options.is_cbz:
|
||||||
generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
|
generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
|
||||||
elif options.is_pdf:
|
elif options.is_pdf:
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals, print_function
|
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
@ -26,12 +26,20 @@ u = urlparse(BASE_URL)
|
|||||||
IMAGE_URL = '%s://i.%s/galleries' % (u.scheme, u.hostname)
|
IMAGE_URL = '%s://i.%s/galleries' % (u.scheme, u.hostname)
|
||||||
|
|
||||||
NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai')
|
NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai')
|
||||||
NHENTAI_PROXY = os.path.join(NHENTAI_HOME, 'proxy')
|
|
||||||
NHENTAI_COOKIE = os.path.join(NHENTAI_HOME, 'cookie')
|
|
||||||
NHENTAI_LANGUAGE = os.path.join(NHENTAI_HOME, 'language')
|
|
||||||
NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
|
NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
|
||||||
|
NHENTAI_CONFIG_FILE = os.path.join(NHENTAI_HOME, 'config.json')
|
||||||
|
|
||||||
PROXY = {}
|
CONFIG = {
|
||||||
|
'proxy': {'http': '', 'https': ''},
|
||||||
|
'cookie': '',
|
||||||
|
'language': '',
|
||||||
|
'template': '',
|
||||||
|
'useragent': 'nhentai command line client (https://github.com/RicterZ/nhentai)'
|
||||||
|
}
|
||||||
|
|
||||||
COOKIE = ''
|
LANGUAGEISO ={
|
||||||
LANGUAGE = ''
|
'english' : 'en',
|
||||||
|
'chinese' : 'zh',
|
||||||
|
'japanese' : 'ja',
|
||||||
|
'translated' : 'translated'
|
||||||
|
}
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import print_function, unicode_literals
|
|
||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
from future.builtins import range
|
|
||||||
|
|
||||||
from nhentai.constant import DETAIL_URL, IMAGE_URL
|
from nhentai.constant import DETAIL_URL, IMAGE_URL
|
||||||
from nhentai.logger import logger
|
from nhentai.logger import logger
|
||||||
from nhentai.utils import format_filename
|
from nhentai.utils import format_filename
|
||||||
|
|
||||||
|
|
||||||
EXT_MAP = {
|
EXT_MAP = {
|
||||||
'j': 'jpg',
|
'j': 'jpg',
|
||||||
'p': 'png',
|
'p': 'png',
|
||||||
@ -27,8 +25,10 @@ class DoujinshiInfo(dict):
|
|||||||
|
|
||||||
|
|
||||||
class Doujinshi(object):
|
class Doujinshi(object):
|
||||||
def __init__(self, name=None, id=None, img_id=None, ext='', pages=0, name_format='[%i][%a][%t]', **kwargs):
|
def __init__(self, name=None, pretty_name=None, id=None, img_id=None,
|
||||||
|
ext='', pages=0, name_format='[%i][%a][%t]', **kwargs):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.pretty_name = pretty_name
|
||||||
self.id = id
|
self.id = id
|
||||||
self.img_id = img_id
|
self.img_id = img_id
|
||||||
self.ext = ext
|
self.ext = ext
|
||||||
@ -40,14 +40,12 @@ class Doujinshi(object):
|
|||||||
name_format = name_format.replace('%i', str(self.id))
|
name_format = name_format.replace('%i', str(self.id))
|
||||||
name_format = name_format.replace('%a', self.info.artists)
|
name_format = name_format.replace('%a', self.info.artists)
|
||||||
name_format = name_format.replace('%t', self.name)
|
name_format = name_format.replace('%t', self.name)
|
||||||
|
name_format = name_format.replace('%p', self.pretty_name)
|
||||||
name_format = name_format.replace('%s', self.info.subtitle)
|
name_format = name_format.replace('%s', self.info.subtitle)
|
||||||
self.filename = format_filename(name_format)
|
self.filename = format_filename(name_format)
|
||||||
|
|
||||||
def __repr__(self):
|
self.table = [
|
||||||
return '<Doujinshi: {0}>'.format(self.name)
|
["Parodies", self.info.parodies],
|
||||||
|
|
||||||
def show(self):
|
|
||||||
table = [
|
|
||||||
["Doujinshi", self.name],
|
["Doujinshi", self.name],
|
||||||
["Subtitle", self.info.subtitle],
|
["Subtitle", self.info.subtitle],
|
||||||
["Characters", self.info.characters],
|
["Characters", self.info.characters],
|
||||||
@ -57,26 +55,25 @@ class Doujinshi(object):
|
|||||||
["URL", self.url],
|
["URL", self.url],
|
||||||
["Pages", self.pages],
|
["Pages", self.pages],
|
||||||
]
|
]
|
||||||
logger.info(u'Print doujinshi information of {0}\n{1}'.format(self.id, tabulate(table)))
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<Doujinshi: {0}>'.format(self.name)
|
||||||
|
|
||||||
|
def show(self):
|
||||||
|
|
||||||
|
logger.info(u'Print doujinshi information of {0}\n{1}'.format(self.id, tabulate(self.table)))
|
||||||
|
|
||||||
def download(self):
|
def download(self):
|
||||||
logger.info('Starting to download doujinshi: %s' % self.name)
|
logger.info('Starting to download doujinshi: %s' % self.name)
|
||||||
if self.downloader:
|
if self.downloader:
|
||||||
download_queue = []
|
download_queue = []
|
||||||
|
|
||||||
if len(self.ext) != self.pages:
|
if len(self.ext) != self.pages:
|
||||||
logger.warning('Page count and ext count do not equal')
|
logger.warning('Page count and ext count do not equal')
|
||||||
|
|
||||||
for i in range(1, min(self.pages, len(self.ext)) + 1):
|
for i in range(1, min(self.pages, len(self.ext)) + 1):
|
||||||
download_queue.append('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i, self.ext[i-1]))
|
download_queue.append('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i, self.ext[i - 1]))
|
||||||
|
|
||||||
self.downloader.download(download_queue, self.filename)
|
self.downloader.download(download_queue, self.filename)
|
||||||
|
|
||||||
'''
|
|
||||||
for i in range(len(self.ext)):
|
|
||||||
download_queue.append('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i+1, EXT_MAP[self.ext[i]]))
|
|
||||||
'''
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.critical('Downloader has not been loaded')
|
logger.critical('Downloader has not been loaded')
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
# coding: utf-
|
# coding: utf-
|
||||||
from __future__ import unicode_literals, print_function
|
|
||||||
|
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import signal
|
import signal
|
||||||
@ -15,6 +14,7 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
|
|
||||||
|
from nhentai import constant
|
||||||
from nhentai.logger import logger
|
from nhentai.logger import logger
|
||||||
from nhentai.parser import request
|
from nhentai.parser import request
|
||||||
from nhentai.utils import Singleton
|
from nhentai.utils import Singleton
|
||||||
@ -35,7 +35,7 @@ class Downloader(Singleton):
|
|||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.delay = delay
|
self.delay = delay
|
||||||
|
|
||||||
def download_(self, url, folder='', filename='', retried=0):
|
def download_(self, url, folder='', filename='', retried=0, proxy=None):
|
||||||
if self.delay:
|
if self.delay:
|
||||||
time.sleep(self.delay)
|
time.sleep(self.delay)
|
||||||
logger.info('Starting to download {0} ...'.format(url))
|
logger.info('Starting to download {0} ...'.format(url))
|
||||||
@ -52,7 +52,7 @@ class Downloader(Singleton):
|
|||||||
i = 0
|
i = 0
|
||||||
while i < 10:
|
while i < 10:
|
||||||
try:
|
try:
|
||||||
response = request('get', url, stream=True, timeout=self.timeout)
|
response = request('get', url, stream=True, timeout=self.timeout, proxies=proxy)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise NHentaiImageNotExistException
|
raise NHentaiImageNotExistException
|
||||||
|
|
||||||
@ -78,7 +78,8 @@ class Downloader(Singleton):
|
|||||||
except (requests.HTTPError, requests.Timeout) as e:
|
except (requests.HTTPError, requests.Timeout) as e:
|
||||||
if retried < 3:
|
if retried < 3:
|
||||||
logger.warning('Warning: {0}, retrying({1}) ...'.format(str(e), retried))
|
logger.warning('Warning: {0}, retrying({1}) ...'.format(str(e), retried))
|
||||||
return 0, self.download_(url=url, folder=folder, filename=filename, retried=retried+1)
|
return 0, self.download_(url=url, folder=folder, filename=filename,
|
||||||
|
retried=retried+1, proxy=proxy)
|
||||||
else:
|
else:
|
||||||
return 0, None
|
return 0, None
|
||||||
|
|
||||||
@ -120,16 +121,16 @@ class Downloader(Singleton):
|
|||||||
folder = os.path.join(self.path, folder)
|
folder = os.path.join(self.path, folder)
|
||||||
|
|
||||||
if not os.path.exists(folder):
|
if not os.path.exists(folder):
|
||||||
logger.warn('Path \'{0}\' does not exist, creating.'.format(folder))
|
logger.warning('Path \'{0}\' does not exist, creating.'.format(folder))
|
||||||
try:
|
try:
|
||||||
os.makedirs(folder)
|
os.makedirs(folder)
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
logger.critical('{0}'.format(str(e)))
|
logger.critical('{0}'.format(str(e)))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.warn('Path \'{0}\' already exist.'.format(folder))
|
logger.warning('Path \'{0}\' already exist.'.format(folder))
|
||||||
|
|
||||||
queue = [(self, url, folder) for url in queue]
|
queue = [(self, url, folder, constant.CONFIG['proxy']) for url in queue]
|
||||||
|
|
||||||
pool = multiprocessing.Pool(self.size, init_worker)
|
pool = multiprocessing.Pool(self.size, init_worker)
|
||||||
[pool.apply_async(download_wrapper, args=item) for item in queue]
|
[pool.apply_async(download_wrapper, args=item) for item in queue]
|
||||||
@ -138,9 +139,9 @@ class Downloader(Singleton):
|
|||||||
pool.join()
|
pool.join()
|
||||||
|
|
||||||
|
|
||||||
def download_wrapper(obj, url, folder=''):
|
def download_wrapper(obj, url, folder='', proxy=None):
|
||||||
if sys.platform == 'darwin' or semaphore.get_value():
|
if sys.platform == 'darwin' or semaphore.get_value():
|
||||||
return Downloader.download_(obj, url=url, folder=folder)
|
return Downloader.download_(obj, url=url, folder=folder, proxy=proxy)
|
||||||
else:
|
else:
|
||||||
return -3, None
|
return -3, None
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#
|
#
|
||||||
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
|
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
|
||||||
#
|
#
|
||||||
from __future__ import print_function, unicode_literals
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import platform
|
import platform
|
||||||
@ -174,7 +173,7 @@ logger.setLevel(logging.DEBUG)
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
logger.log(15, 'nhentai')
|
logger.log(15, 'nhentai')
|
||||||
logger.info('info')
|
logger.info('info')
|
||||||
logger.warn('warn')
|
logger.warning('warning')
|
||||||
logger.debug('debug')
|
logger.debug('debug')
|
||||||
logger.error('error')
|
logger.error('error')
|
||||||
logger.critical('critical')
|
logger.critical('critical')
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals, print_function
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
@ -64,7 +62,7 @@ def _get_title_and_id(response):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def favorites_parser(page_range=''):
|
def favorites_parser(page=None):
|
||||||
result = []
|
result = []
|
||||||
html = BeautifulSoup(request('get', constant.FAV_URL).content, 'html.parser')
|
html = BeautifulSoup(request('get', constant.FAV_URL).content, 'html.parser')
|
||||||
count = html.find('span', attrs={'class': 'count'})
|
count = html.find('span', attrs={'class': 'count'})
|
||||||
@ -78,20 +76,20 @@ def favorites_parser(page_range=''):
|
|||||||
return []
|
return []
|
||||||
pages = int(count / 25)
|
pages = int(count / 25)
|
||||||
|
|
||||||
if pages:
|
if page:
|
||||||
pages += 1 if count % (25 * pages) else 0
|
page_range_list = page
|
||||||
else:
|
else:
|
||||||
pages = 1
|
if pages:
|
||||||
|
pages += 1 if count % (25 * pages) else 0
|
||||||
|
else:
|
||||||
|
pages = 1
|
||||||
|
|
||||||
logger.info('You have %d favorites in %d pages.' % (count, pages))
|
logger.info('You have %d favorites in %d pages.' % (count, pages))
|
||||||
|
|
||||||
if os.getenv('DEBUG'):
|
if os.getenv('DEBUG'):
|
||||||
pages = 1
|
pages = 1
|
||||||
|
|
||||||
page_range_list = range(1, pages + 1)
|
page_range_list = range(1, pages + 1)
|
||||||
if page_range:
|
|
||||||
logger.info('page range is {0}'.format(page_range))
|
|
||||||
page_range_list = page_range_parser(page_range, pages)
|
|
||||||
|
|
||||||
for page in page_range_list:
|
for page in page_range_list:
|
||||||
try:
|
try:
|
||||||
@ -105,32 +103,6 @@ def favorites_parser(page_range=''):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def page_range_parser(page_range, max_page_num):
|
|
||||||
pages = set()
|
|
||||||
ranges = str.split(page_range, ',')
|
|
||||||
for range_str in ranges:
|
|
||||||
idx = range_str.find('-')
|
|
||||||
if idx == -1:
|
|
||||||
try:
|
|
||||||
page = int(range_str)
|
|
||||||
if page <= max_page_num:
|
|
||||||
pages.add(page)
|
|
||||||
except ValueError:
|
|
||||||
logger.error('page range({0}) is not valid'.format(page_range))
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
left = int(range_str[:idx])
|
|
||||||
right = int(range_str[idx + 1:])
|
|
||||||
if right > max_page_num:
|
|
||||||
right = max_page_num
|
|
||||||
for page in range(left, right + 1):
|
|
||||||
pages.add(page)
|
|
||||||
except ValueError:
|
|
||||||
logger.error('page range({0}) is not valid'.format(page_range))
|
|
||||||
|
|
||||||
return list(pages)
|
|
||||||
|
|
||||||
|
|
||||||
def doujinshi_parser(id_):
|
def doujinshi_parser(id_):
|
||||||
if not isinstance(id_, (int,)) and (isinstance(id_, (str,)) and not id_.isdigit()):
|
if not isinstance(id_, (int,)) and (isinstance(id_, (str,)) and not id_.isdigit()):
|
||||||
raise Exception('Doujinshi id({0}) is not valid'.format(id_))
|
raise Exception('Doujinshi id({0}) is not valid'.format(id_))
|
||||||
@ -143,28 +115,34 @@ def doujinshi_parser(id_):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
response = request('get', url)
|
response = request('get', url)
|
||||||
if response.status_code in (200,):
|
if response.status_code in (200, ):
|
||||||
response = response.content
|
response = response.content
|
||||||
|
elif response.status_code in (404,):
|
||||||
|
logger.error("Doujinshi with id {0} cannot be found".format(id_))
|
||||||
|
return []
|
||||||
else:
|
else:
|
||||||
logger.debug('Slow down and retry ({}) ...'.format(id_))
|
logger.debug('Slow down and retry ({}) ...'.format(id_))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
return doujinshi_parser(str(id_))
|
return doujinshi_parser(str(id_))
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warn('Error: {}, ignored'.format(str(e)))
|
logger.warning('Error: {}, ignored'.format(str(e)))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
html = BeautifulSoup(response, 'html.parser')
|
html = BeautifulSoup(response, 'html.parser')
|
||||||
doujinshi_info = html.find('div', attrs={'id': 'info'})
|
doujinshi_info = html.find('div', attrs={'id': 'info'})
|
||||||
|
|
||||||
title = doujinshi_info.find('h1').text
|
title = doujinshi_info.find('h1').text
|
||||||
|
pretty_name = doujinshi_info.find('h1').find('span', attrs={'class': 'pretty'}).text
|
||||||
subtitle = doujinshi_info.find('h2')
|
subtitle = doujinshi_info.find('h2')
|
||||||
|
|
||||||
doujinshi['name'] = title
|
doujinshi['name'] = title
|
||||||
|
doujinshi['pretty_name'] = pretty_name
|
||||||
doujinshi['subtitle'] = subtitle.text if subtitle else ''
|
doujinshi['subtitle'] = subtitle.text if subtitle else ''
|
||||||
|
|
||||||
doujinshi_cover = html.find('div', attrs={'id': 'cover'})
|
doujinshi_cover = html.find('div', attrs={'id': 'cover'})
|
||||||
img_id = re.search('/galleries/([\d]+)/cover\.(jpg|png|gif)$', doujinshi_cover.a.img.attrs['data-src'])
|
img_id = re.search('/galleries/([0-9]+)/cover.(jpg|png|gif)$',
|
||||||
|
doujinshi_cover.a.img.attrs['data-src'])
|
||||||
|
|
||||||
ext = []
|
ext = []
|
||||||
for i in html.find_all('div', attrs={'class': 'thumb-container'}):
|
for i in html.find_all('div', attrs={'class': 'thumb-container'}):
|
||||||
@ -205,7 +183,7 @@ def old_search_parser(keyword, sorting='date', page=1):
|
|||||||
|
|
||||||
result = _get_title_and_id(response)
|
result = _get_title_and_id(response)
|
||||||
if not result:
|
if not result:
|
||||||
logger.warn('Not found anything of keyword {}'.format(keyword))
|
logger.warning('Not found anything of keyword {}'.format(keyword))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -215,34 +193,47 @@ def print_doujinshi(doujinshi_list):
|
|||||||
return
|
return
|
||||||
doujinshi_list = [(i['id'], i['title']) for i in doujinshi_list]
|
doujinshi_list = [(i['id'], i['title']) for i in doujinshi_list]
|
||||||
headers = ['id', 'doujinshi']
|
headers = ['id', 'doujinshi']
|
||||||
logger.info('Search Result\n' +
|
logger.info('Search Result || Found %i doujinshis \n' % doujinshi_list.__len__() +
|
||||||
tabulate(tabular_data=doujinshi_list, headers=headers, tablefmt='rst'))
|
tabulate(tabular_data=doujinshi_list, headers=headers, tablefmt='rst'))
|
||||||
|
|
||||||
|
|
||||||
def search_parser(keyword, sorting, page):
|
def search_parser(keyword, sorting, page, is_page_all=False):
|
||||||
logger.debug('Searching doujinshis using keywords {0}'.format(keyword))
|
|
||||||
# keyword = '+'.join([i.strip().replace(' ', '-').lower() for i in keyword.split(',')])
|
# keyword = '+'.join([i.strip().replace(' ', '-').lower() for i in keyword.split(',')])
|
||||||
result = []
|
result = []
|
||||||
i = 0
|
if not page:
|
||||||
while i < 5:
|
page = [1]
|
||||||
try:
|
|
||||||
url = request('get', url=constant.SEARCH_URL, params={'query': keyword, 'page': page, 'sort': sorting}).url
|
|
||||||
response = request('get', url.replace('%2B', '+')).json()
|
|
||||||
except Exception as e:
|
|
||||||
logger.critical(str(e))
|
|
||||||
|
|
||||||
break
|
if is_page_all:
|
||||||
|
url = request('get', url=constant.SEARCH_URL, params={'query': keyword}).url
|
||||||
|
init_response = request('get', url.replace('%2B', '+')).json()
|
||||||
|
page = range(1, init_response['num_pages']+1)
|
||||||
|
|
||||||
if 'result' not in response:
|
total = '/{0}'.format(page[-1]) if is_page_all else ''
|
||||||
raise Exception('No result in response')
|
for p in page:
|
||||||
|
i = 0
|
||||||
|
|
||||||
for row in response['result']:
|
logger.info('Searching doujinshis using keywords "{0}" on page {1}{2}'.format(keyword, p, total))
|
||||||
title = row['title']['english']
|
while i < 3:
|
||||||
title = title[:85] + '..' if len(title) > 85 else title
|
try:
|
||||||
result.append({'id': row['id'], 'title': title})
|
url = request('get', url=constant.SEARCH_URL, params={'query': keyword,
|
||||||
|
'page': p, 'sort': sorting}).url
|
||||||
|
response = request('get', url.replace('%2B', '+')).json()
|
||||||
|
except Exception as e:
|
||||||
|
logger.critical(str(e))
|
||||||
|
|
||||||
if not result:
|
break
|
||||||
logger.warn('No results for keywords {}'.format(keyword))
|
|
||||||
|
if 'result' not in response:
|
||||||
|
logger.warning('No result in response in page {}'.format(p))
|
||||||
|
break
|
||||||
|
|
||||||
|
for row in response['result']:
|
||||||
|
title = row['title']['english']
|
||||||
|
title = title[:85] + '..' if len(title) > 85 else title
|
||||||
|
result.append({'id': row['id'], 'title': title})
|
||||||
|
|
||||||
|
if not result:
|
||||||
|
logger.warning('No results for keywords {}'.format(keyword))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from xml.sax.saxutils import escape
|
from xml.sax.saxutils import escape
|
||||||
|
from nhentai.constant import LANGUAGEISO
|
||||||
|
|
||||||
|
|
||||||
def serialize_json(doujinshi, dir):
|
def serialize_json(doujinshi, dir):
|
||||||
@ -26,12 +27,12 @@ def serialize_json(doujinshi, dir):
|
|||||||
metadata['Pages'] = doujinshi.pages
|
metadata['Pages'] = doujinshi.pages
|
||||||
|
|
||||||
with open(os.path.join(dir, 'metadata.json'), 'w') as f:
|
with open(os.path.join(dir, 'metadata.json'), 'w') as f:
|
||||||
json.dump(metadata, f, separators=','':')
|
json.dump(metadata, f, separators=(',', ':'))
|
||||||
|
|
||||||
|
|
||||||
def serialize_comicxml(doujinshi, dir):
|
def serialize_comic_xml(doujinshi, dir):
|
||||||
from iso8601 import parse_date
|
from iso8601 import parse_date
|
||||||
with open(os.path.join(dir, 'ComicInfo.xml'), 'w') as f:
|
with open(os.path.join(dir, 'ComicInfo.xml'), 'w', encoding="utf-8") as f:
|
||||||
f.write('<?xml version="1.0" encoding="utf-8"?>\n')
|
f.write('<?xml version="1.0" encoding="utf-8"?>\n')
|
||||||
f.write('<ComicInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
|
f.write('<ComicInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
|
||||||
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n')
|
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n')
|
||||||
@ -45,7 +46,8 @@ def serialize_comicxml(doujinshi, dir):
|
|||||||
xml_write_simple_tag(f, 'NhentaiId', doujinshi.id)
|
xml_write_simple_tag(f, 'NhentaiId', doujinshi.id)
|
||||||
xml_write_simple_tag(f, 'Genre', doujinshi.info.categories)
|
xml_write_simple_tag(f, 'Genre', doujinshi.info.categories)
|
||||||
|
|
||||||
xml_write_simple_tag(f, 'BlackAndWhite', 'No' if doujinshi.info.tags and 'full color' in doujinshi.info.tags else 'Yes')
|
xml_write_simple_tag(f, 'BlackAndWhite', 'No' if doujinshi.info.tags and
|
||||||
|
'full color' in doujinshi.info.tags else 'Yes')
|
||||||
|
|
||||||
if doujinshi.info.date:
|
if doujinshi.info.date:
|
||||||
dt = parse_date(doujinshi.info.date)
|
dt = parse_date(doujinshi.info.date)
|
||||||
@ -59,13 +61,14 @@ def serialize_comicxml(doujinshi, dir):
|
|||||||
if doujinshi.info.tags:
|
if doujinshi.info.tags:
|
||||||
xml_write_simple_tag(f, 'Tags', doujinshi.info.tags)
|
xml_write_simple_tag(f, 'Tags', doujinshi.info.tags)
|
||||||
if doujinshi.info.artists:
|
if doujinshi.info.artists:
|
||||||
xml_write_simple_tag(f, 'Writer', ' & '.join([i.strip() for i in doujinshi.info.artists.split(',')]))
|
xml_write_simple_tag(f, 'Writer', ' & '.join([i.strip() for i in
|
||||||
# if doujinshi.info.groups:
|
doujinshi.info.artists.split(',')]))
|
||||||
# metadata['group'] = [i.strip() for i in doujinshi.info.groups.split(',')]
|
|
||||||
if doujinshi.info.languages:
|
if doujinshi.info.languages:
|
||||||
languages = [i.strip() for i in doujinshi.info.languages.split(',')]
|
languages = [i.strip() for i in doujinshi.info.languages.split(',')]
|
||||||
xml_write_simple_tag(f, 'Translated', 'Yes' if 'translated' in languages else 'No')
|
xml_write_simple_tag(f, 'Translated', 'Yes' if 'translated' in languages else 'No')
|
||||||
[xml_write_simple_tag(f, 'Language', i) for i in languages if i != 'translated']
|
[xml_write_simple_tag(f, 'LanguageISO', LANGUAGEISO[i]) for i in languages
|
||||||
|
if (i != 'translated' and i in LANGUAGEISO)]
|
||||||
|
|
||||||
f.write('</ComicInfo>')
|
f.write('</ComicInfo>')
|
||||||
|
|
||||||
@ -120,7 +123,7 @@ def serialize_unique(lst):
|
|||||||
def set_js_database():
|
def set_js_database():
|
||||||
with open('data.js', 'w') as f:
|
with open('data.js', 'w') as f:
|
||||||
indexed_json = merge_json()
|
indexed_json = merge_json()
|
||||||
unique_json = json.dumps(serialize_unique(indexed_json), separators=','':')
|
unique_json = json.dumps(serialize_unique(indexed_json), separators=(',', ':'))
|
||||||
indexed_json = json.dumps(indexed_json, separators=','':')
|
indexed_json = json.dumps(indexed_json, separators=(',', ':'))
|
||||||
f.write('var data = ' + indexed_json)
|
f.write('var data = ' + indexed_json)
|
||||||
f.write(';\nvar tags = ' + unique_json)
|
f.write(';\nvar tags = ' + unique_json)
|
||||||
|
178
nhentai/utils.py
178
nhentai/utils.py
@ -1,10 +1,8 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals, print_function
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import string
|
|
||||||
import zipfile
|
import zipfile
|
||||||
import shutil
|
import shutil
|
||||||
import requests
|
import requests
|
||||||
@ -12,24 +10,32 @@ import sqlite3
|
|||||||
|
|
||||||
from nhentai import constant
|
from nhentai import constant
|
||||||
from nhentai.logger import logger
|
from nhentai.logger import logger
|
||||||
from nhentai.serializer import serialize_json, serialize_comicxml, set_js_database
|
from nhentai.serializer import serialize_json, serialize_comic_xml, set_js_database
|
||||||
|
|
||||||
|
|
||||||
def request(method, url, **kwargs):
|
def request(method, url, **kwargs):
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
session.headers.update({
|
session.headers.update({
|
||||||
'Referer': constant.LOGIN_URL,
|
'Referer': constant.LOGIN_URL,
|
||||||
'User-Agent': 'nhentai command line client (https://github.com/RicterZ/nhentai)',
|
'User-Agent': constant.CONFIG['useragent'],
|
||||||
'Cookie': constant.COOKIE
|
'Cookie': constant.CONFIG['cookie']
|
||||||
})
|
})
|
||||||
return getattr(session, method)(url, proxies=constant.PROXY, verify=False, **kwargs)
|
|
||||||
|
if not kwargs.get('proxies', None):
|
||||||
|
kwargs['proxies'] = constant.CONFIG['proxy']
|
||||||
|
|
||||||
|
return getattr(session, method)(url, verify=False, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def check_cookie():
|
def check_cookie():
|
||||||
response = request('get', constant.BASE_URL).text
|
response = request('get', constant.BASE_URL)
|
||||||
username = re.findall('"/users/\d+/(.*?)"', response)
|
if response.status_code == 503 and 'cf-browser-verification' in response.text:
|
||||||
|
logger.error('Blocked by Cloudflare captcha, please set your cookie and useragent')
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
username = re.findall('"/users/\d+/(.*?)"', response.text)
|
||||||
if not username:
|
if not username:
|
||||||
logger.error('Cannot get your username, please check your cookie or use `nhentai --cookie` to set your cookie')
|
logger.warning('Cannot get your username, please check your cookie or use `nhentai --cookie` to set your cookie')
|
||||||
else:
|
else:
|
||||||
logger.info('Login successfully! Your username: {}'.format(username[0]))
|
logger.info('Login successfully! Your username: {}'.format(username[0]))
|
||||||
|
|
||||||
@ -64,7 +70,7 @@ def readfile(path):
|
|||||||
return file.read()
|
return file.read()
|
||||||
|
|
||||||
|
|
||||||
def generate_html(output_dir='.', doujinshi_obj=None):
|
def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
|
||||||
image_html = ''
|
image_html = ''
|
||||||
|
|
||||||
if doujinshi_obj is not None:
|
if doujinshi_obj is not None:
|
||||||
@ -72,6 +78,13 @@ def generate_html(output_dir='.', doujinshi_obj=None):
|
|||||||
else:
|
else:
|
||||||
doujinshi_dir = '.'
|
doujinshi_dir = '.'
|
||||||
|
|
||||||
|
if not os.path.exists(doujinshi_dir):
|
||||||
|
logger.warning('Path \'{0}\' does not exist, creating.'.format(doujinshi_dir))
|
||||||
|
try:
|
||||||
|
os.makedirs(doujinshi_dir)
|
||||||
|
except EnvironmentError as e:
|
||||||
|
logger.critical('{0}'.format(str(e)))
|
||||||
|
|
||||||
file_list = os.listdir(doujinshi_dir)
|
file_list = os.listdir(doujinshi_dir)
|
||||||
file_list.sort()
|
file_list.sort()
|
||||||
|
|
||||||
@ -79,11 +92,11 @@ def generate_html(output_dir='.', doujinshi_obj=None):
|
|||||||
if not os.path.splitext(image)[1] in ('.jpg', '.png'):
|
if not os.path.splitext(image)[1] in ('.jpg', '.png'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
image_html += '<img src="{0}" class="image-item"/>\n'\
|
image_html += '<img src="{0}" class="image-item"/>\n' \
|
||||||
.format(image)
|
.format(image)
|
||||||
html = readfile('viewer/index.html')
|
html = readfile('viewer/{}/index.html'.format(template))
|
||||||
css = readfile('viewer/styles.css')
|
css = readfile('viewer/{}/styles.css'.format(template))
|
||||||
js = readfile('viewer/scripts.js')
|
js = readfile('viewer/{}/scripts.js'.format(template))
|
||||||
|
|
||||||
if doujinshi_obj is not None:
|
if doujinshi_obj is not None:
|
||||||
serialize_json(doujinshi_obj, doujinshi_dir)
|
serialize_json(doujinshi_obj, doujinshi_dir)
|
||||||
@ -160,7 +173,7 @@ def generate_main_html(output_dir='./'):
|
|||||||
else:
|
else:
|
||||||
with open('./main.html', 'wb') as f:
|
with open('./main.html', 'wb') as f:
|
||||||
f.write(data.encode('utf-8'))
|
f.write(data.encode('utf-8'))
|
||||||
shutil.copy(os.path.dirname(__file__)+'/viewer/logo.png', './')
|
shutil.copy(os.path.dirname(__file__) + '/viewer/logo.png', './')
|
||||||
set_js_database()
|
set_js_database()
|
||||||
logger.log(
|
logger.log(
|
||||||
15, 'Main Viewer has been written to \'{0}main.html\''.format(output_dir))
|
15, 'Main Viewer has been written to \'{0}main.html\''.format(output_dir))
|
||||||
@ -168,11 +181,11 @@ def generate_main_html(output_dir='./'):
|
|||||||
logger.warning('Writing Main Viewer failed ({})'.format(str(e)))
|
logger.warning('Writing Main Viewer failed ({})'.format(str(e)))
|
||||||
|
|
||||||
|
|
||||||
def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=False):
|
def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=True):
|
||||||
if doujinshi_obj is not None:
|
if doujinshi_obj is not None:
|
||||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
||||||
if write_comic_info:
|
if write_comic_info:
|
||||||
serialize_comicxml(doujinshi_obj, doujinshi_dir)
|
serialize_comic_xml(doujinshi_obj, doujinshi_dir)
|
||||||
cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), '{}.cbz'.format(doujinshi_obj.filename))
|
cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), '{}.cbz'.format(doujinshi_obj.filename))
|
||||||
else:
|
else:
|
||||||
cbz_filename = './doujinshi.cbz'
|
cbz_filename = './doujinshi.cbz'
|
||||||
@ -196,52 +209,61 @@ def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_
|
|||||||
def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
|
def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
|
||||||
try:
|
try:
|
||||||
import img2pdf
|
import img2pdf
|
||||||
|
|
||||||
|
"""Write images to a PDF file using img2pdf."""
|
||||||
|
if doujinshi_obj is not None:
|
||||||
|
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
||||||
|
pdf_filename = os.path.join(
|
||||||
|
os.path.join(doujinshi_dir, '..'),
|
||||||
|
'{}.pdf'.format(doujinshi_obj.filename)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
pdf_filename = './doujinshi.pdf'
|
||||||
|
doujinshi_dir = '.'
|
||||||
|
|
||||||
|
file_list = os.listdir(doujinshi_dir)
|
||||||
|
file_list.sort()
|
||||||
|
|
||||||
|
logger.info('Writing PDF file to path: {}'.format(pdf_filename))
|
||||||
|
with open(pdf_filename, 'wb') as pdf_f:
|
||||||
|
full_path_list = (
|
||||||
|
[os.path.join(doujinshi_dir, image) for image in file_list]
|
||||||
|
)
|
||||||
|
pdf_f.write(img2pdf.convert(full_path_list))
|
||||||
|
|
||||||
|
if rm_origin_dir:
|
||||||
|
shutil.rmtree(doujinshi_dir, ignore_errors=True)
|
||||||
|
|
||||||
|
logger.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir))
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
logger.error("Please install img2pdf package by using pip.")
|
logger.error("Please install img2pdf package by using pip.")
|
||||||
|
|
||||||
"""Write images to a PDF file using img2pdf."""
|
|
||||||
if doujinshi_obj is not None:
|
|
||||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
|
||||||
pdf_filename = os.path.join(
|
|
||||||
os.path.join(doujinshi_dir, '..'),
|
|
||||||
'{}.pdf'.format(doujinshi_obj.filename)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
pdf_filename = './doujinshi.pdf'
|
|
||||||
doujinshi_dir = '.'
|
|
||||||
|
|
||||||
file_list = os.listdir(doujinshi_dir)
|
def unicode_truncate(s, length, encoding='utf-8'):
|
||||||
file_list.sort()
|
"""https://stackoverflow.com/questions/1809531/truncating-unicode-so-it-fits-a-maximum-size-when-encoded-for-wire-transfer
|
||||||
|
"""
|
||||||
logger.info('Writing PDF file to path: {}'.format(pdf_filename))
|
encoded = s.encode(encoding)[:length]
|
||||||
with open(pdf_filename, 'wb') as pdf_f:
|
return encoded.decode(encoding, 'ignore')
|
||||||
full_path_list = (
|
|
||||||
[os.path.join(doujinshi_dir, image) for image in file_list]
|
|
||||||
)
|
|
||||||
pdf_f.write(img2pdf.convert(full_path_list))
|
|
||||||
|
|
||||||
if rm_origin_dir:
|
|
||||||
shutil.rmtree(doujinshi_dir, ignore_errors=True)
|
|
||||||
|
|
||||||
logger.log(15, 'PDF file has been written 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.
|
"""
|
||||||
Uses a whitelist approach: any characters not present in valid_chars are
|
It used to be a whitelist approach allowed only alphabet and a part of symbols.
|
||||||
removed. Also spaces are replaced with underscores.
|
but most doujinshi's names include Japanese 2-byte characters and these was rejected.
|
||||||
|
so it is using blacklist approach now.
|
||||||
Note: this method may produce invalid filenames such as ``, `.` or `..`
|
if filename include forbidden characters (\'/:,;*?"<>|) ,it replace space character(' ').
|
||||||
When I use this method I prepend a date string like '2009_01_15_19_46_32_'
|
"""
|
||||||
and append a file extension like '.txt', so I avoid the potential of using
|
|
||||||
an invalid filename.
|
|
||||||
|
|
||||||
"""
|
|
||||||
# maybe you can use `--format` to select a suitable filename
|
# maybe you can use `--format` to select a suitable filename
|
||||||
valid_chars = "-_.()[] %s%s" % (string.ascii_letters, string.digits)
|
ban_chars = '\\\'/:,;*?"<>|\t'
|
||||||
filename = ''.join(c for c in s if c in valid_chars)
|
filename = s.translate(str.maketrans(ban_chars, ' ' * len(ban_chars))).strip()
|
||||||
|
filename = ' '.join(filename.split())
|
||||||
|
|
||||||
|
while filename.endswith('.'):
|
||||||
|
filename = filename[:-1]
|
||||||
|
|
||||||
if len(filename) > 100:
|
if len(filename) > 100:
|
||||||
filename = filename[:100] + '...]'
|
filename = filename[:100] + u'…'
|
||||||
|
|
||||||
# Remove [] from filename
|
# Remove [] from filename
|
||||||
filename = filename.replace('[]', '').strip()
|
filename = filename.replace('[]', '').strip()
|
||||||
@ -253,6 +275,54 @@ def signal_handler(signal, frame):
|
|||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def paging(page_string):
|
||||||
|
# 1,3-5,14 -> [1, 3, 4, 5, 14]
|
||||||
|
if not page_string:
|
||||||
|
return []
|
||||||
|
|
||||||
|
page_list = []
|
||||||
|
for i in page_string.split(','):
|
||||||
|
if '-' in i:
|
||||||
|
start, end = i.split('-')
|
||||||
|
if not (start.isdigit() and end.isdigit()):
|
||||||
|
raise Exception('Invalid page number')
|
||||||
|
page_list.extend(list(range(int(start), int(end) + 1)))
|
||||||
|
else:
|
||||||
|
if not i.isdigit():
|
||||||
|
raise Exception('Invalid page number')
|
||||||
|
page_list.append(int(i))
|
||||||
|
|
||||||
|
return page_list
|
||||||
|
|
||||||
|
|
||||||
|
def generate_metadata_file(output_dir, table, doujinshi_obj=None):
|
||||||
|
logger.info('Writing Metadata Info')
|
||||||
|
|
||||||
|
if doujinshi_obj is not None:
|
||||||
|
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
||||||
|
else:
|
||||||
|
doujinshi_dir = '.'
|
||||||
|
|
||||||
|
logger.info(doujinshi_dir)
|
||||||
|
|
||||||
|
f = open(os.path.join(doujinshi_dir, 'info.txt'), 'w', encoding='utf-8')
|
||||||
|
|
||||||
|
fields = ['TITLE', 'ORIGINAL TITLE', 'AUTHOR', 'ARTIST', 'CIRCLE', 'SCANLATOR',
|
||||||
|
'TRANSLATOR', 'PUBLISHER', 'DESCRIPTION', 'STATUS', 'CHAPTERS', 'PAGES',
|
||||||
|
'TAGS', 'TYPE', 'LANGUAGE', 'RELEASED', 'READING DIRECTION', 'CHARACTERS',
|
||||||
|
'SERIES', 'PARODY', 'URL']
|
||||||
|
special_fields = ['PARODY', 'TITLE', 'ORIGINAL TITLE', 'CHARACTERS', 'AUTHOR',
|
||||||
|
'LANGUAGE', 'TAGS', 'URL', 'PAGES']
|
||||||
|
|
||||||
|
for i in range(len(fields)):
|
||||||
|
f.write('{}: '.format(fields[i]))
|
||||||
|
if fields[i] in special_fields:
|
||||||
|
f.write(str(table[special_fields.index(fields[i])][1]))
|
||||||
|
f.write('\n')
|
||||||
|
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
class DB(object):
|
class DB(object):
|
||||||
conn = None
|
conn = None
|
||||||
cur = None
|
cur = None
|
||||||
|
25
nhentai/viewer/minimal/index.html
Normal file
25
nhentai/viewer/minimal/index.html
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes, viewport-fit=cover" />
|
||||||
|
<title>{TITLE}</title>
|
||||||
|
<style>
|
||||||
|
{STYLES}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<nav id="list" hidden=true>
|
||||||
|
{IMAGES}</nav>
|
||||||
|
|
||||||
|
<div id="image-container">
|
||||||
|
<div id="dest"></div>
|
||||||
|
<span id="page-num"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
{SCRIPTS}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
79
nhentai/viewer/minimal/scripts.js
Normal file
79
nhentai/viewer/minimal/scripts.js
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
const pages = Array.from(document.querySelectorAll('img.image-item'));
|
||||||
|
let currentPage = 0;
|
||||||
|
|
||||||
|
function changePage(pageNum) {
|
||||||
|
const previous = pages[currentPage];
|
||||||
|
const current = pages[pageNum];
|
||||||
|
|
||||||
|
if (current == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
previous.classList.remove('current');
|
||||||
|
current.classList.add('current');
|
||||||
|
|
||||||
|
currentPage = pageNum;
|
||||||
|
|
||||||
|
const display = document.getElementById('dest');
|
||||||
|
display.style.backgroundImage = `url("${current.src}")`;
|
||||||
|
|
||||||
|
scroll(0,0)
|
||||||
|
|
||||||
|
document.getElementById('page-num')
|
||||||
|
.innerText = [
|
||||||
|
(pageNum + 1).toLocaleString(),
|
||||||
|
pages.length.toLocaleString()
|
||||||
|
].join('\u200a/\u200a');
|
||||||
|
}
|
||||||
|
|
||||||
|
changePage(0);
|
||||||
|
|
||||||
|
document.getElementById('image-container').onclick = event => {
|
||||||
|
const width = document.getElementById('image-container').clientWidth;
|
||||||
|
const clickPos = event.clientX / width;
|
||||||
|
|
||||||
|
if (clickPos < 0.5) {
|
||||||
|
changePage(currentPage - 1);
|
||||||
|
} else {
|
||||||
|
changePage(currentPage + 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
document.onkeypress = event => {
|
||||||
|
switch (event.key.toLowerCase()) {
|
||||||
|
// Previous Image
|
||||||
|
case 'w':
|
||||||
|
scrollBy(0, -40);
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
changePage(currentPage - 1);
|
||||||
|
break;
|
||||||
|
// Return to previous page
|
||||||
|
case 'q':
|
||||||
|
window.history.go(-1);
|
||||||
|
break;
|
||||||
|
// Next Image
|
||||||
|
case ' ':
|
||||||
|
case 's':
|
||||||
|
scrollBy(0, 40);
|
||||||
|
break;
|
||||||
|
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
|
||||||
|
break;
|
||||||
|
case 39: //right
|
||||||
|
changePage(currentPage + 1);
|
||||||
|
break;
|
||||||
|
case 40: //down
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
75
nhentai/viewer/minimal/styles.css
Normal file
75
nhentai/viewer/minimal/styles.css
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
|
||||||
|
*, *::after, *::before {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
display: flex;
|
||||||
|
background-color: #e8e6e6;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
font-family: sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
#list {
|
||||||
|
height: 2000px;
|
||||||
|
overflow: scroll;
|
||||||
|
width: 260px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
#list img {
|
||||||
|
width: 200px;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 10px;
|
||||||
|
margin: 15px 0;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
#list img.current {
|
||||||
|
background: #0003;
|
||||||
|
}
|
||||||
|
|
||||||
|
#image-container {
|
||||||
|
flex: auto;
|
||||||
|
height: 100%;
|
||||||
|
background: rgb(0, 0, 0);
|
||||||
|
color: rgb(100, 100, 100);
|
||||||
|
text-align: center;
|
||||||
|
cursor: pointer;
|
||||||
|
-webkit-user-select: none;
|
||||||
|
user-select: none;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
#image-container #dest {
|
||||||
|
height: 2000px;
|
||||||
|
width: 100%;
|
||||||
|
background-size: contain;
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
background-position: top;
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 100vh;
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
#image-container #page-num {
|
||||||
|
position: static;
|
||||||
|
font-size: 9pt;
|
||||||
|
left: 10px;
|
||||||
|
bottom: 5px;
|
||||||
|
font-weight: bold;
|
||||||
|
opacity: 0.9;
|
||||||
|
text-shadow: /* Duplicate the same shadow to make it very strong */
|
||||||
|
0 0 2px #222,
|
||||||
|
0 0 2px #222,
|
||||||
|
0 0 2px #222;
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
requests>=2.5.0
|
requests>=2.5.0
|
||||||
soupsieve<2.0
|
soupsieve
|
||||||
BeautifulSoup4>=4.0.0
|
BeautifulSoup4>=4.0.0
|
||||||
threadpool>=1.2.7
|
|
||||||
tabulate>=0.7.5
|
tabulate>=0.7.5
|
||||||
future>=0.15.2
|
future>=0.15.2
|
||||||
iso8601 >= 0.1
|
iso8601 >= 0.1
|
||||||
|
Reference in New Issue
Block a user