mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-07-01 16:09:28 +02:00
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
35c55503fa | |||
29aac84d53 | |||
4ed4523782 | |||
4223326c13 | |||
a248ff98c4 | |||
021f17d229 | |||
4162eabe93 | |||
dc54a43610 | |||
4ecffaff55 | |||
457f12d40d | |||
499081a9cd | |||
53aa04af1e | |||
473f948565 | |||
f701485840 | |||
d8e4f50609 | |||
a893f54da1 | |||
4e307911ce | |||
f9b7f828a5 | |||
092df9e539 | |||
8d74866abf | |||
bc5b7f982d | |||
e54f3cbd06 | |||
a31c615259 | |||
cf0b76204d | |||
17402623c4 | |||
a1a310f06b | |||
57673da762 |
@ -11,6 +11,8 @@ nhentai
|
||||
|
||||
nhentai is a CLI tool for downloading doujinshi from `nhentai.net <https://nhentai.net>`_
|
||||
|
||||
GUI version: `https://github.com/edgar1016/nhentai-GUI <https://github.com/edgar1016/nhentai-GUI>`_
|
||||
|
||||
===================
|
||||
Manual Installation
|
||||
===================
|
||||
@ -200,6 +202,8 @@ Other options:
|
||||
-P, --pdf generate PDF file
|
||||
--rm-origin-dir remove downloaded doujinshi dir when generated CBZ or
|
||||
PDF file
|
||||
--move-to-folder remove files in doujinshi dir then move new file to folder
|
||||
when generated CBZ or PDF file
|
||||
--meta generate a metadata file in doujinshi format
|
||||
--regenerate-cbz regenerate the cbz file if exists
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
__version__ = '0.5.3'
|
||||
__version__ = '0.5.6'
|
||||
__author__ = 'RicterZ'
|
||||
__email__ = 'ricterzheng@gmail.com'
|
||||
|
@ -73,6 +73,8 @@ def cmd_parser():
|
||||
help='search doujinshi by keyword')
|
||||
parser.add_option('--favorites', '-F', action='store_true', dest='favorites',
|
||||
help='list or download your favorites')
|
||||
parser.add_option('--artist', '-a', action='store', dest='artist',
|
||||
help='list doujinshi by artist name')
|
||||
|
||||
# page options
|
||||
parser.add_option('--page-all', dest='page_all', action='store_true', default=False,
|
||||
@ -112,6 +114,8 @@ def cmd_parser():
|
||||
help='generate PDF file')
|
||||
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')
|
||||
parser.add_option('--move-to-folder', dest='move_to_folder', action='store_true', default=False,
|
||||
help='remove files in doujinshi dir then move new file to folder when generated CBZ or PDF file')
|
||||
parser.add_option('--meta', dest='generate_metadata', action='store_true',
|
||||
help='generate a metadata file in doujinshi format')
|
||||
parser.add_option('--regenerate-cbz', dest='regenerate_cbz', action='store_true', default=False,
|
||||
@ -209,12 +213,12 @@ def cmd_parser():
|
||||
_ = [i.strip() for i in f.readlines()]
|
||||
args.id = set(int(i) for i in _ if i.isdigit())
|
||||
|
||||
if (args.is_download or args.is_show) and not args.id and not args.keyword and not args.favorites:
|
||||
if (args.is_download or args.is_show) and not args.id and not args.keyword and not args.favorites and not args.artist:
|
||||
logger.critical('Doujinshi id(s) are required for downloading')
|
||||
parser.print_help()
|
||||
sys.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 and not args.artist:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
|
@ -46,7 +46,7 @@ def main():
|
||||
if not options.is_download:
|
||||
logger.warning('You do not specify --download option')
|
||||
|
||||
doujinshis = favorites_parser(page=page_list)
|
||||
doujinshis = favorites_parser() if options.page_all else favorites_parser(page=page_list)
|
||||
|
||||
elif options.keyword:
|
||||
if constant.CONFIG['language']:
|
||||
@ -57,6 +57,10 @@ def main():
|
||||
doujinshis = _search_parser(options.keyword, sorting=options.sorting, page=page_list,
|
||||
is_page_all=options.page_all)
|
||||
|
||||
elif options.artist:
|
||||
doujinshis = legacy_search_parser(options.artist, sorting=options.sorting, page=page_list,
|
||||
is_page_all=options.page_all, type_='ARTIST')
|
||||
|
||||
elif not doujinshi_ids:
|
||||
doujinshi_ids = options.id
|
||||
|
||||
@ -96,9 +100,9 @@ def main():
|
||||
if not options.is_nohtml and not options.is_cbz and not options.is_pdf:
|
||||
generate_html(options.output_dir, doujinshi, template=constant.CONFIG['template'])
|
||||
elif options.is_cbz:
|
||||
generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
|
||||
generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir, True, options.move_to_folder)
|
||||
elif options.is_pdf:
|
||||
generate_pdf(options.output_dir, doujinshi, options.rm_origin_dir)
|
||||
generate_pdf(options.output_dir, doujinshi, options.rm_origin_dir, options.move_to_folder)
|
||||
|
||||
if options.main_viewer:
|
||||
generate_main_html(options.output_dir)
|
||||
|
@ -3,6 +3,23 @@ import os
|
||||
import tempfile
|
||||
|
||||
from urllib.parse import urlparse
|
||||
from platform import system
|
||||
|
||||
|
||||
def get_nhentai_home() -> str:
|
||||
home = os.getenv('HOME', tempfile.gettempdir())
|
||||
|
||||
if system() == 'Linux':
|
||||
xdgdat = os.getenv('XDG_DATA_HOME')
|
||||
if xdgdat and os.path.exists(os.path.join(xdgdat, 'nhentai')):
|
||||
return os.path.join(xdgdat, 'nhentai')
|
||||
if home and os.path.exists(os.path.join(home, '.nhentai')):
|
||||
return os.path.join(home, '.nhentai')
|
||||
if xdgdat:
|
||||
return os.path.join(xdgdat, 'nhentai')
|
||||
|
||||
# Use old default path in other systems
|
||||
return os.path.join(home, '.nhentai')
|
||||
|
||||
|
||||
DEBUG = os.getenv('DEBUG', False)
|
||||
@ -11,15 +28,22 @@ BASE_URL = os.getenv('NHENTAI', 'https://nhentai.net')
|
||||
DETAIL_URL = f'{BASE_URL}/g'
|
||||
LEGACY_SEARCH_URL = f'{BASE_URL}/search/'
|
||||
SEARCH_URL = f'{BASE_URL}/api/galleries/search'
|
||||
ARTIST_URL = f'{BASE_URL}/artist/'
|
||||
|
||||
TAG_API_URL = f'{BASE_URL}/api/galleries/tagged'
|
||||
LOGIN_URL = f'{BASE_URL}/login/'
|
||||
CHALLENGE_URL = f'{BASE_URL}/challenge'
|
||||
FAV_URL = f'{BASE_URL}/favorites/'
|
||||
|
||||
IMAGE_URL = f'{urlparse(BASE_URL).scheme}://i.{urlparse(BASE_URL).hostname}/galleries'
|
||||
|
||||
NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai')
|
||||
IMAGE_URL = f'{urlparse(BASE_URL).scheme}://i.{urlparse(BASE_URL).hostname}/galleries'
|
||||
IMAGE_URL_MIRRORS = [
|
||||
f'{urlparse(BASE_URL).scheme}://i3.{urlparse(BASE_URL).hostname}'
|
||||
f'{urlparse(BASE_URL).scheme}://i5.{urlparse(BASE_URL).hostname}'
|
||||
f'{urlparse(BASE_URL).scheme}://i7.{urlparse(BASE_URL).hostname}'
|
||||
]
|
||||
|
||||
NHENTAI_HOME = get_nhentai_home()
|
||||
NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
|
||||
NHENTAI_CONFIG_FILE = os.path.join(NHENTAI_HOME, 'config.json')
|
||||
|
||||
@ -30,7 +54,8 @@ CONFIG = {
|
||||
'cookie': '',
|
||||
'language': '',
|
||||
'template': '',
|
||||
'useragent': 'nhentai command line client (https://github.com/RicterZ/nhentai)'
|
||||
'useragent': 'nhentai command line client (https://github.com/RicterZ/nhentai)',
|
||||
'max_filename': 85
|
||||
}
|
||||
|
||||
LANGUAGE_ISO = {
|
||||
|
@ -67,10 +67,14 @@ class Downloader(Singleton):
|
||||
try:
|
||||
response = request('get', url, stream=True, timeout=self.timeout, proxies=proxy)
|
||||
if response.status_code != 200:
|
||||
raise NHentaiImageNotExistException
|
||||
|
||||
except NHentaiImageNotExistException as e:
|
||||
raise e
|
||||
path = urlparse(url).path
|
||||
for mirror in constant.IMAGE_URL_MIRRORS:
|
||||
print(f'{mirror}{path}')
|
||||
mirror_url = f'{mirror}{path}'
|
||||
response = request('get', mirror_url, stream=True,
|
||||
timeout=self.timeout, proxies=proxy)
|
||||
if response.status_code == 200:
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
i += 1
|
||||
|
@ -135,6 +135,7 @@ def doujinshi_parser(id_, counter=0):
|
||||
logger.warning(f'Error: {e}, ignored')
|
||||
return None
|
||||
|
||||
# print(response)
|
||||
html = BeautifulSoup(response, 'html.parser')
|
||||
doujinshi_info = html.find('div', attrs={'id': 'info'})
|
||||
|
||||
@ -240,13 +241,21 @@ def print_doujinshi(doujinshi_list):
|
||||
print(tabulate(tabular_data=doujinshi_list, headers=headers, tablefmt='rst'))
|
||||
|
||||
|
||||
def legacy_search_parser(keyword, sorting, page, is_page_all=False):
|
||||
def legacy_search_parser(keyword, sorting, page, is_page_all=False, type_='SEARCH'):
|
||||
logger.info(f'Searching doujinshis of keyword {keyword}')
|
||||
result = []
|
||||
|
||||
if type_ not in ('SEARCH', 'ARTIST', ):
|
||||
raise ValueError('Invalid type')
|
||||
|
||||
if is_page_all:
|
||||
response = request('get', url=constant.LEGACY_SEARCH_URL,
|
||||
params={'q': keyword, 'page': 1, 'sort': sorting}).content
|
||||
if type_ == 'SEARCH':
|
||||
response = request('get', url=constant.LEGACY_SEARCH_URL,
|
||||
params={'q': keyword, 'page': 1, 'sort': sorting}).content
|
||||
else:
|
||||
url = constant.ARTIST_URL + keyword + '/' + ('' if sorting == 'recent' else sorting)
|
||||
response = request('get', url=url, params={'page': 1}).content
|
||||
|
||||
html = BeautifulSoup(response, 'lxml')
|
||||
pagination = html.find(attrs={'class': 'pagination'})
|
||||
last_page = pagination.find(attrs={'class': 'last'})
|
||||
@ -258,8 +267,13 @@ def legacy_search_parser(keyword, sorting, page, is_page_all=False):
|
||||
|
||||
for p in pages:
|
||||
logger.info(f'Fetching page {p} ...')
|
||||
response = request('get', url=constant.LEGACY_SEARCH_URL,
|
||||
params={'q': keyword, 'page': p, 'sort': sorting}).content
|
||||
if type_ == 'SEARCH':
|
||||
response = request('get', url=constant.LEGACY_SEARCH_URL,
|
||||
params={'q': keyword, 'page': p, 'sort': sorting}).content
|
||||
else:
|
||||
url = constant.ARTIST_URL + keyword + '/' + ('' if sorting == 'recent' else sorting)
|
||||
response = request('get', url=url, params={'page': p}).content
|
||||
|
||||
if response is None:
|
||||
logger.warning(f'No result in response in page {p}')
|
||||
continue
|
||||
@ -313,7 +327,9 @@ def search_parser(keyword, sorting, page, is_page_all=False):
|
||||
|
||||
for row in response['result']:
|
||||
title = row['title']['english']
|
||||
title = title[:85] + '..' if len(title) > 85 else title
|
||||
title = title[:constant.CONFIG['max_filename']] + '..' if \
|
||||
len(title) > constant.CONFIG['max_filename'] else title
|
||||
|
||||
result.append({'id': row['id'], 'title': title})
|
||||
|
||||
not_exists_persist = False
|
||||
|
@ -163,9 +163,12 @@ def generate_main_html(output_dir='./'):
|
||||
logger.warning(f'Writing Main Viewer failed ({e})')
|
||||
|
||||
|
||||
def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=True):
|
||||
def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=True, move_to_folder=False):
|
||||
if doujinshi_obj is not None:
|
||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
||||
if os.path.exists(doujinshi_dir+".cbz"):
|
||||
logger.warning(f'Comic Book CBZ file exists, skip "{doujinshi_dir}"')
|
||||
return
|
||||
if write_comic_info:
|
||||
serialize_comic_xml(doujinshi_obj, doujinshi_dir)
|
||||
cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), f'{doujinshi_obj.filename}.cbz')
|
||||
@ -185,10 +188,21 @@ def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_
|
||||
if rm_origin_dir:
|
||||
shutil.rmtree(doujinshi_dir, ignore_errors=True)
|
||||
|
||||
if move_to_folder:
|
||||
for filename in os.listdir(doujinshi_dir):
|
||||
file_path = os.path.join(doujinshi_dir, filename)
|
||||
if os.path.isfile(file_path):
|
||||
try:
|
||||
os.remove(file_path)
|
||||
except Exception as e:
|
||||
print(f"Error deleting file: {e}")
|
||||
|
||||
shutil.move(cbz_filename, doujinshi_dir)
|
||||
|
||||
logger.log(16, f'Comic Book CBZ file has been written to "{doujinshi_dir}"')
|
||||
|
||||
|
||||
def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
|
||||
def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, move_to_folder=False):
|
||||
try:
|
||||
import img2pdf
|
||||
|
||||
@ -216,6 +230,17 @@ def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
|
||||
if rm_origin_dir:
|
||||
shutil.rmtree(doujinshi_dir, ignore_errors=True)
|
||||
|
||||
if move_to_folder:
|
||||
for filename in os.listdir(doujinshi_dir):
|
||||
file_path = os.path.join(doujinshi_dir, filename)
|
||||
if os.path.isfile(file_path):
|
||||
try:
|
||||
os.remove(file_path)
|
||||
except Exception as e:
|
||||
print(f"Error deleting file: {e}")
|
||||
|
||||
shutil.move(pdf_filename, doujinshi_dir)
|
||||
|
||||
logger.log(16, f'PDF file has been written to "{doujinshi_dir}"')
|
||||
|
||||
except ImportError:
|
||||
@ -232,7 +257,7 @@ def format_filename(s, length=MAX_FIELD_LENGTH, _truncate_only=False):
|
||||
# maybe you can use `--format` to select a suitable filename
|
||||
|
||||
if not _truncate_only:
|
||||
ban_chars = '\\\'/:,;*?"<>|\t'
|
||||
ban_chars = '\\\'/:,;*?"<>|\t\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b'
|
||||
filename = s.translate(str.maketrans(ban_chars, ' ' * len(ban_chars))).strip()
|
||||
filename = ' '.join(filename.split())
|
||||
|
||||
|
@ -139,7 +139,7 @@ function filter_searcher(){
|
||||
break
|
||||
}
|
||||
}
|
||||
if (verifier){doujinshi_id.push(data[i].Folder);}
|
||||
if (verifier){doujinshi_id.push(data[i].Folder.replace("_", " "));}
|
||||
}
|
||||
var gallery = document.getElementsByClassName("gallery-favorite");
|
||||
for (var i = 0; i < gallery.length; i++){
|
||||
@ -174,4 +174,4 @@ function tag_maker(data){
|
||||
document.getElementById("tags").appendChild(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
29
poetry.lock
generated
29
poetry.lock
generated
@ -1,10 +1,9 @@
|
||||
# This file is automatically @generated by Poetry 1.4.0 and should not be changed by hand.
|
||||
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "beautifulsoup4"
|
||||
version = "4.11.2"
|
||||
description = "Screen-scraping library"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.6.0"
|
||||
files = [
|
||||
@ -23,7 +22,6 @@ lxml = ["lxml"]
|
||||
name = "certifi"
|
||||
version = "2022.12.7"
|
||||
description = "Python package for providing Mozilla's CA Bundle."
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.6"
|
||||
files = [
|
||||
@ -35,7 +33,6 @@ files = [
|
||||
name = "charset-normalizer"
|
||||
version = "3.0.1"
|
||||
description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet."
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
files = [
|
||||
@ -133,7 +130,6 @@ files = [
|
||||
name = "idna"
|
||||
version = "3.4"
|
||||
description = "Internationalized Domain Names in Applications (IDNA)"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.5"
|
||||
files = [
|
||||
@ -145,7 +141,6 @@ files = [
|
||||
name = "iso8601"
|
||||
version = "1.1.0"
|
||||
description = "Simple module to parse ISO 8601 dates"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.6.2,<4.0"
|
||||
files = [
|
||||
@ -155,21 +150,20 @@ files = [
|
||||
|
||||
[[package]]
|
||||
name = "requests"
|
||||
version = "2.28.2"
|
||||
version = "2.31.0"
|
||||
description = "Python HTTP for Humans."
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.7, <4"
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "requests-2.28.2-py3-none-any.whl", hash = "sha256:64299f4909223da747622c030b781c0d7811e359c37124b4bd368fb8c6518baa"},
|
||||
{file = "requests-2.28.2.tar.gz", hash = "sha256:98b1b2782e3c6c4904938b84c0eb932721069dfdb9134313beff7c83c2df24bf"},
|
||||
{file = "requests-2.31.0-py3-none-any.whl", hash = "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f"},
|
||||
{file = "requests-2.31.0.tar.gz", hash = "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
certifi = ">=2017.4.17"
|
||||
charset-normalizer = ">=2,<4"
|
||||
idna = ">=2.5,<4"
|
||||
urllib3 = ">=1.21.1,<1.27"
|
||||
urllib3 = ">=1.21.1,<3"
|
||||
|
||||
[package.extras]
|
||||
socks = ["PySocks (>=1.5.6,!=1.5.7)"]
|
||||
@ -179,7 +173,6 @@ use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]
|
||||
name = "soupsieve"
|
||||
version = "2.4"
|
||||
description = "A modern CSS selector implementation for Beautiful Soup."
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
@ -191,7 +184,6 @@ files = [
|
||||
name = "tabulate"
|
||||
version = "0.9.0"
|
||||
description = "Pretty-print tabular data"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
@ -204,18 +196,17 @@ widechars = ["wcwidth"]
|
||||
|
||||
[[package]]
|
||||
name = "urllib3"
|
||||
version = "1.26.14"
|
||||
version = "1.26.18"
|
||||
description = "HTTP library with thread-safe connection pooling, file post, and more."
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*"
|
||||
files = [
|
||||
{file = "urllib3-1.26.14-py2.py3-none-any.whl", hash = "sha256:75edcdc2f7d85b137124a6c3c9fc3933cdeaa12ecb9a6a959f22797a0feca7e1"},
|
||||
{file = "urllib3-1.26.14.tar.gz", hash = "sha256:076907bf8fd355cde77728471316625a4d2f7e713c125f51953bb5b3eecf4f72"},
|
||||
{file = "urllib3-1.26.18-py2.py3-none-any.whl", hash = "sha256:34b97092d7e0a3a8cf7cd10e386f401b3737364026c45e622aa02903dffe0f07"},
|
||||
{file = "urllib3-1.26.18.tar.gz", hash = "sha256:f8ecc1bba5667413457c529ab955bf8c67b45db799d159066261719e328580a0"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
brotli = ["brotli (>=1.0.9)", "brotlicffi (>=0.8.0)", "brotlipy (>=0.6.0)"]
|
||||
brotli = ["brotli (==1.0.9)", "brotli (>=1.0.9)", "brotlicffi (>=0.8.0)", "brotlipy (>=0.6.0)"]
|
||||
secure = ["certifi", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "ipaddress", "pyOpenSSL (>=0.14)", "urllib3-secure-extra"]
|
||||
socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"]
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "nhentai"
|
||||
version = "0.5.2"
|
||||
version = "0.5.3"
|
||||
description = "nhentai doujinshi downloader"
|
||||
authors = ["Ricter Z <ricterzheng@gmail.com>"]
|
||||
license = "MIT"
|
||||
|
Reference in New Issue
Block a user