mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-07-02 00:19:29 +02:00
Compare commits
64 Commits
Author | SHA1 | Date | |
---|---|---|---|
c3f3182df3 | |||
12aad842f8 | |||
f9f76ab0f5 | |||
744a9e4418 | |||
c3e9fff491 | |||
a84e2c5714 | |||
c814c35c50 | |||
e2f71437e2 | |||
2fa45ae4df | |||
17bc33c6cb | |||
09bb8460f6 | |||
eb5b93d654 | |||
cb6cf6df1a | |||
98a66a3cb0 | |||
02d47632cf | |||
f932b1fbbe | |||
fd9e92f9d4 | |||
a8a48c6ce7 | |||
f6e9d08fc7 | |||
9c1c2ea069 | |||
984ae4262c | |||
cbf9448ed9 | |||
16bac45f02 | |||
7fa9193112 | |||
a05a308e71 | |||
5a29eaf775 | |||
497eb6fe50 | |||
4bfe104714 | |||
12364e980c | |||
b51e812449 | |||
0ed5fa1931 | |||
7f655b0f10 | |||
dec3f44542 | |||
40072a8483 | |||
f97469259d | |||
ec608cc741 | |||
30e2814fe2 | |||
da298e1fe7 | |||
51d43ddde0 | |||
c734881fc7 | |||
8d5803a45e | |||
b441085b45 | |||
132b26f8c4 | |||
a0dc952fd3 | |||
2bd862777b | |||
35c55503fa | |||
29aac84d53 | |||
4ed4523782 | |||
4223326c13 | |||
a248ff98c4 | |||
021f17d229 | |||
4162eabe93 | |||
c75e9efb21 | |||
f2dec5c2a3 | |||
845a0d5659 | |||
03d85c4e5d | |||
dc54a43610 | |||
4ecffaff55 | |||
457f12d40d | |||
499081a9cd | |||
53aa04af1e | |||
473f948565 | |||
f701485840 | |||
d8e4f50609 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@ dist/
|
||||
output/
|
||||
venv/
|
||||
.vscode/
|
||||
test-output
|
25
README.rst
25
README.rst
@ -161,25 +161,21 @@ Other options:
|
||||
NHENTAI nhentai mirror url
|
||||
|
||||
Options:
|
||||
# Operation options, control the program behaviors
|
||||
-h, --help show this help message and exit
|
||||
-D, --download download doujinshi (for search results)
|
||||
-S, --show just show the doujinshi information
|
||||
|
||||
# Doujinshi options, specify id, keyword, etc.
|
||||
--id doujinshi ids set, e.g. 167680 167681 167682
|
||||
-s KEYWORD, --search=KEYWORD
|
||||
search doujinshi by keyword
|
||||
-F, --favorites list or download your favorites
|
||||
|
||||
# Page options, control the page to fetch / download
|
||||
-a ARTIST, --artist=ARTIST
|
||||
list doujinshi by artist name
|
||||
--page-all all search results
|
||||
--page=PAGE, --page-range=PAGE
|
||||
page number of search results. e.g. 1,2-5,14
|
||||
--sorting=SORTING sorting of doujinshi (recent / popular /
|
||||
--sorting=SORTING, --sort=SORTING
|
||||
sorting of doujinshi (recent / popular /
|
||||
popular-[today|week])
|
||||
|
||||
# Download options, the output directory, threads, timeout, delay, etc.
|
||||
-o OUTPUT_DIR, --output=OUTPUT_DIR
|
||||
output dir
|
||||
-t THREADS, --threads=THREADS
|
||||
@ -192,8 +188,6 @@ Other options:
|
||||
-f FILE, --file=FILE read gallery IDs from file.
|
||||
--format=NAME_FORMAT format the saved folder name
|
||||
--dry-run Dry run, skip file download
|
||||
|
||||
# Generate options, for generate html viewer, cbz file, pdf file, etc
|
||||
--html generate a html viewer at current directory
|
||||
--no-html don't generate HTML after downloading
|
||||
--gen-main generate a main viewer contain all the doujin in the
|
||||
@ -202,12 +196,10 @@ 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
|
||||
--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
|
||||
|
||||
# nhentai options, set cookie, user-agent, language, remove caches, histories, etc
|
||||
--regenerate regenerate the cbz or pdf file if exists
|
||||
--cookie=COOKIE set cookie of nhentai to bypass Cloudflare captcha
|
||||
--useragent=USERAGENT, --user-agent=USERAGENT
|
||||
set useragent to bypass Cloudflare captcha
|
||||
@ -231,6 +223,9 @@ For example:
|
||||
.. code-block::
|
||||
|
||||
i.h.loli.club -> i.nhentai.net
|
||||
i3.h.loli.club -> i3.nhentai.net
|
||||
i5.h.loli.club -> i5.nhentai.net
|
||||
i7.h.loli.club -> i7.nhentai.net
|
||||
h.loli.club -> nhentai.net
|
||||
|
||||
Set `NHENTAI` env var to your nhentai mirror.
|
||||
|
@ -1,3 +1,3 @@
|
||||
__version__ = '0.5.4'
|
||||
__version__ = '0.5.12'
|
||||
__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,
|
||||
@ -116,8 +118,8 @@ def cmd_parser():
|
||||
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,
|
||||
help='regenerate the cbz file if exists')
|
||||
parser.add_option('--regenerate', dest='regenerate', action='store_true', default=False,
|
||||
help='regenerate the cbz or pdf file if exists')
|
||||
|
||||
# nhentai options
|
||||
parser.add_option('--cookie', type='str', dest='cookie', action='store',
|
||||
@ -211,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)
|
||||
|
||||
|
@ -1,4 +1,6 @@
|
||||
# coding: utf-8
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import signal
|
||||
import platform
|
||||
@ -11,8 +13,8 @@ from nhentai.doujinshi import Doujinshi
|
||||
from nhentai.downloader import Downloader
|
||||
from nhentai.logger import logger
|
||||
from nhentai.constant import BASE_URL
|
||||
from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, generate_metadata_file, \
|
||||
paging, check_cookie, signal_handler, DB
|
||||
from nhentai.utils import generate_html, generate_doc, generate_main_html, generate_metadata_file, \
|
||||
paging, check_cookie, signal_handler, DB, move_to_folder
|
||||
|
||||
|
||||
def main():
|
||||
@ -57,6 +59,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
|
||||
|
||||
@ -83,22 +89,40 @@ def main():
|
||||
|
||||
if not options.dryrun:
|
||||
doujinshi.downloader = downloader
|
||||
doujinshi.download(regenerate_cbz=options.regenerate_cbz)
|
||||
|
||||
if doujinshi.check_if_need_download(options):
|
||||
doujinshi.download()
|
||||
else:
|
||||
logger.info(f'Skip download doujinshi because a PDF/CBZ file exists of doujinshi {doujinshi.name}')
|
||||
continue
|
||||
|
||||
if options.generate_metadata:
|
||||
table = doujinshi.table
|
||||
generate_metadata_file(options.output_dir, table, doujinshi)
|
||||
generate_metadata_file(options.output_dir, doujinshi)
|
||||
|
||||
if options.is_save_download_history:
|
||||
with DB() as db:
|
||||
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:
|
||||
generate_html(options.output_dir, doujinshi, template=constant.CONFIG['template'])
|
||||
elif options.is_cbz:
|
||||
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, options.move_to_folder)
|
||||
|
||||
if options.is_cbz:
|
||||
generate_doc('cbz', options.output_dir, doujinshi, options.regenerate)
|
||||
|
||||
if options.is_pdf:
|
||||
generate_doc('pdf', options.output_dir, doujinshi, options.regenerate)
|
||||
|
||||
if options.move_to_folder:
|
||||
if options.is_cbz:
|
||||
move_to_folder(options.output_dir, doujinshi, 'cbz')
|
||||
if options.is_pdf:
|
||||
move_to_folder(options.output_dir, doujinshi, 'pdf')
|
||||
|
||||
if options.rm_origin_dir:
|
||||
if options.move_to_folder:
|
||||
logger.critical('You specified both --move-to-folder and --rm-origin-dir options, '
|
||||
'you will not get anything :(')
|
||||
shutil.rmtree(os.path.join(options.output_dir, doujinshi.filename), ignore_errors=True)
|
||||
|
||||
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 = {
|
||||
|
@ -1,4 +1,5 @@
|
||||
# coding: utf-8
|
||||
import os
|
||||
|
||||
from tabulate import tabulate
|
||||
|
||||
@ -55,6 +56,7 @@ class Doujinshi(object):
|
||||
['Parodies', self.info.parodies],
|
||||
['Doujinshi', self.name],
|
||||
['Subtitle', self.info.subtitle],
|
||||
['Date', self.info.date],
|
||||
['Characters', self.info.characters],
|
||||
['Authors', self.info.artists],
|
||||
['Groups', self.info.groups],
|
||||
@ -70,7 +72,35 @@ class Doujinshi(object):
|
||||
def show(self):
|
||||
logger.info(f'Print doujinshi information of {self.id}\n{tabulate(self.table)}')
|
||||
|
||||
def download(self, regenerate_cbz=False):
|
||||
def check_if_need_download(self, options):
|
||||
base_path = os.path.join(self.downloader.path, self.filename)
|
||||
|
||||
# regenerate, re-download
|
||||
if options.regenerate:
|
||||
return True
|
||||
|
||||
# pdf or cbz file exists, skip re-download
|
||||
# doujinshi directory may not exist b/c of --rm-origin-dir option set.
|
||||
# user should pass --regenerate option to get back origin dir.
|
||||
ret_pdf = ret_cbz = None
|
||||
if options.is_pdf:
|
||||
ret_pdf = os.path.exists(f'{base_path}.pdf') or os.path.exists(f'{base_path}/{self.filename}.pdf')
|
||||
|
||||
if options.is_cbz:
|
||||
ret_cbz = os.path.exists(f'{base_path}.cbz') or os.path.exists(f'{base_path}/{self.filename}.cbz')
|
||||
|
||||
ret = list(filter(lambda s: s is not None, [ret_cbz, ret_pdf]))
|
||||
if ret and all(ret):
|
||||
return False
|
||||
|
||||
# doujinshi directory doesn't exist, re-download
|
||||
if not (os.path.exists(base_path) and os.path.isdir(base_path)):
|
||||
return True
|
||||
|
||||
# fallback
|
||||
return True
|
||||
|
||||
def download(self):
|
||||
logger.info(f'Starting to download doujinshi: {self.name}')
|
||||
if self.downloader:
|
||||
download_queue = []
|
||||
@ -80,9 +110,10 @@ class Doujinshi(object):
|
||||
for i in range(1, min(self.pages, len(self.ext)) + 1):
|
||||
download_queue.append(f'{IMAGE_URL}/{self.img_id}/{i}.{self.ext[i-1]}')
|
||||
|
||||
self.downloader.start_download(download_queue, self.filename, regenerate_cbz=regenerate_cbz)
|
||||
return self.downloader.start_download(download_queue, self.filename)
|
||||
else:
|
||||
logger.critical('Downloader has not been loaded')
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -57,7 +57,7 @@ class Downloader(Singleton):
|
||||
save_file_path = os.path.join(folder, base_filename.zfill(3) + extension)
|
||||
try:
|
||||
if os.path.exists(save_file_path):
|
||||
logger.warning(f'Ignored exists file: {save_file_path}')
|
||||
logger.warning(f'Skipped download: {save_file_path} already exists')
|
||||
return 1, url
|
||||
|
||||
response = None
|
||||
@ -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
|
||||
@ -111,18 +115,13 @@ class Downloader(Singleton):
|
||||
|
||||
return 1, url
|
||||
|
||||
def start_download(self, queue, folder='', regenerate_cbz=False):
|
||||
def start_download(self, queue, folder='') -> bool:
|
||||
if not isinstance(folder, (str, )):
|
||||
folder = str(folder)
|
||||
|
||||
if self.path:
|
||||
folder = os.path.join(self.path, folder)
|
||||
|
||||
if os.path.exists(folder + '.cbz'):
|
||||
if not regenerate_cbz:
|
||||
logger.warning(f'CBZ file "{folder}.cbz" exists, ignored download request')
|
||||
return
|
||||
|
||||
logger.info(f'Doujinshi will be saved at "{folder}"')
|
||||
if not os.path.exists(folder):
|
||||
try:
|
||||
@ -130,9 +129,9 @@ class Downloader(Singleton):
|
||||
except EnvironmentError as e:
|
||||
logger.critical(str(e))
|
||||
|
||||
else:
|
||||
logger.warning(f'Path "{folder}" already exist.')
|
||||
|
||||
if os.getenv('DEBUG', None) == 'NODOWNLOAD':
|
||||
# Assuming we want to continue with rest of process.
|
||||
return True
|
||||
queue = [(self, url, folder, constant.CONFIG['proxy']) for url in queue]
|
||||
|
||||
pool = multiprocessing.Pool(self.size, init_worker)
|
||||
@ -141,6 +140,8 @@ class Downloader(Singleton):
|
||||
pool.close()
|
||||
pool.join()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def download_wrapper(obj, url, folder='', proxy=None):
|
||||
if sys.platform == 'darwin' or semaphore.get_value():
|
||||
|
@ -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
|
||||
|
@ -22,7 +22,7 @@ def serialize_json(doujinshi, output_dir):
|
||||
metadata['group'] = [i.strip() for i in doujinshi.info.groups.split(',')]
|
||||
if doujinshi.info.languages:
|
||||
metadata['language'] = [i.strip() for i in doujinshi.info.languages.split(',')]
|
||||
metadata['category'] = doujinshi.info.categories
|
||||
metadata['category'] = [i.strip() for i in doujinshi.info.categories.split(',')]
|
||||
metadata['URL'] = doujinshi.url
|
||||
metadata['Pages'] = doujinshi.pages
|
||||
|
||||
|
173
nhentai/utils.py
173
nhentai/utils.py
@ -5,14 +5,16 @@ import re
|
||||
import os
|
||||
import zipfile
|
||||
import shutil
|
||||
|
||||
import requests
|
||||
import sqlite3
|
||||
import urllib.parse
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from nhentai import constant
|
||||
from nhentai.logger import logger
|
||||
from nhentai.serializer import serialize_json, serialize_comic_xml, set_js_database
|
||||
|
||||
|
||||
MAX_FIELD_LENGTH = 100
|
||||
|
||||
|
||||
@ -38,7 +40,8 @@ def check_cookie():
|
||||
|
||||
username = re.findall('"/users/[0-9]+/(.*?)"', response.text)
|
||||
if not username:
|
||||
logger.warning('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:
|
||||
logger.log(16, f'Login successfully! Your username: {username[0]}')
|
||||
|
||||
@ -64,13 +67,31 @@ def readfile(path):
|
||||
return file.read()
|
||||
|
||||
|
||||
def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
|
||||
image_html = ''
|
||||
def parse_doujinshi_obj(
|
||||
output_dir: str,
|
||||
doujinshi_obj=None,
|
||||
file_type: str = ''
|
||||
) -> Tuple[str, str]:
|
||||
|
||||
filename = f'./doujinshi.{file_type}'
|
||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
||||
if doujinshi_obj is not None:
|
||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
||||
else:
|
||||
doujinshi_dir = '.'
|
||||
_filename = f'{doujinshi_obj.filename}.{file_type}'
|
||||
|
||||
if file_type == 'cbz':
|
||||
serialize_comic_xml(doujinshi_obj, doujinshi_dir)
|
||||
|
||||
if file_type == 'pdf':
|
||||
_filename = _filename.replace('/', '-')
|
||||
|
||||
filename = os.path.join(output_dir, _filename)
|
||||
|
||||
return doujinshi_dir, filename
|
||||
|
||||
|
||||
def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
|
||||
doujinshi_dir, filename = parse_doujinshi_obj(output_dir, doujinshi_obj, '.html')
|
||||
image_html = ''
|
||||
|
||||
if not os.path.exists(doujinshi_dir):
|
||||
logger.warning(f'Path "{doujinshi_dir}" does not exist, creating.')
|
||||
@ -107,6 +128,27 @@ def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
|
||||
logger.warning(f'Writing HTML Viewer failed ({e})')
|
||||
|
||||
|
||||
def move_to_folder(output_dir='.', doujinshi_obj=None, file_type=None):
|
||||
if not file_type:
|
||||
raise RuntimeError('no file_type specified')
|
||||
|
||||
doujinshi_dir, filename = parse_doujinshi_obj(output_dir, doujinshi_obj, file_type)
|
||||
|
||||
for fn in os.listdir(doujinshi_dir):
|
||||
file_path = os.path.join(doujinshi_dir, fn)
|
||||
_, ext = os.path.splitext(file_path)
|
||||
if ext in ['.pdf', '.cbz']:
|
||||
continue
|
||||
|
||||
if os.path.isfile(file_path):
|
||||
try:
|
||||
os.remove(file_path)
|
||||
except Exception as e:
|
||||
print(f"Error deleting file: {e}")
|
||||
|
||||
shutil.move(filename, os.path.join(doujinshi_dir, os.path.basename(filename)))
|
||||
|
||||
|
||||
def generate_main_html(output_dir='./'):
|
||||
"""
|
||||
Generate a main html to show all the contains doujinshi.
|
||||
@ -148,7 +190,7 @@ def generate_main_html(output_dir='./'):
|
||||
else:
|
||||
title = 'nHentai HTML Viewer'
|
||||
|
||||
image_html += element.format(FOLDER=folder, IMAGE=image, TITLE=title)
|
||||
image_html += element.format(FOLDER=urllib.parse.quote(folder), IMAGE=image, TITLE=title)
|
||||
if image_html == '':
|
||||
logger.warning('No index.html found, --gen-main paused.')
|
||||
return
|
||||
@ -158,90 +200,50 @@ def generate_main_html(output_dir='./'):
|
||||
f.write(data.encode('utf-8'))
|
||||
shutil.copy(os.path.dirname(__file__) + '/viewer/logo.png', './')
|
||||
set_js_database()
|
||||
logger.log(16, f'Main Viewer has been written to "{output_dir}main.html"')
|
||||
output_dir = output_dir[:-1] if output_dir.endswith('/') else output_dir
|
||||
logger.log(16, f'Main Viewer has been written to "{output_dir}/main.html"')
|
||||
except Exception as e:
|
||||
logger.warning(f'Writing Main Viewer failed ({e})')
|
||||
|
||||
|
||||
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 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')
|
||||
else:
|
||||
cbz_filename = './doujinshi.cbz'
|
||||
doujinshi_dir = '.'
|
||||
def generate_doc(file_type='', output_dir='.', doujinshi_obj=None, regenerate=False):
|
||||
|
||||
file_list = os.listdir(doujinshi_dir)
|
||||
file_list.sort()
|
||||
doujinshi_dir, filename = parse_doujinshi_obj(output_dir, doujinshi_obj, file_type)
|
||||
|
||||
logger.info(f'Writing CBZ file to path: {cbz_filename}')
|
||||
with zipfile.ZipFile(cbz_filename, 'w') as cbz_pf:
|
||||
for image in file_list:
|
||||
image_path = os.path.join(doujinshi_dir, image)
|
||||
cbz_pf.write(image_path, image)
|
||||
|
||||
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, move_to_folder=False):
|
||||
try:
|
||||
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, '..'),
|
||||
f'{doujinshi_obj.filename}.pdf'
|
||||
)
|
||||
else:
|
||||
pdf_filename = './doujinshi.pdf'
|
||||
doujinshi_dir = '.'
|
||||
if os.path.exists(f'{doujinshi_dir}.{file_type}') and not regenerate:
|
||||
logger.info(f'Skipped {file_type} file generation: {doujinshi_dir}.{file_type} already exists')
|
||||
return
|
||||
|
||||
if file_type == 'cbz':
|
||||
file_list = os.listdir(doujinshi_dir)
|
||||
file_list.sort()
|
||||
|
||||
logger.info(f'Writing PDF file to path: {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))
|
||||
logger.info(f'Writing CBZ file to path: {filename}')
|
||||
with zipfile.ZipFile(filename, 'w') as cbz_pf:
|
||||
for image in file_list:
|
||||
image_path = os.path.join(doujinshi_dir, image)
|
||||
cbz_pf.write(image_path, image)
|
||||
|
||||
if rm_origin_dir:
|
||||
shutil.rmtree(doujinshi_dir, ignore_errors=True)
|
||||
logger.log(16, f'Comic Book CBZ file has been written to "{filename}"')
|
||||
elif file_type == 'pdf':
|
||||
try:
|
||||
import img2pdf
|
||||
|
||||
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}")
|
||||
"""Write images to a PDF file using img2pdf."""
|
||||
file_list = [f for f in os.listdir(doujinshi_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif'))]
|
||||
file_list.sort()
|
||||
|
||||
shutil.move(pdf_filename, doujinshi_dir)
|
||||
logger.info(f'Writing PDF file to path: {filename}')
|
||||
with open(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, rotation=img2pdf.Rotation.ifvalid))
|
||||
|
||||
logger.log(16, f'PDF file has been written to "{doujinshi_dir}"')
|
||||
logger.log(16, f'PDF file has been written to "{filename}"')
|
||||
|
||||
except ImportError:
|
||||
logger.error("Please install img2pdf package by using pip.")
|
||||
except ImportError:
|
||||
logger.error("Please install img2pdf package by using pip.")
|
||||
|
||||
|
||||
def format_filename(s, length=MAX_FIELD_LENGTH, _truncate_only=False):
|
||||
@ -297,32 +299,27 @@ def paging(page_string):
|
||||
return page_list
|
||||
|
||||
|
||||
def generate_metadata_file(output_dir, table, doujinshi_obj=None):
|
||||
logger.info('Writing Metadata Info')
|
||||
def generate_metadata_file(output_dir, doujinshi_obj):
|
||||
|
||||
if doujinshi_obj is not None:
|
||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
||||
else:
|
||||
doujinshi_dir = '.'
|
||||
info_txt_path = os.path.join(output_dir, doujinshi_obj.filename, 'info.txt')
|
||||
|
||||
logger.info(doujinshi_dir)
|
||||
|
||||
f = open(os.path.join(doujinshi_dir, 'info.txt'), 'w', encoding='utf-8')
|
||||
f = open(info_txt_path, 'w', encoding='utf-8')
|
||||
|
||||
fields = ['TITLE', 'ORIGINAL TITLE', 'AUTHOR', 'ARTIST', 'GROUPS', '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', 'GROUPS',
|
||||
special_fields = ['PARODY', 'TITLE', 'ORIGINAL TITLE', 'DATE', 'CHARACTERS', 'AUTHOR', 'GROUPS',
|
||||
'LANGUAGE', 'TAGS', 'URL', 'PAGES']
|
||||
|
||||
for i in range(len(fields)):
|
||||
f.write(f'{fields[i]}: ')
|
||||
if fields[i] in special_fields:
|
||||
f.write(str(table[special_fields.index(fields[i])][1]))
|
||||
f.write(str(doujinshi_obj.table[special_fields.index(fields[i])][1]))
|
||||
f.write('\n')
|
||||
|
||||
f.close()
|
||||
logger.log(16, f'Metadata Info has been written to "{info_txt_path}"')
|
||||
|
||||
|
||||
class DB(object):
|
||||
|
@ -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++){
|
||||
|
30
poetry.lock
generated
30
poetry.lock
generated
@ -1,4 +1,4 @@
|
||||
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
|
||||
# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "beautifulsoup4"
|
||||
@ -20,13 +20,13 @@ lxml = ["lxml"]
|
||||
|
||||
[[package]]
|
||||
name = "certifi"
|
||||
version = "2022.12.7"
|
||||
version = "2024.7.4"
|
||||
description = "Python package for providing Mozilla's CA Bundle."
|
||||
optional = false
|
||||
python-versions = ">=3.6"
|
||||
files = [
|
||||
{file = "certifi-2022.12.7-py3-none-any.whl", hash = "sha256:4ad3232f5e926d6718ec31cfc1fcadfde020920e278684144551c91769c7bc18"},
|
||||
{file = "certifi-2022.12.7.tar.gz", hash = "sha256:35824b4c3a97115964b408844d64aa14db1cc518f6562e8d7261699d1350a9e3"},
|
||||
{file = "certifi-2024.7.4-py3-none-any.whl", hash = "sha256:c198e21b1289c2ab85ee4e67bb4b4ef3ead0892059901a8d5b622f24a1101e90"},
|
||||
{file = "certifi-2024.7.4.tar.gz", hash = "sha256:5a1e7645bc0ec61a09e26c36f6106dd4cf40c6db3a1fb6352b0244e7fb057c7b"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -128,13 +128,13 @@ files = [
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "3.4"
|
||||
version = "3.7"
|
||||
description = "Internationalized Domain Names in Applications (IDNA)"
|
||||
optional = false
|
||||
python-versions = ">=3.5"
|
||||
files = [
|
||||
{file = "idna-3.4-py3-none-any.whl", hash = "sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2"},
|
||||
{file = "idna-3.4.tar.gz", hash = "sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4"},
|
||||
{file = "idna-3.7-py3-none-any.whl", hash = "sha256:82fee1fc78add43492d3a1898bfa6d8a904cc97d8427f683ed8e798d07761aa0"},
|
||||
{file = "idna-3.7.tar.gz", hash = "sha256:028ff3aadf0609c1fd278d8ea3089299412a7a8b9bd005dd08b9f8285bcb5cfc"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -150,13 +150,13 @@ files = [
|
||||
|
||||
[[package]]
|
||||
name = "requests"
|
||||
version = "2.31.0"
|
||||
version = "2.32.0"
|
||||
description = "Python HTTP for Humans."
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
python-versions = ">=3.8"
|
||||
files = [
|
||||
{file = "requests-2.31.0-py3-none-any.whl", hash = "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f"},
|
||||
{file = "requests-2.31.0.tar.gz", hash = "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1"},
|
||||
{file = "requests-2.32.0-py3-none-any.whl", hash = "sha256:f2c3881dddb70d056c5bd7600a4fae312b2a300e39be6a118d30b90bd27262b5"},
|
||||
{file = "requests-2.32.0.tar.gz", hash = "sha256:fa5490319474c82ef1d2c9bc459d3652e3ae4ef4c4ebdd18a21145a47ca4b6b8"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
@ -196,13 +196,13 @@ widechars = ["wcwidth"]
|
||||
|
||||
[[package]]
|
||||
name = "urllib3"
|
||||
version = "1.26.18"
|
||||
version = "1.26.19"
|
||||
description = "HTTP library with thread-safe connection pooling, file post, and more."
|
||||
optional = false
|
||||
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*"
|
||||
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,>=2.7"
|
||||
files = [
|
||||
{file = "urllib3-1.26.18-py2.py3-none-any.whl", hash = "sha256:34b97092d7e0a3a8cf7cd10e386f401b3737364026c45e622aa02903dffe0f07"},
|
||||
{file = "urllib3-1.26.18.tar.gz", hash = "sha256:f8ecc1bba5667413457c529ab955bf8c67b45db799d159066261719e328580a0"},
|
||||
{file = "urllib3-1.26.19-py2.py3-none-any.whl", hash = "sha256:37a0344459b199fce0e80b0d3569837ec6b6937435c5244e7fd73fa6006830f3"},
|
||||
{file = "urllib3-1.26.19.tar.gz", hash = "sha256:3e3d753a8618b86d7de333b4223005f68720bcd6a7d2bcb9fbd2229ec7c1e429"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
|
@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "nhentai"
|
||||
version = "0.5.3"
|
||||
version = "0.5.12"
|
||||
description = "nhentai doujinshi downloader"
|
||||
authors = ["Ricter Z <ricterzheng@gmail.com>"]
|
||||
license = "MIT"
|
||||
|
@ -1,5 +1,6 @@
|
||||
requests
|
||||
soupsieve
|
||||
setuptools
|
||||
BeautifulSoup4
|
||||
tabulate
|
||||
iso8601
|
||||
|
Reference in New Issue
Block a user